Skip to content

Commit

Permalink
Issue #168: Fix change_association with only a scope proc
Browse files Browse the repository at this point in the history
  • Loading branch information
FLeinzi committed Oct 6, 2022
1 parent cef4599 commit 18006da
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_type/change_association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ChangeAssociation

module ClassMethods

def change_association(association_name, new_scope, new_options = nil)
def change_association(association_name, new_scope, new_options = {})
if (existing_association = self.reflect_on_association(association_name))
if new_scope.is_a?(Hash)
new_options = new_scope
Expand Down
23 changes: 23 additions & 0 deletions spec/active_type/change_association_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ def self.name
expect(extended_nice_children.size).to eq 2
expect(extended_nice_children.first).to be_instance_of(ExtendedChild)
end

it 'does not raise an error when overriding scopes without new_options' do
record = Record.create
Child.create(record: record, nice: true)
Child.create(record: record, nice: false)
Child.create(record: record, nice: false)
expect(record.nice_children.size).to eq 1

extended_proc = ->(_class) do
def self.name
'ExtendedRecord'
end

# today is opposite day
change_association :nice_children, -> { where(nice: false) }
end

expect { @extended_class = Class.new(ActiveType::Record[Record], &extended_proc) }.not_to raise_error

extended_nice_children = @extended_class.first.nice_children
expect(extended_nice_children.size).to eq 2
expect(extended_nice_children.first).to be_instance_of(Child)
end
end

end
Expand Down

0 comments on commit 18006da

Please sign in to comment.