enumerate_it 1.0.3 → 1.1.0
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/Gemfile.lock +1 -1
- data/README.rdoc +17 -1
- data/lib/enumerate_it/base.rb +23 -2
- data/lib/enumerate_it/version.rb +1 -1
- data/lib/generators/enumerate_it/enum/USAGE +1 -1
- data/spec/enumerate_it/base_spec.rb +200 -0
- data/spec/enumerate_it_spec.rb +0 -216
- data/spec/spec_helper.rb +10 -7
- data/spec/support/test_classes.rb +52 -0
- metadata +8 -4
data/Gemfile.lock
CHANGED
data/README.rdoc
CHANGED
@@ -117,6 +117,22 @@ You can also create enumerations in the following ways:
|
|
117
117
|
associate_values :married => 1, :single => 2
|
118
118
|
end
|
119
119
|
|
120
|
+
=== Defining a default sort mode
|
121
|
+
|
122
|
+
When calling methods like `to_a` and `to_json`, the returned values will be sorted using the translation for each one of the enumeration values. If you want
|
123
|
+
to overwrite the default sort mode, you can use the `sort_mode` class method.
|
124
|
+
|
125
|
+
class RelationshipStatus < EnumerateIt::Base
|
126
|
+
associate_values :married => 1, :single => 2
|
127
|
+
|
128
|
+
sort_by :value
|
129
|
+
end
|
130
|
+
|
131
|
+
The `sort_by` methods accept one of the following values:
|
132
|
+
|
133
|
+
* `:translation`: The default behavior, will sort the returned values based on translations.
|
134
|
+
* `:value`: Will sort the returned values based on values.
|
135
|
+
* `:name`: Will sort the returned values based on the name of each enumeration option.
|
120
136
|
|
121
137
|
== Using enumerations
|
122
138
|
|
@@ -282,7 +298,7 @@ An interesting approach to use it in Rails apps is to create an app/enumerations
|
|
282
298
|
|
283
299
|
There is also a Rails Generator that you can use to generate enumerations and their locale files. Take a look at how to use it running
|
284
300
|
|
285
|
-
rails generate enumerate_it --help
|
301
|
+
rails generate enumerate_it:enum --help
|
286
302
|
|
287
303
|
== Why did you reinvent the wheel?
|
288
304
|
|
data/lib/enumerate_it/base.rb
CHANGED
@@ -3,6 +3,10 @@ module EnumerateIt
|
|
3
3
|
class Base
|
4
4
|
@@registered_enumerations = {}
|
5
5
|
|
6
|
+
class << self
|
7
|
+
attr_reader :sort_mode
|
8
|
+
end
|
9
|
+
|
6
10
|
def self.associate_values(*args)
|
7
11
|
values_hash = args.first.is_a?(Hash) ? args.first : args.inject({}) { |h, v| h[v] = v.to_s; h }
|
8
12
|
|
@@ -10,6 +14,10 @@ module EnumerateIt
|
|
10
14
|
values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
|
11
15
|
end
|
12
16
|
|
17
|
+
def self.sort_by(sort_mode)
|
18
|
+
@sort_mode = sort_mode
|
19
|
+
end
|
20
|
+
|
13
21
|
def self.list
|
14
22
|
enumeration.values.map { |value| value[0] }.sort
|
15
23
|
end
|
@@ -19,7 +27,7 @@ module EnumerateIt
|
|
19
27
|
end
|
20
28
|
|
21
29
|
def self.to_a
|
22
|
-
|
30
|
+
sorted_map.map { |k, v| [translate(v[1]), v[0]] }
|
23
31
|
end
|
24
32
|
|
25
33
|
def self.length
|
@@ -35,7 +43,7 @@ module EnumerateIt
|
|
35
43
|
end
|
36
44
|
|
37
45
|
def self.to_json
|
38
|
-
|
46
|
+
sorted_map.map { |k, v| { :value => v[0], :label => translate(v[1]) } }.to_json
|
39
47
|
end
|
40
48
|
|
41
49
|
def self.t(value)
|
@@ -64,6 +72,19 @@ module EnumerateIt
|
|
64
72
|
end
|
65
73
|
|
66
74
|
private
|
75
|
+
|
76
|
+
def self.sorted_map
|
77
|
+
enumeration.sort_by { |k, v| sort_lambda.call(k, v) }
|
78
|
+
end
|
79
|
+
|
80
|
+
def self.sort_lambda
|
81
|
+
{
|
82
|
+
:value => lambda { |k, v| v[0] },
|
83
|
+
:name => lambda { |k, v| k },
|
84
|
+
:translation => lambda { |k, v| translate(v[1]) }
|
85
|
+
}[sort_mode || :translation]
|
86
|
+
end
|
87
|
+
|
67
88
|
def self.translate(value)
|
68
89
|
return value unless value.is_a? Symbol
|
69
90
|
|
data/lib/enumerate_it/version.rb
CHANGED
@@ -8,7 +8,7 @@ Example:
|
|
8
8
|
app/enumerations/civil_status.rb with `associate_values :single, :married, :divorced, :widower, :concubinage, :separated, :stable`
|
9
9
|
config/locales/civil_status.yml
|
10
10
|
|
11
|
-
rails g enumerate_it CivilStatus single:1 married:2 divorced:3 widower:4
|
11
|
+
rails g enumerate_it:enum CivilStatus single:1 married:2 divorced:3 widower:4
|
12
12
|
|
13
13
|
This will create:
|
14
14
|
app/enumerations/civil_status.rb with `associate_values :single => 1, :married => 2, :divorced => 2, :widower => 4`
|
@@ -0,0 +1,200 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
3
|
+
|
4
|
+
describe EnumerateIt::Base do
|
5
|
+
it "creates constants for each enumeration value" do
|
6
|
+
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
|
7
|
+
constant.should == (idx + 1).to_s
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "creates a method that returns the allowed values in the enumeration's class" do
|
12
|
+
TestEnumeration.list.should == ['1', '2', '3']
|
13
|
+
end
|
14
|
+
|
15
|
+
it "creates a method that returns the enumeration specification" do
|
16
|
+
TestEnumeration.enumeration.should == {
|
17
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
18
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
19
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
describe ".length" do
|
24
|
+
it "returns the length of the enumeration" do
|
25
|
+
TestEnumeration.length.should == 3
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe ".each_translation" do
|
30
|
+
it "yields each enumeration's value translation" do
|
31
|
+
translations = []
|
32
|
+
TestEnumeration.each_translation do |translation|
|
33
|
+
translations << translation
|
34
|
+
end
|
35
|
+
translations.should == ["Hey, I am 1!", "Hey, I am 2!", "Hey, I am 3!"]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe ".each_value" do
|
40
|
+
it "yields each enumeration's value" do
|
41
|
+
values = []
|
42
|
+
TestEnumeration.each_value do |value|
|
43
|
+
values << value
|
44
|
+
end
|
45
|
+
values.should == TestEnumeration.list
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".to_a" do
|
50
|
+
it "returns an array with the values and human representations" do
|
51
|
+
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
52
|
+
end
|
53
|
+
|
54
|
+
it "translates the available values" do
|
55
|
+
TestEnumerationWithoutArray.to_a.should == [['First Value', '1'], ['Value Two', '2']]
|
56
|
+
I18n.locale = :pt
|
57
|
+
TestEnumerationWithoutArray.to_a.should == [['Primeiro Valor', '1'], ['Value Two', '2']]
|
58
|
+
end
|
59
|
+
|
60
|
+
it "can be extended from the enumeration class" do
|
61
|
+
TestEnumerationWithExtendedBehaviour.to_a.should == [['Second', '2'],['First','1']]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe ".to_json" do
|
66
|
+
it "gives a valid json back" do
|
67
|
+
I18n.locale = :inexsistent
|
68
|
+
TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Value One"},{"value":"2","label":"Value Two"}]'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "give translated values when available" do
|
72
|
+
I18n.locale = :pt
|
73
|
+
TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Primeiro Valor"},{"value":"2","label":"Value Two"}]'
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe ".t" do
|
78
|
+
it "translates a given value" do
|
79
|
+
I18n.locale = :pt
|
80
|
+
TestEnumerationWithoutArray.t('1').should == 'Primeiro Valor'
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe ".to_range" do
|
85
|
+
it "returns a Range object containing the enumeration's value interval" do
|
86
|
+
TestEnumeration.to_range.should == ("1".."3")
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe ".values_for" do
|
91
|
+
it "returns an array with the corresponding values for a string array representing some of the enumeration's values" do
|
92
|
+
TestEnumeration.values_for(%w(VALUE_1 VALUE_2)).should == [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe ".value_for" do
|
97
|
+
it "returns the enumeration's value" do
|
98
|
+
TestEnumeration.value_for("VALUE_1").should == TestEnumeration::VALUE_1
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe ".keys" do
|
103
|
+
it "returns a list with the keys used to define the enumeration" do
|
104
|
+
TestEnumeration.keys.should == [:value_1, :value_2, :value_3]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe ".key_for" do
|
109
|
+
it "returns the key for the given value inside the enumeration" do
|
110
|
+
TestEnumeration.key_for(TestEnumeration::VALUE_1).should == :value_1
|
111
|
+
end
|
112
|
+
|
113
|
+
it "returns nil if the enumeration does not have the given value" do
|
114
|
+
TestEnumeration.key_for("foo").should be_nil
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'associate values with a list' do
|
119
|
+
it "creates constants for each enumeration value" do
|
120
|
+
TestEnumerationWithList::FIRST.should == "first"
|
121
|
+
TestEnumerationWithList::SECOND.should == "second"
|
122
|
+
end
|
123
|
+
|
124
|
+
it "returns an array with the values and human representations" do
|
125
|
+
TestEnumerationWithList.to_a.should == [['First', 'first'], ['Second', 'second']]
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "specifying a default sort mode" do
|
130
|
+
subject { create_enumeration_class_with_sort_mode(sort_mode).to_a }
|
131
|
+
|
132
|
+
context "by value" do
|
133
|
+
let(:sort_mode) { :value }
|
134
|
+
|
135
|
+
it { should == [["xyz", "1"], ["fgh", "2"], ["abc", "3"]] }
|
136
|
+
end
|
137
|
+
|
138
|
+
context "by name" do
|
139
|
+
let(:sort_mode) { :name }
|
140
|
+
|
141
|
+
it { should == [["fgh", "2"], ["xyz", "1"], ["abc", "3"]] }
|
142
|
+
end
|
143
|
+
|
144
|
+
context "by translation" do
|
145
|
+
let(:sort_mode) { :translation }
|
146
|
+
|
147
|
+
it { should == [["abc", "3"] ,["fgh", "2"], ["xyz", "1"]] }
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
context "when included in ActiveRecord::Base" do
|
152
|
+
before :each do
|
153
|
+
class ActiveRecordStub
|
154
|
+
attr_accessor :bla
|
155
|
+
|
156
|
+
class << self
|
157
|
+
def validates_inclusion_of(options); true; end
|
158
|
+
def validates_presence_of; true; end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
163
|
+
ActiveRecordStub.extend EnumerateIt
|
164
|
+
end
|
165
|
+
|
166
|
+
it "creates a validation for inclusion" do
|
167
|
+
ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
|
168
|
+
class ActiveRecordStub
|
169
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
context "using the :required option" do
|
174
|
+
before :each do
|
175
|
+
ActiveRecordStub.stub!(:validates_presence_of).and_return(true)
|
176
|
+
end
|
177
|
+
|
178
|
+
it "creates a validation for presence" do
|
179
|
+
ActiveRecordStub.should_receive(:validates_presence_of)
|
180
|
+
class ActiveRecordStub
|
181
|
+
has_enumeration_for :bla, :with => TestEnumeration, :required => true
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
it "passes the given options to the validation method" do
|
186
|
+
ActiveRecordStub.should_receive(:validates_presence_of).with(:bla, :if => :some_method)
|
187
|
+
class ActiveRecordStub
|
188
|
+
has_enumeration_for :bla, :with => TestEnumeration, :required => { :if => :some_method }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
it "do not require the attribute by default" do
|
193
|
+
ActiveRecordStub.should_not_receive(:validates_presence_of)
|
194
|
+
class ActiveRecordStub
|
195
|
+
has_enumeration_for :bla, :with => TestEnumeration
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
data/spec/enumerate_it_spec.rb
CHANGED
@@ -1,46 +1,5 @@
|
|
1
1
|
#encoding: utf-8
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
-
require 'active_record'
|
4
|
-
|
5
|
-
class TestEnumeration < EnumerateIt::Base
|
6
|
-
associate_values(
|
7
|
-
:value_1 => ['1', 'Hey, I am 1!'],
|
8
|
-
:value_2 => ['2', 'Hey, I am 2!'],
|
9
|
-
:value_3 => ['3', 'Hey, I am 3!']
|
10
|
-
)
|
11
|
-
end
|
12
|
-
|
13
|
-
class TestEnumerationWithoutArray < EnumerateIt::Base
|
14
|
-
associate_values(
|
15
|
-
:value_one => '1',
|
16
|
-
:value_two => '2'
|
17
|
-
)
|
18
|
-
end
|
19
|
-
|
20
|
-
class TestEnumerationWithExtendedBehaviour < EnumerateIt::Base
|
21
|
-
associate_values(
|
22
|
-
:first => '1',
|
23
|
-
:second => '2'
|
24
|
-
)
|
25
|
-
def self.to_a
|
26
|
-
super.reverse
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
class TestEnumerationWithList < EnumerateIt::Base
|
31
|
-
associate_values :first, :second
|
32
|
-
end
|
33
|
-
|
34
|
-
class Foobar < EnumerateIt::Base
|
35
|
-
associate_values(
|
36
|
-
:bar => 'foo'
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
class BaseClass
|
41
|
-
extend EnumerateIt
|
42
|
-
has_enumeration_for :foobar, :with => TestEnumeration
|
43
|
-
end
|
44
3
|
|
45
4
|
describe EnumerateIt do
|
46
5
|
before :each do
|
@@ -266,178 +225,3 @@ describe EnumerateIt do
|
|
266
225
|
end
|
267
226
|
end
|
268
227
|
|
269
|
-
describe EnumerateIt::Base do
|
270
|
-
it "creates constants for each enumeration value" do
|
271
|
-
[TestEnumeration::VALUE_1, TestEnumeration::VALUE_2, TestEnumeration::VALUE_3].each_with_index do |constant, idx|
|
272
|
-
constant.should == (idx + 1).to_s
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
it "creates a method that returns the allowed values in the enumeration's class" do
|
277
|
-
TestEnumeration.list.should == ['1', '2', '3']
|
278
|
-
end
|
279
|
-
|
280
|
-
it "creates a method that returns the enumeration specification" do
|
281
|
-
TestEnumeration.enumeration.should == {
|
282
|
-
:value_1 => ['1', 'Hey, I am 1!'],
|
283
|
-
:value_2 => ['2', 'Hey, I am 2!'],
|
284
|
-
:value_3 => ['3', 'Hey, I am 3!']
|
285
|
-
}
|
286
|
-
end
|
287
|
-
|
288
|
-
describe ".length" do
|
289
|
-
it "returns the length of the enumeration" do
|
290
|
-
TestEnumeration.length.should == 3
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
describe ".each_translation" do
|
295
|
-
it "yields each enumeration's value translation" do
|
296
|
-
translations = []
|
297
|
-
TestEnumeration.each_translation do |translation|
|
298
|
-
translations << translation
|
299
|
-
end
|
300
|
-
translations.should == ["Hey, I am 1!", "Hey, I am 2!", "Hey, I am 3!"]
|
301
|
-
end
|
302
|
-
end
|
303
|
-
|
304
|
-
describe ".each_value" do
|
305
|
-
it "yields each enumeration's value" do
|
306
|
-
values = []
|
307
|
-
TestEnumeration.each_value do |value|
|
308
|
-
values << value
|
309
|
-
end
|
310
|
-
values.should == TestEnumeration.list
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
describe ".to_a" do
|
315
|
-
it "returns an array with the values and human representations" do
|
316
|
-
TestEnumeration.to_a.should == [['Hey, I am 1!', '1'], ['Hey, I am 2!', '2'], ['Hey, I am 3!', '3']]
|
317
|
-
end
|
318
|
-
|
319
|
-
it "translates the available values" do
|
320
|
-
TestEnumerationWithoutArray.to_a.should == [['First Value', '1'], ['Value Two', '2']]
|
321
|
-
I18n.locale = :pt
|
322
|
-
TestEnumerationWithoutArray.to_a.should == [['Primeiro Valor', '1'], ['Value Two', '2']]
|
323
|
-
end
|
324
|
-
|
325
|
-
it "can be extended from the enumeration class" do
|
326
|
-
TestEnumerationWithExtendedBehaviour.to_a.should == [['Second', '2'],['First','1']]
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
describe ".to_json" do
|
331
|
-
it "gives a valid json back" do
|
332
|
-
I18n.locale = :inexsistent
|
333
|
-
TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Value One"},{"value":"2","label":"Value Two"}]'
|
334
|
-
end
|
335
|
-
|
336
|
-
it "give translated values when available" do
|
337
|
-
I18n.locale = :pt
|
338
|
-
TestEnumerationWithoutArray.to_json.should == '[{"value":"1","label":"Primeiro Valor"},{"value":"2","label":"Value Two"}]'
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
describe ".t" do
|
343
|
-
it "translates a given value" do
|
344
|
-
I18n.locale = :pt
|
345
|
-
TestEnumerationWithoutArray.t('1').should == 'Primeiro Valor'
|
346
|
-
end
|
347
|
-
end
|
348
|
-
|
349
|
-
describe ".to_range" do
|
350
|
-
it "returns a Range object containing the enumeration's value interval" do
|
351
|
-
TestEnumeration.to_range.should == ("1".."3")
|
352
|
-
end
|
353
|
-
end
|
354
|
-
|
355
|
-
describe ".values_for" do
|
356
|
-
it "returns an array with the corresponding values for a string array representing some of the enumeration's values" do
|
357
|
-
TestEnumeration.values_for(%w(VALUE_1 VALUE_2)).should == [TestEnumeration::VALUE_1, TestEnumeration::VALUE_2]
|
358
|
-
end
|
359
|
-
end
|
360
|
-
|
361
|
-
describe ".value_for" do
|
362
|
-
it "returns the enumeration's value" do
|
363
|
-
TestEnumeration.value_for("VALUE_1").should == TestEnumeration::VALUE_1
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
describe ".keys" do
|
368
|
-
it "returns a list with the keys used to define the enumeration" do
|
369
|
-
TestEnumeration.keys.should == [:value_1, :value_2, :value_3]
|
370
|
-
end
|
371
|
-
end
|
372
|
-
|
373
|
-
describe ".key_for" do
|
374
|
-
it "returns the key for the given value inside the enumeration" do
|
375
|
-
TestEnumeration.key_for(TestEnumeration::VALUE_1).should == :value_1
|
376
|
-
end
|
377
|
-
|
378
|
-
it "returns nil if the enumeration does not have the given value" do
|
379
|
-
TestEnumeration.key_for("foo").should be_nil
|
380
|
-
end
|
381
|
-
end
|
382
|
-
|
383
|
-
context 'associate values with a list' do
|
384
|
-
it "creates constants for each enumeration value" do
|
385
|
-
TestEnumerationWithList::FIRST.should == "first"
|
386
|
-
TestEnumerationWithList::SECOND.should == "second"
|
387
|
-
end
|
388
|
-
|
389
|
-
it "returns an array with the values and human representations" do
|
390
|
-
TestEnumerationWithList.to_a.should == [['First', 'first'], ['Second', 'second']]
|
391
|
-
end
|
392
|
-
end
|
393
|
-
|
394
|
-
context "when included in ActiveRecord::Base" do
|
395
|
-
before :each do
|
396
|
-
class ActiveRecordStub
|
397
|
-
attr_accessor :bla
|
398
|
-
|
399
|
-
class << self
|
400
|
-
def validates_inclusion_of(options); true; end
|
401
|
-
def validates_presence_of; true; end
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
ActiveRecordStub.stub!(:validates_inclusion_of).and_return(true)
|
406
|
-
ActiveRecordStub.extend EnumerateIt
|
407
|
-
end
|
408
|
-
|
409
|
-
it "creates a validation for inclusion" do
|
410
|
-
ActiveRecordStub.should_receive(:validates_inclusion_of).with(:bla, :in => TestEnumeration.list, :allow_blank => true)
|
411
|
-
class ActiveRecordStub
|
412
|
-
has_enumeration_for :bla, :with => TestEnumeration
|
413
|
-
end
|
414
|
-
end
|
415
|
-
|
416
|
-
context "using the :required option" do
|
417
|
-
before :each do
|
418
|
-
ActiveRecordStub.stub!(:validates_presence_of).and_return(true)
|
419
|
-
end
|
420
|
-
|
421
|
-
it "creates a validation for presence" do
|
422
|
-
ActiveRecordStub.should_receive(:validates_presence_of)
|
423
|
-
class ActiveRecordStub
|
424
|
-
has_enumeration_for :bla, :with => TestEnumeration, :required => true
|
425
|
-
end
|
426
|
-
end
|
427
|
-
|
428
|
-
it "passes the given options to the validation method" do
|
429
|
-
ActiveRecordStub.should_receive(:validates_presence_of).with(:bla, :if => :some_method)
|
430
|
-
class ActiveRecordStub
|
431
|
-
has_enumeration_for :bla, :with => TestEnumeration, :required => { :if => :some_method }
|
432
|
-
end
|
433
|
-
end
|
434
|
-
|
435
|
-
it "do not require the attribute by default" do
|
436
|
-
ActiveRecordStub.should_not_receive(:validates_presence_of)
|
437
|
-
class ActiveRecordStub
|
438
|
-
has_enumeration_for :bla, :with => TestEnumeration
|
439
|
-
end
|
440
|
-
end
|
441
|
-
end
|
442
|
-
end
|
443
|
-
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,15 @@
|
|
1
1
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__),
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
|
3
|
+
require "enumerate_it"
|
4
|
+
require "rspec"
|
5
|
+
require "rspec/autorun"
|
6
6
|
|
7
|
-
require
|
7
|
+
require "rubygems"
|
8
8
|
require "active_support"
|
9
|
+
require "active_record"
|
9
10
|
require "active_support/core_ext/string/inflections"
|
10
|
-
require
|
11
|
+
require "active_support/core_ext/object/to_json"
|
11
12
|
|
12
|
-
|
13
|
+
Dir["./spec/support/**/*.rb"].each { |f| require f }
|
14
|
+
|
15
|
+
I18n.load_path = Dir["spec/i18n/*.yml"]
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class TestEnumeration < EnumerateIt::Base
|
2
|
+
associate_values(
|
3
|
+
:value_1 => ['1', 'Hey, I am 1!'],
|
4
|
+
:value_2 => ['2', 'Hey, I am 2!'],
|
5
|
+
:value_3 => ['3', 'Hey, I am 3!']
|
6
|
+
)
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestEnumerationWithoutArray < EnumerateIt::Base
|
10
|
+
associate_values(
|
11
|
+
:value_one => '1',
|
12
|
+
:value_two => '2'
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
class TestEnumerationWithExtendedBehaviour < EnumerateIt::Base
|
17
|
+
associate_values(
|
18
|
+
:first => '1',
|
19
|
+
:second => '2'
|
20
|
+
)
|
21
|
+
def self.to_a
|
22
|
+
super.reverse
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class TestEnumerationWithList < EnumerateIt::Base
|
27
|
+
associate_values :first, :second
|
28
|
+
end
|
29
|
+
|
30
|
+
class Foobar < EnumerateIt::Base
|
31
|
+
associate_values(
|
32
|
+
:bar => 'foo'
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
class BaseClass
|
37
|
+
extend EnumerateIt
|
38
|
+
has_enumeration_for :foobar, :with => TestEnumeration
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_enumeration_class_with_sort_mode(sort_mode)
|
42
|
+
Class.new(EnumerateIt::Base) do
|
43
|
+
sort_by sort_mode
|
44
|
+
|
45
|
+
associate_values(
|
46
|
+
:foo => ["1", "xyz"],
|
47
|
+
:bar => ["2", "fgh"],
|
48
|
+
:zomg => ["3", "abc"]
|
49
|
+
)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: enumerate_it
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-07-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -137,11 +137,13 @@ files:
|
|
137
137
|
- lib/generators/enumerate_it/install/USAGE
|
138
138
|
- lib/generators/enumerate_it/install/install_generator.rb
|
139
139
|
- lib/generators/enumerate_it/install/templates/enumerate_it_initializer.rb
|
140
|
+
- spec/enumerate_it/base_spec.rb
|
140
141
|
- spec/enumerate_it_spec.rb
|
141
142
|
- spec/i18n/en.yml
|
142
143
|
- spec/i18n/pt.yml
|
143
144
|
- spec/spec.opts
|
144
145
|
- spec/spec_helper.rb
|
146
|
+
- spec/support/test_classes.rb
|
145
147
|
homepage: http://github.com/cassiomarques/enumerate_it
|
146
148
|
licenses: []
|
147
149
|
post_install_message:
|
@@ -156,7 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
156
158
|
version: '0'
|
157
159
|
segments:
|
158
160
|
- 0
|
159
|
-
hash:
|
161
|
+
hash: 1268518782242094131
|
160
162
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
161
163
|
none: false
|
162
164
|
requirements:
|
@@ -165,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
165
167
|
version: '0'
|
166
168
|
segments:
|
167
169
|
- 0
|
168
|
-
hash:
|
170
|
+
hash: 1268518782242094131
|
169
171
|
requirements: []
|
170
172
|
rubyforge_project:
|
171
173
|
rubygems_version: 1.8.24
|
@@ -173,8 +175,10 @@ signing_key:
|
|
173
175
|
specification_version: 3
|
174
176
|
summary: Ruby Enumerations
|
175
177
|
test_files:
|
178
|
+
- spec/enumerate_it/base_spec.rb
|
176
179
|
- spec/enumerate_it_spec.rb
|
177
180
|
- spec/i18n/en.yml
|
178
181
|
- spec/i18n/pt.yml
|
179
182
|
- spec/spec.opts
|
180
183
|
- spec/spec_helper.rb
|
184
|
+
- spec/support/test_classes.rb
|