Sha256: cf597db1ad1c576863e5260b4338bac2cec6600462755270e4e4b8388718b01b

Contents?: true

Size: 1.31 KB

Versions: 6

Compression:

Stored size: 1.31 KB

Contents

require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class CatBreed < ClassyEnum::Base
  enum_classes :abyssian, :bengal, :birman, :persian
  owner :cat

  def breed_color
    "#{cat.color} #{name}"
  end
end

class CatBreedAbyssian < CatBreed
end

class CatBreedBengal < CatBreed
end

class CatBreedBirman < CatBreed
end

class CatBreedPersian < CatBreed
end

class Cat < ActiveRecord::Base
  classy_enum_attr :breed, :enum => :cat_breed
  attr_accessor :color
  delegate :breed_color, :to => :breed
end

class OtherCat < ActiveRecord::Base
  classy_enum_attr :breed, :enum => :cat_breed, :serialize_as_json => true
  attr_accessor :color
  delegate :breed_color, :to => :breed
end

describe Cat do
  let(:abyssian) { Cat.new(:breed => :abyssian, :color => 'black') }
  let(:persian) { OtherCat.new(:breed => :persian, :color => 'white') }

  it 'should delegate breed color to breed with an ownership reference' do
    abyssian.breed_color { should eql('black Abyssian') }
    persian.breed_color { should eql('white Persian') }
  end

  it 'should correctly serialize without the owner reference' do
    JSON.parse(abyssian.to_json)['cat']['breed'].should == 'abyssian'
  end

  it 'should convert the enum to a string when serializing' do
    JSON.parse(persian.to_json)['other_cat']['breed'].should be_a(Hash)
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
classy_enum-2.3.0 spec/classy_enum_owner_reference_spec.rb
classy_enum-2.2.0 spec/classy_enum_owner_reference_spec.rb
classy_enum-2.1.0 spec/classy_enum_owner_reference_spec.rb
classy_enum-2.0.3 spec/classy_enum_owner_reference_spec.rb
classy_enum-2.0.2 spec/classy_enum_owner_reference_spec.rb
classy_enum-2.0.1 spec/classy_enum_owner_reference_spec.rb