Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## [Unreleased]

- Fix compatibility with Rails `delegated_type` by accepting optional `scope` parameter in `safe_polymorphic` method signature

## [0.1.0] - 2022-12-28

- Initial release
Expand All @@ -11,4 +13,4 @@

## [0.1.2] - 2022-12-28

- Fix issue that forced to declare the I18n locale (`class_not_allowed`) in each project using this gem instead of using the default locale included in the gem
- Fix issue that forced to declare the I18n locale (`class_not_allowed`) in each project using this gem instead of using the default locale included in the gem
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ GEM
PLATFORMS
arm64-darwin-21
arm64-darwin-23
arm64-darwin-24
x86_64-linux

DEPENDENCIES
Expand Down
4 changes: 2 additions & 2 deletions lib/safe_polymorphic/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class << base
end

module ClassMethods
def safe_polymorphic(name, **options)
unsafe_polymorphic name, **options
def safe_polymorphic(name, scope = nil, **options)
unsafe_polymorphic name, scope, **options
polymorphic = options[:polymorphic]

return unless polymorphic.present? && polymorphic.class != TrueClass
Expand Down
61 changes: 61 additions & 0 deletions spec/safe_polymorphic/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,65 @@
end
end
end

describe 'with delegated_type' do
before do
Book.create(title: 'Book', owner: User.first)
end
it 'should work alongside delegated_type without errors' do
expect(Address).to respond_to(:addressable_types)
expect(Address.respond_to?(:with_addressable_user)).to be true
expect(Address.respond_to?(:with_addressable_publisher)).to be true
end

it 'should create an Address with a User addressable' do
user = User.first
address = Address.new(addressable: user, delegated: Book.first)

expect(address.addressable_type).to eq('User')
expect(address.addressable).to eq(user)
end

it 'should create an Address with a Publisher addressable' do
publisher = Publisher.first
address = Address.new(addressable: publisher, delegated: Book.first)

expect(address.addressable_type).to eq('Publisher')
expect(address.addressable).to eq(publisher)
end

it 'should have safe_polymorphic helper methods working' do
expect(Address.addressable_types).to match_array([User, Publisher])
end

it 'should reject invalid addressable types' do
other = OtherThing.create
address = Address.new(addressable: other, delegated: Book.first)

expect(address).to_not be_valid
expect(address.errors[:addressable_type]).to include('OtherThing is not an allowed class')
end

it 'should provide scopes for safe_polymorphic' do
user = User.first
publisher = Publisher.first

Address.create(addressable: user, delegated: Book.first)
Address.create(addressable: publisher, delegated: Book.first)

expect(Address.with_addressable_user.count).to eq(1)
expect(Address.with_addressable_publisher.count).to eq(1)
end

it 'should provide class methods of delegated_type ' do
user = User.first
book = Book.first

address = Address.create(addressable: user, delegated: book)

expect(Address.with_addressable_user.first).to eq(address)
expect(address.addressable).to eq(user)
expect(address.delegated).to eq(book)
end
end
end
1 change: 1 addition & 0 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
require_relative 'models/publisher'
require_relative 'models/book'
require_relative 'models/other_thing'
require_relative 'models/address'
6 changes: 6 additions & 0 deletions spec/support/models/address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Address < ActiveRecord::Base
belongs_to :addressable, polymorphic: [Publisher, User]
delegated_type :delegated, types: %w[Book], dependent: :destroy
end
9 changes: 9 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@

t.timestamps
end

create_table :addresses, force: true do |t|
t.integer :addressable_id
t.string :addressable_type
t.bigint :delegated_id, null: false
t.string :delegated_type, null: false

t.timestamps
end
end
Loading