Skip to content

Commit

Permalink
test: add spec for pluck method with enum like behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
iberianpig committed Feb 9, 2024
1 parent 28a3ea7 commit 9341e7a
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion spec/active_hash/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9341e7a

Please sign in to comment.