Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #where for hashes with string keys #292

Merged
merged 3 commits into from
Nov 15, 2023
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
2 changes: 1 addition & 1 deletion lib/active_hash/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def [](key)
end

def _read_attribute(key)
attributes[key]
attributes[key.to_sym]
end
alias_method :read_attribute, :_read_attribute

Expand Down
19 changes: 19 additions & 0 deletions spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ class Region < ActiveHash::Base
expect(record.first.name).to eq('US')
end

it "filters records when passed a hash with string keys" do
record = Country.where('name' => 'US')
expect(record.count).to eq(1)
expect(record.first.id).to eq(1)
expect(record.first.name).to eq('US')
end

it "raises an error if ids aren't unique" do
expect do
Country.data = [
Expand Down Expand Up @@ -1194,12 +1201,24 @@ class Region < ActiveHash::Base
expect(country._read_attribute(:foo)).to eq(:bar)
end

it "works when string key passed to _read_attribute" do
Country.field :foo
country = Country.new(:foo => :bar)
expect(country._read_attribute('foo')).to eq(:bar)
end

it "works with read_attribute" do
Country.field :foo
country = Country.new(:foo => :bar)
expect(country.read_attribute(:foo)).to eq(:bar)
end

it "works when string key passed to read_attribute" do
Country.field :foo
country = Country.new(:foo => :bar)
expect(country.read_attribute('foo')).to eq(:bar)
end

it "works with #[]=" do
Country.field :foo
country = Country.new
Expand Down