/
active_record_spec.rb
158 lines (126 loc) · 4.49 KB
/
active_record_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
class Breed < ClassyEnum::Base; end
class Breed::GoldenRetriever < Breed; end
class Breed::Snoop < Breed; end
class Breed::Husky < Breed; end
class Color < ClassyEnum::Base; end
class Color::White < Color; end;
class Color::Black < Color; end;
class CatBreed < ClassyEnum::Base
owner :cat
def breed_color
"#{cat.color} #{self}"
end
end
class CatBreed::Abyssian < CatBreed; end
class CatBreed::Bengal < CatBreed; end
class CatBreed::Birman < CatBreed; end
class CatBreed::Persian < CatBreed; end
class Dog < ActiveRecord::Base
classy_enum_attr :breed
end
class AllowBlankBreedDog < ActiveRecord::Base
classy_enum_attr :breed, :allow_blank => true
end
class AllowNilBreedDog < ActiveRecord::Base
classy_enum_attr :breed, :allow_nil => true
end
class OtherDog < ActiveRecord::Base
classy_enum_attr :other_breed, :enum => 'Breed'
end
describe Dog do
specify { Dog.new(:breed => nil).should_not be_valid }
specify { Dog.new(:breed => '').should_not be_valid }
context "with valid breed options" do
[:golden_retriever, 'golden_retriever', Breed::GoldenRetriever.new].each do |option|
subject { Dog.new(:breed => option) }
it { should be_valid }
its(:breed) { should be_a(Breed::GoldenRetriever) }
its('breed.allow_blank') { should be_false }
end
end
context "with invalid breed options" do
subject { Dog.new(:breed => :fake_breed) }
it { should_not be_valid }
it { should have(1).error_on(:breed) }
end
end
describe "A ClassyEnum that allows blanks" do
specify { AllowBlankBreedDog.new(:breed => nil).should be_valid }
specify { AllowBlankBreedDog.new(:breed => '').should be_valid }
context "with valid breed options" do
subject { AllowBlankBreedDog.new(:breed => :golden_retriever) }
it { should be_valid }
its('breed.allow_blank') { should be_true }
end
context "with invalid breed options" do
subject { AllowBlankBreedDog.new(:breed => :fake_breed) }
it { should_not be_valid }
it { should have(1).error_on(:breed) }
end
end
describe "A ClassyEnum that allows nils" do
specify { AllowNilBreedDog.new(:breed => nil).should be_valid }
specify { AllowNilBreedDog.new(:breed => '').should_not be_valid }
context "with valid breed options" do
subject { AllowNilBreedDog.new(:breed => :golden_retriever) }
it { should be_valid }
its('breed.allow_blank') { should be_true }
end
context "with invalid breed options" do
subject { AllowNilBreedDog.new(:breed => :fake_breed) }
it { should_not be_valid }
it { should have(1).error_on(:breed) }
end
end
describe "A ClassyEnum that has a different field name than the enum" do
subject { OtherDog.new(:other_breed => :snoop) }
its(:other_breed) { should be_a(Breed::Snoop) }
end
class ActiveDog < ActiveRecord::Base
classy_enum_attr :color
validates_uniqueness_of :name, :scope => :color
scope :goldens, where(:breed => 'golden_retriever')
end
describe ActiveDog do
context 'uniqueness on name' do
subject { ActiveDog.new(:name => 'Kitteh', :breed => :golden_retriever, :color => :black) }
it { should be_valid }
context 'with existing kitteh' do
before do
ActiveDog.create!(:name => 'Kitteh', :breed => :husky, :color => :black)
end
it { should have(1).error_on(:name) }
end
end
context 'scopes' do
let!(:golden) { ActiveDog.create!(:name => 'Sebastian', :breed => :golden_retriever, :color => :white) }
let!(:husky) { ActiveDog.create!(:name => 'Sirius', :breed => :husky, :color => :black) }
after { ActiveDog.destroy_all }
it 'should know all dogs' do
ActiveDog.all.should include(golden, husky)
end
it 'should have a working scope' do
ActiveDog.goldens.should include(golden)
ActiveDog.goldens.should_not include(husky)
end
end
end
class Cat < ActiveRecord::Base
classy_enum_attr :breed, :enum => 'CatBreed'
attr_accessor :color
delegate :breed_color, :to => :breed
end
class OtherCat < ActiveRecord::Base
classy_enum_attr :breed, :enum => 'CatBreed', :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
end