enumerate_it 0.7.5 → 0.7.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +0 -10
- data/lib/enumerate_it.rb +12 -6
- data/lib/version.rb +1 -1
- data/spec/enumerate_it_spec.rb +89 -103
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -145,16 +145,6 @@ This will create:
|
|
145
145
|
|
146
146
|
NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
|
147
147
|
|
148
|
-
* If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
|
149
|
-
|
150
|
-
class Person < ActiveRecord::Base
|
151
|
-
has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_scopes => true
|
152
|
-
end
|
153
|
-
|
154
|
-
Person.married.to_sql # => SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
|
155
|
-
|
156
|
-
NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
|
157
|
-
|
158
148
|
* If your class can manage validations and responds to :validates_inclusion_of, it will create this validation:
|
159
149
|
|
160
150
|
class Person < ActiveRecord::Base
|
data/lib/enumerate_it.rb
CHANGED
@@ -126,6 +126,16 @@
|
|
126
126
|
# p.married? #=> true
|
127
127
|
# p.divorced? #=> false
|
128
128
|
#
|
129
|
+
# - If you pass the :create_scopes option as 'true', it will create a scope method for each enumeration option (this option defaults to false):
|
130
|
+
#
|
131
|
+
# class Person < ActiveRecord::Base
|
132
|
+
# has_enumeration_for :relationship_status, :with => RelationshipStatus, :create_scopes => true
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# Person.married.to_sql # => SELECT "people".* FROM "people" WHERE "people"."relationship_status" = 1
|
136
|
+
#
|
137
|
+
# NOTE: The :create_scopes option can only be used for Rails.version >= 3.0.0.
|
138
|
+
#
|
129
139
|
# - If your class can manage validations and responds to :validates_inclusion_of, it will create this
|
130
140
|
# validation:
|
131
141
|
#
|
@@ -275,12 +285,8 @@ module EnumerateIt
|
|
275
285
|
|
276
286
|
def create_scopes(klass, attribute_name)
|
277
287
|
klass.enumeration.keys.each do |option|
|
278
|
-
if
|
279
|
-
|
280
|
-
scope option, where(attribute_name => klass.enumeration[option].first)
|
281
|
-
else
|
282
|
-
raise StandardError, "EnumerateIt cannot create scopes if Rails.version < 3.0.0"
|
283
|
-
end
|
288
|
+
if respond_to? :scope
|
289
|
+
scope option, where(attribute_name => klass.enumeration[option].first)
|
284
290
|
end
|
285
291
|
end
|
286
292
|
end
|
data/lib/version.rb
CHANGED
data/spec/enumerate_it_spec.rb
CHANGED
@@ -135,146 +135,132 @@ describe EnumerateIt do
|
|
135
135
|
TestClass.send(:has_enumeration_for, :foobar, :with => TestEnumeration, :create_scopes => true)
|
136
136
|
end
|
137
137
|
|
138
|
-
context "
|
139
|
-
before
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
module Rails
|
144
|
-
def self.version; "3.0.0"; end
|
145
|
-
end
|
146
|
-
|
147
|
-
class TestClass
|
148
|
-
def self.where(whatever); end
|
149
|
-
def self.scope(name, whatever); end
|
150
|
-
end
|
151
|
-
|
152
|
-
setup_enumeration
|
138
|
+
context "if the hosting class responds to :scope" do
|
139
|
+
before do
|
140
|
+
class TestClass
|
141
|
+
def self.where(whatever); end
|
142
|
+
def self.scope(name, whatever); end
|
153
143
|
end
|
154
144
|
|
155
|
-
|
156
|
-
|
157
|
-
TestClass.should respond_to(symbol)
|
158
|
-
end
|
159
|
-
end
|
145
|
+
setup_enumeration
|
146
|
+
end
|
160
147
|
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
TestClass.send symbol
|
165
|
-
end
|
148
|
+
it "creates a scope for each enumeration value" do
|
149
|
+
TestEnumeration.enumeration do |symbol, pair|
|
150
|
+
TestClass.should respond_to(symbol)
|
166
151
|
end
|
167
152
|
end
|
168
153
|
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
end
|
154
|
+
it "when called, the scopes create the correct query" do
|
155
|
+
TestEnumeration.enumeration do |symbol, pair|
|
156
|
+
TestClass.should_receive(:where).with(:foobar => pair.firs)
|
157
|
+
TestClass.send symbol
|
174
158
|
end
|
159
|
+
end
|
160
|
+
end
|
175
161
|
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
162
|
+
context "when the hosting class do not respond to :scope" do
|
163
|
+
it "raises no errors" do
|
164
|
+
expect {
|
165
|
+
setup_enumeration
|
166
|
+
}.to_not raise_error
|
181
167
|
end
|
182
168
|
end
|
183
169
|
end
|
170
|
+
end
|
184
171
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
end
|
172
|
+
describe EnumerateIt::Base do
|
173
|
+
it "creates constants for each enumeration value" do
|
174
|
+
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
|
175
|
+
constant.should == (idx + 1).to_s
|
190
176
|
end
|
177
|
+
end
|
191
178
|
|
192
|
-
|
193
|
-
|
179
|
+
it "creates a method that returns the allowed values in the enumeration's class" do
|
180
|
+
TestEnumeration.list.should == ['1', '2', '3']
|
181
|
+
end
|
182
|
+
|
183
|
+
it "creates a method that returns the enumeration specification" do
|
184
|
+
TestEnumeration.enumeration.should == {
|
185
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
186
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
187
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
188
|
+
}
|
189
|
+
end
|
190
|
+
|
191
|
+
describe ".to_a" do
|
192
|
+
it "returns an array with the values and human representations" do
|
193
|
+
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
194
194
|
end
|
195
195
|
|
196
|
-
it "
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
:value_3 => ['3', 'Hey, I am 3!']
|
201
|
-
}
|
196
|
+
it "translates the available values" do
|
197
|
+
TestEnumerationWithoutArray.to_a.should == [['First Value', '1'], ['Value Two', '2']]
|
198
|
+
I18n.locale = :pt
|
199
|
+
TestEnumerationWithoutArray.to_a.should == [['Primeiro Valor', '1'], ['Value Two', '2']]
|
202
200
|
end
|
203
201
|
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
202
|
+
it "can be extended from the enumeration class" do
|
203
|
+
TestEnumerationWithExtendedBehaviour.to_a.should == [['Second', '2'],['First','1']]
|
204
|
+
end
|
205
|
+
end
|
208
206
|
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
207
|
+
describe ".t" do
|
208
|
+
it "translates a given value" do
|
209
|
+
I18n.locale = :pt
|
210
|
+
TestEnumerationWithoutArray.t('1').should == 'Primeiro Valor'
|
211
|
+
end
|
212
|
+
end
|
214
213
|
|
215
|
-
|
216
|
-
|
217
|
-
|
214
|
+
describe "#to_range" do
|
215
|
+
it "returns a Range object containing the enumeration's value interval" do
|
216
|
+
TestEnumeration.to_range.should == ("1".."3")
|
218
217
|
end
|
218
|
+
end
|
219
219
|
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
TestEnumerationWithoutArray.t('1').should == 'Primeiro Valor'
|
224
|
-
end
|
220
|
+
describe ".values_for" do
|
221
|
+
it "returns an array with the corresponding values for a string array representing some of the enumeration's values" do
|
222
|
+
TestEnumeration.values_for(%w(VALUE_1 VALUE_2)).should == [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2]
|
225
223
|
end
|
224
|
+
end
|
226
225
|
|
227
|
-
|
228
|
-
|
229
|
-
|
226
|
+
context "when included in ActiveRecord::Base" do
|
227
|
+
before :each do
|
228
|
+
class ActiveRecordStub
|
229
|
+
attr_accessor :bla
|
230
|
+
|
231
|
+
class << self
|
232
|
+
def validates_inclusion_of(options); true; end
|
233
|
+
def validates_presence_of; true; end
|
234
|
+
end
|
230
235
|
end
|
236
|
+
|
237
|
+
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
238
|
+
ActiveRecordStub.send :include, EnumerateIt
|
231
239
|
end
|
232
240
|
|
233
|
-
|
234
|
-
|
235
|
-
|
241
|
+
it "creates a validation for inclusion" do
|
242
|
+
ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
|
243
|
+
class ActiveRecordStub
|
244
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
236
245
|
end
|
237
246
|
end
|
238
247
|
|
239
|
-
context "
|
248
|
+
context "using the :required option" do
|
240
249
|
before :each do
|
241
|
-
|
242
|
-
attr_accessor :bla
|
243
|
-
|
244
|
-
class << self
|
245
|
-
def validates_inclusion_of(options); true; end
|
246
|
-
def validates_presence_of; true; end
|
247
|
-
end
|
248
|
-
end
|
249
|
-
|
250
|
-
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
251
|
-
ActiveRecordStub.send :include, EnumerateIt
|
250
|
+
ActiveRecordStub.stub!(:validates_presence_of).and_return(true)
|
252
251
|
end
|
253
252
|
|
254
|
-
it "creates a validation for
|
255
|
-
ActiveRecordStub.should_receive(:
|
253
|
+
it "creates a validation for presence" do
|
254
|
+
ActiveRecordStub.should_receive(:validates_presence_of)
|
256
255
|
class ActiveRecordStub
|
257
|
-
has_enumeration_for :bla, :with => TestEnumeration
|
256
|
+
has_enumeration_for :bla, :with => TestEnumeration, :required => true
|
258
257
|
end
|
259
258
|
end
|
260
259
|
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
it "creates a validation for presence" do
|
267
|
-
ActiveRecordStub.should_receive(:validates_presence_of)
|
268
|
-
class ActiveRecordStub
|
269
|
-
has_enumeration_for :bla, :with => TestEnumeration, :required => true
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
it "do not require the attribute by default" do
|
274
|
-
ActiveRecordStub.should_not_receive(:validates_presence_of)
|
275
|
-
class ActiveRecordStub
|
276
|
-
has_enumeration_for :bla, :with => TestEnumeration
|
277
|
-
end
|
260
|
+
it "do not require the attribute by default" do
|
261
|
+
ActiveRecordStub.should_not_receive(:validates_presence_of)
|
262
|
+
class ActiveRecordStub
|
263
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
278
264
|
end
|
279
265
|
end
|
280
266
|
end
|