From 63e135523abd8fca8fac70b4f07430b727d5a4f3 Mon Sep 17 00:00:00 2001 From: Stanislav Dobrovolschii Date: Tue, 24 Oct 2023 01:34:58 +0200 Subject: [PATCH 1/3] Failing spec Signed-off-by: Stanislav Dobrovolschii --- spec/active_hash/base_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 314cfdba..7df04c06 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -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 = [ From 46d468bc6d14890093025bae5c59c60577dbc2fd Mon Sep 17 00:00:00 2001 From: Stanislav Dobrovolschii Date: Tue, 24 Oct 2023 02:08:08 +0200 Subject: [PATCH 2/3] Symbolize key when accessing attribute Signed-off-by: Stanislav Dobrovolschii --- lib/active_hash/condition.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/active_hash/condition.rb b/lib/active_hash/condition.rb index cf78b052..40ee00cf 100644 --- a/lib/active_hash/condition.rb +++ b/lib/active_hash/condition.rb @@ -19,7 +19,7 @@ def matches?(record) expectation_method = inverted ? :any? : :all? constraints.send(expectation_method) do |attribute, expected| - value = record.read_attribute(attribute) + value = record.read_attribute(attribute.to_sym) matches_value?(value, expected) end From 381335adf9d8878e5fd911d457a78c10f054b7bd Mon Sep 17 00:00:00 2001 From: Stanislav Dobrovolschii Date: Tue, 24 Oct 2023 23:15:17 +0200 Subject: [PATCH 3/3] Move attribute key conversion inside the _read_attribute Signed-off-by: Stanislav Dobrovolschii --- lib/active_hash/base.rb | 2 +- lib/active_hash/condition.rb | 2 +- spec/active_hash/base_spec.rb | 12 ++++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/active_hash/base.rb b/lib/active_hash/base.rb index eda0a20b..0c062219 100644 --- a/lib/active_hash/base.rb +++ b/lib/active_hash/base.rb @@ -411,7 +411,7 @@ def [](key) end def _read_attribute(key) - attributes[key] + attributes[key.to_sym] end alias_method :read_attribute, :_read_attribute diff --git a/lib/active_hash/condition.rb b/lib/active_hash/condition.rb index 40ee00cf..cf78b052 100644 --- a/lib/active_hash/condition.rb +++ b/lib/active_hash/condition.rb @@ -19,7 +19,7 @@ def matches?(record) expectation_method = inverted ? :any? : :all? constraints.send(expectation_method) do |attribute, expected| - value = record.read_attribute(attribute.to_sym) + value = record.read_attribute(attribute) matches_value?(value, expected) end diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index 7df04c06..f105dca4 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -1201,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