From 9341e7acdd3ed203a266ffe2deb995f7b5a151b9 Mon Sep 17 00:00:00 2001 From: iberianpig Date: Fri, 9 Feb 2024 13:56:31 +0900 Subject: [PATCH] test: add spec for pluck method with enum like behavior --- spec/active_hash/base_spec.rb | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/spec/active_hash/base_spec.rb b/spec/active_hash/base_spec.rb index f4a4c34..9cec450 100644 --- a/spec/active_hash/base_spec.rb +++ b/spec/active_hash/base_spec.rb @@ -599,6 +599,30 @@ class Region < ActiveHash::Base it "returns an Array of attribute values" do expect(Country.pluck(:id)).to match_array([1, 2, 3]) end + + context 'with the same field name and method name' do + before do + class CountryWithContinent < ActiveHash::Base + self.data = [ + {:id => 1, :name => "US", continent: 1}, + {:id => 2, :name => "Canada", continent: 1}, + {:id => 3, :name => "Mexico", continent: 1}, + {:id => 4, :name => "Brazil", continent: 2} + ] + + # behave like ActiveRecord Enum + CONTINENTS = { north_america: 1, south_america: 2, europe: 3, asia: 4, africa: 5, oceania: 6 } + + def continent + CONTINENTS.key(self[:continent]).to_sym + end + end + end + + it "returns the value of the method when the field name is the same as the method name" do + expect(CountryWithContinent.pluck(:id, :name, :continent)).to match_array([[1, "US", :north_america], [2, "Canada", :north_america], [3, "Mexico", :north_america], [4, "Brazil", :south_america]]) + end + end end describe '.ids' do @@ -1278,7 +1302,7 @@ class Region < ActiveHash::Base before do Country.field :active, :default => false end - + it "returns the default value when not present" do country = Country.new expect(country.attributes[:active]).to eq(false)