Skip to content

Commit

Permalink
Merge pull request #281 from active-hash/280-object-method-collision
Browse files Browse the repository at this point in the history
Condition explicitly uses attribute values and not public_send
  • Loading branch information
kbrock authored Jun 20, 2023
2 parents 7a07bcc + 911e7a2 commit 45cfdb8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# active_hash Changelog

## next - unreleased

- Fix relation matching when attribute name collides with a method. [#281](https://github.com/active-hash/active_hash/pull/281) @flavorjones


## Version [3.2.0] - <sub><sup>2022-07-14</sup></sub>

- Add Ruby 3.2 to the CI matrix [#275](https://github.com/active-hash/active_hash/pull/275) @petergoldstein
Expand Down
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.read_attribute(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
21 changes: 21 additions & 0 deletions spec/active_hash/relation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,25 @@
expect(array.size).to eq(2)
end
end

describe "colliding methods https://github.com/active-hash/active_hash/issues/280" 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 45cfdb8

Please sign in to comment.