enumerate_it 0.7.5 → 0.7.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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 defined?(Rails)
279
- if Rails.version >= "3.0.0"
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
@@ -1,4 +1,4 @@
1
1
  module EnumerateIt
2
- VERSION = "0.7.5"
2
+ VERSION = "0.7.6"
3
3
  end
4
4
 
@@ -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 "when using Rails" do
139
- before { module Rails; end }
140
-
141
- context "with version >= 3.0.0" do
142
- before do
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
- it "creates a scope for each enumeration value" do
156
- TestEnumeration.enumeration do |symbol, pair|
157
- TestClass.should respond_to(symbol)
158
- end
159
- end
145
+ setup_enumeration
146
+ end
160
147
 
161
- it "when called, the scopes create the correct query" do
162
- TestEnumeration.enumeration do |symbol, pair|
163
- TestClass.should_receive(:where).with(:foobar => pair.firs)
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
- context "with version < 3.0.0" do
170
- before :each do
171
- module Rails
172
- def self.version; "2.3.9"; end
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
- it "raises an error telling that scopes creation are ony supported for Rails >= 3.0.0" do
177
- expect {
178
- setup_enumeration
179
- }.to raise_error("EnumerateIt cannot create scopes if Rails.version < 3.0.0")
180
- end
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
- describe EnumerateIt::Base do
186
- it "creates constants for each enumeration value" do
187
- [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
188
- constant.should == (idx + 1).to_s
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
- it "creates a method that returns the allowed values in the enumeration's class" do
193
- TestEnumeration.list.should == ['1', '2', '3']
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 "creates a method that returns the enumeration specification" do
197
- TestEnumeration.enumeration.should == {
198
- :value_1 => ['1', 'Hey, I am 1!'],
199
- :value_2 => ['2', 'Hey, I am 2!'],
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
- describe ".to_a" do
205
- it "returns an array with the values and human representations" do
206
- TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
207
- end
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
- it "translates the available values" do
210
- TestEnumerationWithoutArray.to_a.should == [['First Value', '1'], ['Value Two', '2']]
211
- I18n.locale = :pt
212
- TestEnumerationWithoutArray.to_a.should == [['Primeiro Valor', '1'], ['Value Two', '2']]
213
- end
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
- it "can be extended from the enumeration class" do
216
- TestEnumerationWithExtendedBehaviour.to_a.should == [['Second', '2'],['First','1']]
217
- end
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
- describe ".t" do
221
- it "translates a given value" do
222
- I18n.locale = :pt
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
- describe "#to_range" do
228
- it "returns a Range object containing the enumeration's value interval" do
229
- TestEnumeration.to_range.should == ("1".."3")
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
- describe ".values_for" do
234
- it "returns an array with the corresponding values for a string array representing some of the enumeration's values" do
235
- TestEnumeration.values_for(%w(VALUE_1 VALUE_2)).should == [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2]
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 "when included in ActiveRecord::Base" do
248
+ context "using the :required option" do
240
249
  before :each do
241
- class ActiveRecordStub
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 inclusion" do
255
- ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
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
- context "using the :required option" do
262
- before :each do
263
- ActiveRecordStub.stub!(:validates_presence_of).and_return(true)
264
- end
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
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: enumerate_it
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.7.5
5
+ version: 0.7.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - "C\xC3\xA1ssio Marques"