Skip to content

Commit

Permalink
fix: condition explicitly uses attribute values and not public_send
Browse files Browse the repository at this point in the history
Using public_send caused an issue when attribute names shadowed method
names. Closes #280
  • Loading branch information
flavorjones committed Jun 7, 2023
1 parent 7a07bcc commit 3669654
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/active_hash/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def matches?(record)
expectation_method = inverted ? :any? : :all?

constraints.send(expectation_method) do |attribute, expected|
value = record.public_send(attribute)
value = record[attribute]

matches_value?(value, expected)
end
Expand All @@ -41,4 +41,4 @@ def matches_value?(value, comparison)
def normalize(value)
value.respond_to?(:to_s) ? value.to_s : value
end
end
end
20 changes: 20 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@
expect(array.size).to eq(2)
end
end

describe "colliding methods" do
it "should handle attributes named after existing methods" do
klass = Class.new(ActiveHash::Base) do
self.data = [
{
id: 1,
name: "Aaa",
display: true,
},
{
id: 2,
name: "Bbb",
display: false,
},
]
end
expect(klass.where(display: true).length).to eq(1)
end
end
end

0 comments on commit 3669654

Please sign in to comment.