active_enum 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e3ba1613a53b9f8ba7fcf13ff5b109f8c5e6ed02916fda74124dc9fdbac5055
4
- data.tar.gz: 1da9d43ebd9a40564e7e7c46624d85f15216fe9906cf1ac85272cdc6454f1218
3
+ metadata.gz: 96e647ed19a3af115aa962e61aaecb6b254d51064a60c86a14a4735b32d264d5
4
+ data.tar.gz: c4d6f20b518d1b8171780048624f36f657f276cd996b78f6fec7ef4d99f7dcdc
5
5
  SHA512:
6
- metadata.gz: d88b6864c8b46a21f02823989e90156f7a3aa631d45f5cebee5dd9aa448b6f5370a1614263fe144b8b497bbdae667a692421fb6583496fd04ed8fa75622b9a85
7
- data.tar.gz: '09e15b242df5668932babded2d1a675831071474656b8368ddd6456638be509169fafb7136700d6a168fce4e690a6b9b12d730f665f98ce34af3bf9fa02aa934'
6
+ metadata.gz: af70d47df41c0ad90a99ca11691609e8812034acbaac5b389ec07542c0e8eed326657351de923eeeb60e9a15a60adadb726186f114a511952e17afd71bc0433b
7
+ data.tar.gz: 76d5265b1205a28435c857a2b108bfb4f36d1cfd2bd3bf1dbe5a99594e0b10b9828f2a7a7bdfb1d4d2e834372fc122912e01939ca22fedd60500692cffc2c929
@@ -4,8 +4,6 @@ module ActiveEnum
4
4
  class NotFound < StandardError; end
5
5
 
6
6
  class Base
7
- DEFAULT_GROUPED_SELECT_TRANSFORM = proc { |group| group&.html_safe }
8
-
9
7
  class << self
10
8
  attr_accessor :store
11
9
 
@@ -24,7 +22,7 @@ module ActiveEnum
24
22
  store.set(*id_and_name_and_meta(enum_value))
25
23
  end
26
24
 
27
- # Specify order enum values are returned.
25
+ # Specify order enum values are returned.
28
26
  # Allowed values are :asc, :desc or :natural
29
27
  #
30
28
  def order(order)
@@ -56,12 +54,12 @@ module ActiveEnum
56
54
  end
57
55
 
58
56
  # Return enum values in an array suitable to pass to a Rails form select helper.
59
- def to_select
60
- store.values.map { |v| [v[1].html_safe, v[0]] }
57
+ def to_select(value_transform: ActiveEnum.default_select_value_transform)
58
+ store.values.map(&value_transform)
61
59
  end
62
60
 
63
61
  # Return enum values in a nested array suitable to pass to a Rails form grouped select helper.
64
- def to_grouped_select(group_by, group_transform: DEFAULT_GROUPED_SELECT_TRANSFORM)
62
+ def to_grouped_select(group_by, group_transform: ActiveEnum.default_select_group_transform, value_transform: ActiveEnum.default_select_value_transform)
65
63
  store.values.group_by { |(_id, _name, meta)| (meta || {})[group_by] }.map { |group, collection|
66
64
  [ group_transform.call(group), collection.map { |(id, name, _meta)| [ name.html_safe, id ] } ]
67
65
  }
@@ -82,20 +80,25 @@ module ActiveEnum
82
80
 
83
81
  # Access id or name value. Pass an id number to retrieve the name or
84
82
  # a symbol or string to retrieve the matching id.
85
- def get(index)
83
+ def get(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
84
+ row = get_value(index, raise_on_not_found)
85
+ return if row.nil?
86
+ index.is_a?(Integer) ? row[1] : row[0]
87
+ end
88
+
89
+ def [](index)
86
90
  row = get_value(index)
87
91
  return if row.nil?
88
92
  index.is_a?(Integer) ? row[1] : row[0]
89
93
  end
90
- alias_method :[], :get
91
94
 
92
95
  def include?(value)
93
96
  !get_value(value, false).nil?
94
97
  end
95
98
 
96
99
  # Access any meta data defined for a given id or name. Returns a hash.
97
- def meta(index)
98
- row = get_value(index)
100
+ def meta(index, raise_on_not_found: ActiveEnum.raise_on_not_found)
101
+ row = get_value(index, raise_on_not_found)
99
102
  row[2] || {} if row
100
103
  end
101
104
 
@@ -41,7 +41,7 @@ module ActiveEnum
41
41
  attribute = attribute.to_sym
42
42
  attributes_enum[attribute] = enum
43
43
 
44
- define_active_enum_methods_for_attribute(attribute)
44
+ define_active_enum_methods_for_attribute(attribute, options) unless options[:skip_accessors]
45
45
  rescue NameError => e
46
46
  raise e unless e.message =~ /uninitialized constant/
47
47
  raise ActiveEnum::EnumNotFound, "Enum class could not be found for attribute '#{attribute}' in class #{self}. Specify the enum class using the :with option."
@@ -54,10 +54,10 @@ module ActiveEnum
54
54
  enumerated_attributes && enumerated_attributes[attribute.to_sym]
55
55
  end
56
56
 
57
- def define_active_enum_methods_for_attribute(attribute)
58
- define_active_enum_read_method(attribute)
59
- define_active_enum_write_method(attribute)
60
- define_active_enum_question_method(attribute)
57
+ def define_active_enum_methods_for_attribute(attribute, options={})
58
+ define_active_enum_read_method(attribute) unless options[:skip_read]
59
+ define_active_enum_write_method(attribute) unless options[:skip_write]
60
+ define_active_enum_question_method(attribute) unless options[:skip_predicate]
61
61
  end
62
62
 
63
63
  def define_implicit_enum_class_for_attribute(attribute, block)
@@ -90,11 +90,11 @@ module ActiveEnum
90
90
  when nil
91
91
  #{ActiveEnum.use_name_as_value ? 'enum[value]' : 'value' }
92
92
  when :id
93
- value if enum[value]
93
+ value if enum.get(value, raise_on_not_found: false)
94
94
  when :name
95
- enum[value].dup
95
+ enum.get(value, raise_on_not_found: false).dup
96
96
  when Symbol
97
- (enum.meta(value) || {})[arg].try(:dup)
97
+ (enum.meta(value, raise_on_not_found: false) || {})[arg].try(:dup)
98
98
  end
99
99
  end
100
100
  DEF
@@ -22,7 +22,7 @@ module ActiveEnum
22
22
  end
23
23
 
24
24
  def translate(key)
25
- I18n.translate key, :scope => i18n_scope, :default => key
25
+ I18n.translate key, scope: i18n_scope, default: key
26
26
  end
27
27
 
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveEnum
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/active_enum.rb CHANGED
@@ -20,6 +20,12 @@ module ActiveEnum
20
20
  mattr_accessor :storage_options
21
21
  @@storage_options = {}
22
22
 
23
+ mattr_accessor :default_select_value_transform
24
+ @@default_select_value_transform = proc { |value| [ value[1].html_safe, value[0] ] }
25
+
26
+ mattr_accessor :default_select_group_transform
27
+ @@default_select_group_transform = proc { |group| group&.html_safe }
28
+
23
29
  def storage=(*args)
24
30
  @@storage_options = args.extract_options!
25
31
  @@storage = args.first
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveEnum::ActsAsEnum do
4
4
  class Person < ActiveRecord::Base
5
- acts_as_enum :name_column => 'first_name'
5
+ acts_as_enum name_column: 'first_name'
6
6
  end
7
7
 
8
8
  class TestPerson < ActiveRecord::Base
@@ -14,14 +14,14 @@ describe ActiveEnum::ActsAsEnum do
14
14
  end
15
15
 
16
16
  class SortedPerson < ActiveRecord::Base
17
- acts_as_enum :name_column => 'first_name', :order => :desc
17
+ acts_as_enum name_column: 'first_name', order: :desc
18
18
  end
19
19
 
20
20
  before(:all) do
21
- Person.create!(:first_name => 'Dave', :last_name => 'Smith')
22
- Person.create!(:first_name => 'John', :last_name => 'Doe')
23
- SortedPerson.create!(:first_name => 'Dave', :last_name => 'Smith')
24
- SortedPerson.create!(:first_name => 'John', :last_name => 'Doe')
21
+ Person.create!(first_name: 'Dave', last_name: 'Smith')
22
+ Person.create!(first_name: 'John', last_name: 'Doe')
23
+ SortedPerson.create!(first_name: 'Dave', last_name: 'Smith')
24
+ SortedPerson.create!(first_name: 'John', last_name: 'Doe')
25
25
  end
26
26
 
27
27
  it "should mixin enum class methods only when act_as_enum defined" do
@@ -31,8 +31,8 @@ describe ActiveEnum::Base do
31
31
 
32
32
  it 'should return an array of arrays with all values defined as [id, name]' do
33
33
  enum = define_enum do
34
- value :name => 'Name 1'
35
- value :name => 'Name 2'
34
+ value name: 'Name 1'
35
+ value name: 'Name 2'
36
36
  end
37
37
  expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
38
38
  end
@@ -41,14 +41,14 @@ describe ActiveEnum::Base do
41
41
  describe ".value" do
42
42
  it 'should allow me to define a value with an id and name' do
43
43
  enum = define_enum do
44
- value :id => 1, :name => 'Name'
44
+ value id: 1, name: 'Name'
45
45
  end
46
46
  expect(enum.values).to eq([[1,'Name']])
47
47
  end
48
48
 
49
49
  it 'should allow me to define a value with a name only' do
50
50
  enum = define_enum do
51
- value :name => 'Name'
51
+ value name: 'Name'
52
52
  end
53
53
  expect(enum.values).to eq([[1,'Name']])
54
54
  end
@@ -62,15 +62,15 @@ describe ActiveEnum::Base do
62
62
 
63
63
  it 'should allow to define meta data value with extra key value pairs' do
64
64
  enum = define_enum do
65
- value :id => 1, :name => 'Name', :description => 'extra'
65
+ value id: 1, name: 'Name', description: 'extra'
66
66
  end
67
- expect(enum.values).to eq([[1,'Name',{:description => 'extra'}]])
67
+ expect(enum.values).to eq([[1,'Name',{description: 'extra'}]])
68
68
  end
69
69
 
70
70
  it 'should increment value ids when defined without ids' do
71
71
  enum = define_enum do
72
- value :name => 'Name 1'
73
- value :name => 'Name 2'
72
+ value name: 'Name 1'
73
+ value name: 'Name 2'
74
74
  end
75
75
  expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
76
76
  end
@@ -78,8 +78,8 @@ describe ActiveEnum::Base do
78
78
  it 'should raise error if the id is a duplicate' do
79
79
  expect {
80
80
  define_enum do
81
- value :id => 1, :name => 'Name 1'
82
- value :id => 1, :name => 'Name 2'
81
+ value id: 1, name: 'Name 1'
82
+ value id: 1, name: 'Name 2'
83
83
  end
84
84
  }.to raise_error(ActiveEnum::DuplicateValue)
85
85
  end
@@ -87,8 +87,8 @@ describe ActiveEnum::Base do
87
87
  it 'should raise error if the name is a duplicate' do
88
88
  expect {
89
89
  define_enum do
90
- value :id => 1, :name => 'Name'
91
- value :id => 2, :name => 'Name'
90
+ value id: 1, name: 'Name'
91
+ value id: 2, name: 'Name'
92
92
  end
93
93
  }.to raise_error(ActiveEnum::DuplicateValue)
94
94
  end
@@ -97,25 +97,40 @@ describe ActiveEnum::Base do
97
97
  describe ".meta" do
98
98
  it 'should return meta values hash for a given index value' do
99
99
  enum = define_enum do
100
- value :id => 1, :name => 'Name', :description => 'extra'
100
+ value id: 1, name: 'Name', description: 'extra'
101
101
  end
102
- expect(enum.meta(1)).to eq({:description => 'extra'})
102
+ expect(enum.meta(1)).to eq({description: 'extra'})
103
103
  end
104
104
 
105
105
  it 'should return empty hash for index with no meta defined' do
106
106
  enum = define_enum do
107
- value :id => 1, :name => 'Name'
107
+ value id: 1, name: 'Name'
108
108
  end
109
109
  expect(enum.meta(1)).to eq({})
110
110
  end
111
111
 
112
+ context "with raise_on_not_found: false" do
113
+ let(:enum) {
114
+ define_enum do
115
+ value id: 1, name: 'Name', description: 'extra'
116
+ end
117
+ }
118
+
119
+ it "should raise ActiveEnum::NotFound for missing id" do
120
+ expect { enum.meta(0, raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
121
+ end
122
+
123
+ it "should raise ActiveEnum::NotFound for missing name" do
124
+ expect { enum.meta('Not a value', raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
125
+ end
126
+ end
112
127
  end
113
128
 
114
129
  context "sorting" do
115
130
  it 'should return values ascending by default' do
116
131
  enum = define_enum do
117
- value :id => 2, :name => 'Name 2'
118
- value :id => 1, :name => 'Name 1'
132
+ value id: 2, name: 'Name 2'
133
+ value id: 1, name: 'Name 1'
119
134
  end
120
135
  expect(enum.values).to eq([[1,'Name 1'], [2, 'Name 2']])
121
136
  end
@@ -123,8 +138,8 @@ describe ActiveEnum::Base do
123
138
  it 'should return sorted values by id using order setting' do
124
139
  enum = define_enum do
125
140
  order :desc
126
- value :id => 1, :name => 'Name 1'
127
- value :id => 2, :name => 'Name 2'
141
+ value id: 1, name: 'Name 1'
142
+ value id: 2, name: 'Name 2'
128
143
  end
129
144
  expect(enum.values).to eq([[2, 'Name 2'], [1,'Name 1']])
130
145
  end
@@ -132,9 +147,9 @@ describe ActiveEnum::Base do
132
147
  it 'should return sorted values by id using order setting' do
133
148
  enum = define_enum do
134
149
  order :natural
135
- value :id => 3, :name => 'Name 3'
136
- value :id => 1, :name => 'Name 1'
137
- value :id => 2, :name => 'Name 2'
150
+ value id: 3, name: 'Name 3'
151
+ value id: 1, name: 'Name 1'
152
+ value id: 2, name: 'Name 2'
138
153
  end
139
154
  expect(enum.values).to eq([[3,'Name 3'], [1,'Name 1'], [2, 'Name 2']])
140
155
  end
@@ -143,8 +158,8 @@ describe ActiveEnum::Base do
143
158
  describe ".ids" do
144
159
  it 'should return array of ids' do
145
160
  enum = define_enum do
146
- value :id => 1, :name => 'Name 1'
147
- value :id => 2, :name => 'Name 2'
161
+ value id: 1, name: 'Name 1'
162
+ value id: 2, name: 'Name 2'
148
163
  end
149
164
  expect(enum.ids).to eq([1,2])
150
165
  end
@@ -153,8 +168,8 @@ describe ActiveEnum::Base do
153
168
  describe ".names" do
154
169
  it 'should return array of names' do
155
170
  enum = define_enum do
156
- value :id => 1, :name => 'Name 1'
157
- value :id => 2, :name => 'Name 2'
171
+ value id: 1, name: 'Name 1'
172
+ value id: 2, name: 'Name 2'
158
173
  end
159
174
  expect(enum.names).to eq(['Name 1', 'Name 2'])
160
175
  end
@@ -163,8 +178,8 @@ describe ActiveEnum::Base do
163
178
  context "element reference method" do
164
179
  let(:enum) {
165
180
  define_enum do
166
- value :id => 1, :name => 'Name 1'
167
- value :id => 2, :name => 'Name 2'
181
+ value id: 1, name: 'Name 1'
182
+ value id: 2, name: 'Name 2'
168
183
  end
169
184
  }
170
185
 
@@ -190,7 +205,7 @@ describe ActiveEnum::Base do
190
205
  expect(enum[0]).to eq nil
191
206
  end
192
207
 
193
- context "with raise_on_not_found" do
208
+ context "with config raise_on_not_found" do
194
209
  with_config :raise_on_not_found, true
195
210
 
196
211
  it "should raise ActiveEnum::NotFound for missing id" do
@@ -204,11 +219,32 @@ describe ActiveEnum::Base do
204
219
  end
205
220
  end
206
221
 
222
+ describe '.get' do
223
+ with_config :raise_on_not_found, false
224
+
225
+ let(:enum) {
226
+ define_enum do
227
+ value id: 1, name: 'Name 1'
228
+ value id: 2, name: 'Name 2'
229
+ end
230
+ }
231
+
232
+ context "with raise_on_not_found: false" do
233
+ it "should raise ActiveEnum::NotFound for missing id" do
234
+ expect { enum.get('Not a value', raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
235
+ end
236
+
237
+ it "should raise ActiveEnum::NotFound for missing name" do
238
+ expect { enum.get(0, raise_on_not_found: true) }.to raise_error(ActiveEnum::NotFound)
239
+ end
240
+ end
241
+ end
242
+
207
243
  describe ".include?" do
208
244
  let(:enum) {
209
245
  define_enum do
210
- value :id => 1, :name => 'Name 1'
211
- value :id => 2, :name => 'Name 2'
246
+ value id: 1, name: 'Name 1'
247
+ value id: 2, name: 'Name 2'
212
248
  end
213
249
  }
214
250
 
@@ -232,8 +268,8 @@ describe ActiveEnum::Base do
232
268
  describe ".to_select" do
233
269
  it 'should return array for select helpers' do
234
270
  enum = define_enum do
235
- value :id => 1, :name => 'Name 1'
236
- value :id => 2, :name => 'Name 2'
271
+ value id: 1, name: 'Name 1'
272
+ value id: 2, name: 'Name 2'
237
273
  end
238
274
  expect(enum.to_select).to eq([['Name 1',1], ['Name 2',2]])
239
275
  end
@@ -241,8 +277,8 @@ describe ActiveEnum::Base do
241
277
  it 'should return array sorted using order setting' do
242
278
  enum = define_enum do
243
279
  order :desc
244
- value :id => 1, :name => 'Name 1'
245
- value :id => 2, :name => 'Name 2'
280
+ value id: 1, name: 'Name 1'
281
+ value id: 2, name: 'Name 2'
246
282
  end
247
283
  expect(enum.to_select).to eq([['Name 2',2], ['Name 1',1]])
248
284
  end
@@ -251,8 +287,8 @@ describe ActiveEnum::Base do
251
287
  describe ".to_grouped_select" do
252
288
  it 'should return array for grouped select helpers grouped by meta key value' do
253
289
  enum = define_enum do
254
- value :id => 1, :name => 'Name 1', :category => 'Foo'
255
- value :id => 2, :name => 'Name 2', :category => 'Bar'
290
+ value id: 1, name: 'Name 1', category: 'Foo'
291
+ value id: 2, name: 'Name 2', category: 'Bar'
256
292
  end
257
293
 
258
294
  expect(enum.to_grouped_select(:category)).to eq([
@@ -263,8 +299,8 @@ describe ActiveEnum::Base do
263
299
 
264
300
  it 'should group any value missing the group_by key by nil' do
265
301
  enum = define_enum do
266
- value :id => 1, :name => 'Name 1', :category => 'Foo'
267
- value :id => 2, :name => 'Name 2'
302
+ value id: 1, name: 'Name 1', category: 'Foo'
303
+ value id: 2, name: 'Name 2'
268
304
  end
269
305
 
270
306
  expect(enum.to_grouped_select(:category)).to eq([
@@ -275,8 +311,8 @@ describe ActiveEnum::Base do
275
311
 
276
312
  it 'shoud transform group name with custom group transform proc' do
277
313
  enum = define_enum do
278
- value :id => 1, :name => 'Name 1', :category => 'Foo'
279
- value :id => 2, :name => 'Name 2'
314
+ value id: 1, name: 'Name 1', category: 'Foo'
315
+ value id: 2, name: 'Name 2'
280
316
  end
281
317
 
282
318
  expect(enum.to_grouped_select(:category, group_transform: proc { |group| group.to_s.upcase })).to eq([
@@ -289,8 +325,8 @@ describe ActiveEnum::Base do
289
325
  describe ".to_h" do
290
326
  it 'should return hash of ids as keys and names as values' do
291
327
  enum = define_enum do
292
- value :id => 1, :name => 'Name 1'
293
- value :id => 2, :name => 'Name 2'
328
+ value id: 1, name: 'Name 1'
329
+ value id: 2, name: 'Name 2'
294
330
  end
295
331
  expect(enum.to_h).to eq({ 1 => 'Name 1', 2 => 'Name 2' })
296
332
  end
@@ -299,8 +335,8 @@ describe ActiveEnum::Base do
299
335
  describe ".length" do
300
336
  it 'should return number of values defined' do
301
337
  enum = define_enum do
302
- value :id => 1, :name => 'Name 1'
303
- value :id => 2, :name => 'Name 2'
338
+ value id: 1, name: 'Name 1'
339
+ value id: 2, name: 'Name 2'
304
340
  end
305
341
  expect(enum.length).to eq 2
306
342
  end
@@ -312,8 +348,8 @@ describe ActiveEnum::Base do
312
348
 
313
349
  it 'should be aliased as size' do
314
350
  enum = define_enum do
315
- value :id => 1, :name => 'Name 1'
316
- value :id => 2, :name => 'Name 2'
351
+ value id: 1, name: 'Name 1'
352
+ value id: 2, name: 'Name 2'
317
353
  end
318
354
  expect(enum.size).to eq enum.length
319
355
  end
@@ -2,14 +2,14 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveEnum::Extensions do
4
4
  class Sex < ActiveEnum::Base
5
- value :id => 1, :name => 'Male'
6
- value :id => 2, :name => 'Female'
5
+ value id: 1, name: 'Male'
6
+ value id: 2, name: 'Female'
7
7
  end
8
8
 
9
9
  class Accepted < ActiveEnum::Base
10
- value :id => 0, :name => 'No'
11
- value :id => 1, :name => 'Definitely'
12
- value :id => 2, :name => 'Maybe'
10
+ value id: 0, name: 'No'
11
+ value id: 1, name: 'Definitely'
12
+ value id: 2, name: 'Maybe'
13
13
  end
14
14
 
15
15
  it 'should add class :enumerate method to ActiveRecord' do
@@ -21,17 +21,17 @@ describe ActiveEnum::Extensions do
21
21
  end
22
22
 
23
23
  it 'should allow multiple attributes to be enumerated with same enum' do
24
- Person.enumerate :attending, :staying, :with => Accepted
24
+ Person.enumerate :attending, :staying, with: Accepted
25
25
 
26
26
  expect(Person.active_enum_for(:attending)).to eq(Accepted)
27
27
  expect(Person.active_enum_for(:staying)).to eq(Accepted)
28
28
  end
29
29
 
30
30
  it 'should allow multiple attributes to be enumerated with different enums' do
31
- Person.enumerate :sex, :with => Sex
32
- Person.enumerate :attending, :with => Accepted
31
+ Person.enumerate :sex, with: Sex
32
+ Person.enumerate :attending, with: Accepted
33
33
 
34
- expect(Person.active_enum_for(:sex)).to eq(Sex)
34
+ expect(Person.active_enum_for(:sex)).to eq(Sex)
35
35
  expect(Person.active_enum_for(:attending)).to eq(Accepted)
36
36
  end
37
37
 
@@ -43,7 +43,7 @@ describe ActiveEnum::Extensions do
43
43
 
44
44
  it 'should create enum namespaced enum class from block' do
45
45
  Person.enumerate :sex do
46
- value :id => 1, :name => 'Male'
46
+ value id: 1, name: 'Male'
47
47
  end
48
48
  expect(Person.active_enum_for(:sex)).to eq(::Person::Sex)
49
49
  end
@@ -55,11 +55,11 @@ describe ActiveEnum::Extensions do
55
55
  end
56
56
 
57
57
  context "attribute" do
58
- let(:person) { Person.new(:sex => 1) }
58
+ let(:person) { Person.new(sex: 1) }
59
59
 
60
60
  before(:all) do
61
61
  reset_class Person do
62
- enumerate :sex, :with => Sex
62
+ enumerate :sex, with: Sex
63
63
  end
64
64
  end
65
65
 
@@ -82,7 +82,7 @@ describe ActiveEnum::Extensions do
82
82
  end
83
83
 
84
84
  context "with nil value" do
85
- let(:person) { Person.new(:sex => nil) }
85
+ let(:person) { Person.new(sex: nil) }
86
86
 
87
87
  it 'should return nil with no arg' do
88
88
  expect(person.sex).to be_nil
@@ -100,7 +100,7 @@ describe ActiveEnum::Extensions do
100
100
  expect(person.sex(:enum)).to eq(Sex)
101
101
  end
102
102
 
103
- context "and raise_on_not_found" do
103
+ context "and global raise_on_not_found set to true" do
104
104
  with_config :raise_on_not_found, true
105
105
 
106
106
  it "should not raise error when attribute is nil" do
@@ -110,7 +110,7 @@ describe ActiveEnum::Extensions do
110
110
  end
111
111
 
112
112
  context "with undefined value" do
113
- let(:person) { Person.new(:sex => -1) }
113
+ let(:person) { Person.new(sex: -1) }
114
114
 
115
115
  it 'should return value with no arg' do
116
116
  expect(person.sex).to eq(-1)
@@ -127,16 +127,24 @@ describe ActiveEnum::Extensions do
127
127
  it 'should return enum class for attribute' do
128
128
  expect(person.sex(:enum)).to eq(Sex)
129
129
  end
130
+
131
+ context "and global raise_on_not_found set to true" do
132
+ with_config :raise_on_not_found, true
133
+
134
+ it "should not raise error when attribute value is invalid" do
135
+ expect { person.sex(:id) }.to_not raise_error
136
+ end
137
+ end
130
138
  end
131
139
 
132
140
  context "with meta data" do
133
- let(:person) { Person.new(:sex =>1) }
141
+ let(:person) { Person.new(sex: 1) }
134
142
 
135
143
  before(:all) do
136
144
  reset_class Person do
137
145
  enumerate :sex do
138
- value :id => 1, :name => 'Male', :description => 'Man'
139
- value :id => 2, :name => 'Female', :description => 'Woman'
146
+ value id: 1, name: 'Male', description: 'Man'
147
+ value id: 2, name: 'Female', description: 'Woman'
140
148
  end
141
149
  end
142
150
  end
@@ -153,6 +161,15 @@ describe ActiveEnum::Extensions do
153
161
  person.sex = nil
154
162
  expect(person.sex(:description)).to be_nil
155
163
  end
164
+
165
+ context "and global raise_on_not_found set to true" do
166
+ let(:person) { Person.new(sex: -1) }
167
+ with_config :raise_on_not_found, true
168
+
169
+ it "should not raise error when attribute value is invalid" do
170
+ expect { person.sex(:nonexistent) }.to_not raise_error
171
+ end
172
+ end
156
173
  end
157
174
 
158
175
  context "question method" do
@@ -201,11 +218,11 @@ describe ActiveEnum::Extensions do
201
218
  before(:all) { ActiveEnum.use_name_as_value = true }
202
219
  let(:person) { Person.new(:sex =>1) }
203
220
 
204
- before do
221
+ before do
205
222
  reset_class Person do
206
- enumerate :sex, :with => Sex
223
+ enumerate :sex, with: Sex
207
224
  end
208
- end
225
+ end
209
226
 
210
227
  it 'should return text name value for attribute' do
211
228
  expect(person.sex).to eq('Male')
@@ -218,6 +235,27 @@ describe ActiveEnum::Extensions do
218
235
  after(:all) { ActiveEnum.use_name_as_value = false }
219
236
  end
220
237
 
238
+ context "with skip_accessors: true" do
239
+ before(:all) do
240
+ reset_class Person do
241
+ enumerate :sex, with: Sex, skip_accessors: true
242
+ end
243
+ end
244
+
245
+ it 'read method should not accept arg' do
246
+ expect{ person.sex(:name) }.to raise_error ArgumentError
247
+ end
248
+
249
+ it 'write method should not accept name' do
250
+ person.sex = :male
251
+ expect(person.sex).to_not eq Sex[:male]
252
+ end
253
+
254
+ it 'question method should not accept arg' do
255
+ expect{ person.sex?(:male) }.to raise_error ArgumentError
256
+ end
257
+ end
258
+
221
259
  end
222
260
 
223
261
  end
@@ -3,87 +3,87 @@ require "spec_helper"
3
3
  require 'simple_form'
4
4
  require 'active_enum/form_helpers/simple_form'
5
5
 
6
- describe ActiveEnum::FormHelpers::SimpleForm, :type => :helper do
6
+ describe ActiveEnum::FormHelpers::SimpleForm, type: :helper do
7
7
  include SimpleForm::ActionViewExtensions::FormHelper
8
8
 
9
9
  before do
10
10
  allow(controller).to receive(:action_name).and_return('new')
11
11
  reset_class Person do
12
12
  enumerate :sex do
13
- value :id => 1, :name => 'Male'
14
- value :id => 2, :name => 'Female'
13
+ value id: 1, name: 'Male'
14
+ value id: 2, name: 'Female'
15
15
  end
16
16
 
17
17
  enumerate :employment_status do
18
- value :id => 1, :name => 'Full-time', group: 'Waged'
19
- value :id => 3, :name => 'Part-time', group: 'Waged'
20
- value :id => 4, :name => 'Casual', group: 'Waged'
21
- value :id => 5, :name => 'Student', group: 'Un-waged'
22
- value :id => 6, :name => 'Retired', group: 'Un-waged'
23
- value :id => 7, :name => 'Unemployed', group: 'Un-waged'
24
- value :id => 8, :name => 'Carer', group: 'Un-waged'
18
+ value id: 1, name: 'Full-time', group: 'Waged'
19
+ value id: 3, name: 'Part-time', group: 'Waged'
20
+ value id: 4, name: 'Casual', group: 'Waged'
21
+ value id: 5, name: 'Student', group: 'Un-waged'
22
+ value id: 6, name: 'Retired', group: 'Un-waged'
23
+ value id: 7, name: 'Unemployed', group: 'Un-waged'
24
+ value id: 8, name: 'Carer', group: 'Un-waged'
25
25
  end
26
26
  end
27
27
  end
28
28
 
29
29
  it "should use enum input type for enumerated attribute" do
30
- output = simple_form_for(Person.new, :url => people_path) do |f|
30
+ output = simple_form_for(Person.new, url: people_path) do |f|
31
31
  concat f.input(:sex)
32
32
  end
33
33
  expect(output).to have_selector('select#person_sex')
34
- expect(output).to have_xpath('//option[@value=1]', :text => 'Male')
35
- expect(output).to have_xpath('//option[@value=2]', :text => 'Female')
34
+ expect(output).to have_xpath('//option[@value=1]', text: 'Male')
35
+ expect(output).to have_xpath('//option[@value=2]', text: 'Female')
36
36
  end
37
37
 
38
38
  it "should use explicit :enum input type" do
39
- output = simple_form_for(Person.new, :url => people_path) do |f|
40
- concat f.input(:sex, :as => :enum)
39
+ output = simple_form_for(Person.new, url: people_path) do |f|
40
+ concat f.input(:sex, as: :enum)
41
41
  end
42
42
  expect(output).to have_selector('select#person_sex')
43
- expect(output).to have_xpath('//option[@value=1]', :text => 'Male')
44
- expect(output).to have_xpath('//option[@value=2]', :text => 'Female')
43
+ expect(output).to have_xpath('//option[@value=1]', text: 'Male')
44
+ expect(output).to have_xpath('//option[@value=2]', text: 'Female')
45
45
  end
46
46
 
47
47
  it "should use explicit :grouped_enum input type" do
48
- output = simple_form_for(Person.new, :url => people_path) do |f|
49
- concat f.input(:employment_status, :as => :grouped_enum, :group_by => :group)
48
+ output = simple_form_for(Person.new, url: people_path) do |f|
49
+ concat f.input(:employment_status, as: :grouped_enum, group_by: :group)
50
50
  end
51
51
  expect(output).to have_selector('select#person_employment_status')
52
- expect(output).to have_xpath('//optgroup[@label="Waged"]/option[@value=1]', :text => 'Full-time')
53
- expect(output).to have_xpath('//optgroup[@label="Un-waged"]/option[@value=8]', :text => 'Carer')
52
+ expect(output).to have_xpath('//optgroup[@label="Waged"]/option[@value=1]', text: 'Full-time')
53
+ expect(output).to have_xpath('//optgroup[@label="Un-waged"]/option[@value=8]', text: 'Carer')
54
54
  end
55
55
 
56
56
  it "should not use enum input type if :as option indicates other type" do
57
- output = simple_form_for(Person.new, :url => people_path) do |f|
58
- concat f.input(:sex, :as => :string)
57
+ output = simple_form_for(Person.new, url: people_path) do |f|
58
+ concat f.input(:sex, as: :string)
59
59
  end
60
60
  expect(output).to have_selector('input#person_sex')
61
61
  end
62
62
 
63
63
  it "should raise error if attribute for enum input is not enumerated" do
64
64
  expect {
65
- simple_form_for(Person.new, :url => people_path) do |f|
66
- f.input(:attending, :as => :enum)
65
+ simple_form_for(Person.new, url: people_path) do |f|
66
+ f.input(:attending, as: :enum)
67
67
  end
68
68
  }.to raise_error "Attribute 'attending' has no enum class"
69
69
  end
70
70
 
71
71
  it "should not use enum input type if class does not support ActiveEnum" do
72
- output = simple_form_for(NotActiveRecord.new, :as => :not_active_record, :url => people_path) do |f|
72
+ output = simple_form_for(NotActiveRecord.new, as: :not_active_record, url: people_path) do |f|
73
73
  concat f.input(:name)
74
74
  end
75
75
  expect(output).to have_selector('input#not_active_record_name')
76
76
  end
77
77
 
78
78
  it "should allow non-enum fields to use default input determination" do
79
- output = simple_form_for(Person.new, :url => people_path) do |f|
79
+ output = simple_form_for(Person.new, url: people_path) do |f|
80
80
  concat f.input(:first_name)
81
81
  end
82
82
  expect(output).to have_selector('input#person_first_name')
83
83
  end
84
84
 
85
85
  it "should allow models without enumerated attributes to behave normally" do
86
- output = simple_form_for(NoEnumPerson.new, :url => people_path) do |f|
86
+ output = simple_form_for(NoEnumPerson.new, url: people_path) do |f|
87
87
  concat f.input(:first_name)
88
88
  end
89
89
  expect(output).to have_selector('input#no_enum_person_first_name')
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe ActiveEnum::Storage::I18nStore do
4
4
  class TestI18nStoreEnum < ActiveEnum::Base; end
5
-
5
+
6
6
  let(:enum_class) { TestI18nStoreEnum }
7
7
  let(:enum_key) { enum_class.name.underscore }
8
8
  let(:store) { ActiveEnum::Storage::I18nStore.new(enum_class, :asc) }
@@ -31,8 +31,8 @@ describe ActiveEnum::Storage::I18nStore do
31
31
  end
32
32
 
33
33
  it 'should store value of id, name and meta hash' do
34
- store.set 1, 'test name', :description => 'meta'
35
- expect(store.send(:_values)).to eq([[1, 'test name', {:description => 'meta'}]])
34
+ store.set 1, 'test name', description: 'meta'
35
+ expect(store.send(:_values)).to eq([[1, 'test name', { description: 'meta' }]])
36
36
  end
37
37
 
38
38
  it 'should raise error if duplicate id' do
@@ -59,8 +59,8 @@ describe ActiveEnum::Storage::I18nStore do
59
59
 
60
60
  describe "#values" do
61
61
  before do
62
- I18n.backend.store_translations :en, :active_enum => { enum_key => { :thanks => 'Thanks' } }
63
- I18n.backend.store_translations :fr, :active_enum => { enum_key => { :thanks => 'Merce' } }
62
+ I18n.backend.store_translations :en, active_enum: { enum_key => { thanks: 'Thanks' } }
63
+ I18n.backend.store_translations :fr, active_enum: { enum_key => { thanks: 'Merce' } }
64
64
  end
65
65
 
66
66
  it 'should return array of stored values for current locale' do
@@ -77,7 +77,7 @@ describe ActiveEnum::Storage::I18nStore do
77
77
 
78
78
  describe "#get_by_id" do
79
79
  before do
80
- I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
80
+ I18n.backend.store_translations :en, active_enum: { enum_key => { 'test' => 'Testing' } }
81
81
  I18n.locale = :en
82
82
  end
83
83
 
@@ -87,8 +87,8 @@ describe ActiveEnum::Storage::I18nStore do
87
87
  end
88
88
 
89
89
  it 'should return the value with meta for a given id' do
90
- store.set 1, 'test', :description => 'meta'
91
- expect(store.get_by_id(1)).to eq([1, 'Testing', { :description => 'meta' }])
90
+ store.set 1, 'test', description: 'meta'
91
+ expect(store.get_by_id(1)).to eq([1, 'Testing', { description: 'meta' }])
92
92
  end
93
93
 
94
94
  it 'should return nil when id not found' do
@@ -104,7 +104,7 @@ describe ActiveEnum::Storage::I18nStore do
104
104
 
105
105
  describe "#get_by_name" do
106
106
  before do
107
- I18n.backend.store_translations :en, :active_enum => { enum_key => { 'test' => 'Testing' } }
107
+ I18n.backend.store_translations :en, active_enum: { enum_key => { 'test' => 'Testing' } }
108
108
  I18n.locale = :en
109
109
  end
110
110
 
@@ -114,8 +114,8 @@ describe ActiveEnum::Storage::I18nStore do
114
114
  end
115
115
 
116
116
  it 'should return the value with meta for a given name' do
117
- store.set 1, 'test', :description => 'meta'
118
- expect(store.get_by_name('test')).to eq([1, 'Testing', { :description => 'meta' }])
117
+ store.set 1, 'test', description: 'meta'
118
+ expect(store.get_by_name('test')).to eq([1, 'Testing', { description: 'meta' }])
119
119
  end
120
120
 
121
121
  it 'should return nil when name not found' do
@@ -125,7 +125,7 @@ describe ActiveEnum::Storage::I18nStore do
125
125
 
126
126
  describe "#sort!" do
127
127
  before do
128
- I18n.backend.store_translations :en, :active_enum => { enum_key => {
128
+ I18n.backend.store_translations :en, active_enum: { enum_key => {
129
129
  'name1' => 'Name 1',
130
130
  'name2' => 'Name 2',
131
131
  'name3' => 'Name 3',
@@ -13,8 +13,8 @@ describe ActiveEnum::Storage::MemoryStore do
13
13
  end
14
14
 
15
15
  it 'should store value of id, name and meta hash' do
16
- store.set 1, 'test name', :description => 'meta'
17
- expect(store.values).to eq([[1, 'test name', {:description => 'meta'}]])
16
+ store.set 1, 'test name', description: 'meta'
17
+ expect(store.values).to eq([[1, 'test name', {description: 'meta'}]])
18
18
  end
19
19
 
20
20
  it 'should raise error if duplicate id' do
@@ -54,8 +54,8 @@ describe ActiveEnum::Storage::MemoryStore do
54
54
 
55
55
  describe "#get_by_id" do
56
56
  it 'should return the value for a given id' do
57
- store.set 1, 'test name', :description => 'meta'
58
- expect(store.get_by_id(1)).to eq([1, 'test name', {:description => "meta"}])
57
+ store.set 1, 'test name', description: 'meta'
58
+ expect(store.get_by_id(1)).to eq([1, 'test name', {description: "meta"}])
59
59
  end
60
60
 
61
61
  it 'should return nil when id not found' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Meehan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-04 00:00:00.000000000 Z
11
+ date: 2024-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubygems_version: 3.0.3.1
90
+ rubygems_version: 3.5.6
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Define enum classes in Rails and use them to enumerate ActiveRecord attributes