|
205 | 205 | end |
206 | 206 | end |
207 | 207 | end |
| 208 | + |
| 209 | + describe 'with delegated_type' do |
| 210 | + before do |
| 211 | + Book.create(title: 'Book', owner: User.first) |
| 212 | + end |
| 213 | + it 'should work alongside delegated_type without errors' do |
| 214 | + expect(Address).to respond_to(:addressable_types) |
| 215 | + expect(Address.respond_to?(:with_addressable_user)).to be true |
| 216 | + expect(Address.respond_to?(:with_addressable_publisher)).to be true |
| 217 | + end |
| 218 | + |
| 219 | + it 'should create an Address with a User addressable' do |
| 220 | + user = User.first |
| 221 | + address = Address.new(addressable: user, delegated: Book.first) |
| 222 | + |
| 223 | + expect(address.addressable_type).to eq('User') |
| 224 | + expect(address.addressable).to eq(user) |
| 225 | + end |
| 226 | + |
| 227 | + it 'should create an Address with a Publisher addressable' do |
| 228 | + publisher = Publisher.first |
| 229 | + address = Address.new(addressable: publisher, delegated: Book.first) |
| 230 | + |
| 231 | + expect(address.addressable_type).to eq('Publisher') |
| 232 | + expect(address.addressable).to eq(publisher) |
| 233 | + end |
| 234 | + |
| 235 | + it 'should have safe_polymorphic helper methods working' do |
| 236 | + expect(Address.addressable_types).to match_array([User, Publisher]) |
| 237 | + end |
| 238 | + |
| 239 | + it 'should reject invalid addressable types' do |
| 240 | + other = OtherThing.create |
| 241 | + address = Address.new(addressable: other, delegated: Book.first) |
| 242 | + |
| 243 | + expect(address).to_not be_valid |
| 244 | + expect(address.errors[:addressable_type]).to include('OtherThing is not an allowed class') |
| 245 | + end |
| 246 | + |
| 247 | + it 'should provide scopes for safe_polymorphic' do |
| 248 | + user = User.first |
| 249 | + publisher = Publisher.first |
| 250 | + |
| 251 | + Address.create(addressable: user, delegated: Book.first) |
| 252 | + Address.create(addressable: publisher, delegated: Book.first) |
| 253 | + |
| 254 | + expect(Address.with_addressable_user.count).to eq(1) |
| 255 | + expect(Address.with_addressable_publisher.count).to eq(1) |
| 256 | + end |
| 257 | + |
| 258 | + it 'should provide class methods of delegated_type ' do |
| 259 | + user = User.first |
| 260 | + book = Book.first |
| 261 | + |
| 262 | + address = Address.create(addressable: user, delegated: book) |
| 263 | + |
| 264 | + expect(Address.with_addressable_user.first).to eq(address) |
| 265 | + expect(address.addressable).to eq(user) |
| 266 | + expect(address.delegated).to eq(book) |
| 267 | + end |
| 268 | + end |
208 | 269 | end |
0 commit comments