enumerations 2.1.0 → 2.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
  SHA1:
3
- metadata.gz: e9193bf66247b09a32a3a72da1257fe97890da52
4
- data.tar.gz: b005aaba38109c814c7020e7053d31a56a6f98a3
3
+ metadata.gz: 7e452f81340fb7e16689b456cab621d4cd84f787
4
+ data.tar.gz: 154907cc77af681118cb5639fe0a7e68b3281525
5
5
  SHA512:
6
- metadata.gz: 2003c66e3f930f5b3fafd9eea13036159a0583694384a1c644566ca49150485516186dffa0a2191feac09064602f3a89af31607d7b3c7dab6adea3a589b71d02
7
- data.tar.gz: de534b1cab979f08d362ce42ab61fbe2bf4851fb32da2adc4e1e1abd31a7274dd92b261488cb38402eee36ab4220fde052a357ba289fba2491be50879fa3627c
6
+ metadata.gz: a8070503f33e094a8344c95a5103cbd71505345efb89ee4d61e5ac84163b0f541cbbc7e1e319a695b1c39eeafd2140b0ef9d4a4895db27bf69cf7cdcb5af1e85
7
+ data.tar.gz: a45c4d87770db1b4730094acbfe20abe84cb7f92499b9b42e0eb555e9934d0513353f2ef0670c961c57f867d0d668b2c76369d122de05e6710e5fe521107142f
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
1
  .DS_Store
2
2
  *.gem
3
3
  Gemfile.lock
4
+ .todo
data/Readme.md CHANGED
@@ -26,9 +26,9 @@ Create a model for your enumerations:
26
26
 
27
27
  ```ruby
28
28
  class Status < Enumerations::Base
29
- values draft: { id: 1, name: 'Draft' },
30
- review_pending: { id: 2, name: 'Review pending' },
31
- published: { id: 3, name: 'Published' }
29
+ values draft: { name: 'Draft' },
30
+ review_pending: { name: 'Review pending' },
31
+ published: { name: 'Published' }
32
32
  end
33
33
  ```
34
34
 
@@ -36,9 +36,9 @@ Or you can use `value` method for defining your enumerations:
36
36
 
37
37
  ```ruby
38
38
  class Status < Enumerations::Base
39
- value :draft, id: 1, name: 'Draft'
40
- value :review_pending, id: 2, name: 'Review pending'
41
- value :published, id: 3, name: 'Published'
39
+ value :draft, name: 'Draft'
40
+ value :review_pending, name: 'Review pending'
41
+ value :published, name: 'Published'
42
42
  end
43
43
  ```
44
44
 
@@ -48,7 +48,7 @@ Include enumerations for integer fields in other models:
48
48
  class Post < ActiveRecord::Base
49
49
  enumeration :status
50
50
 
51
- validates :status, presence: true # You can validate either :status or :status_id
51
+ validates :status, presence: true
52
52
  end
53
53
  ```
54
54
 
@@ -57,8 +57,8 @@ You can pass attributes to specify which enumeration and which column to use:
57
57
  ```ruby
58
58
  class Post < ActiveRecord::Base
59
59
  enumeration :status,
60
- foreign_key: :post_status_id, # specifies which column to use
61
- class_name: Post::Status # specifies the class of the enumerator
60
+ foreign_key: :post_status, # specifies which column to use
61
+ class_name: Post::Status # specifies the class of the enumerator
62
62
 
63
63
  validates :post_status, presence: true
64
64
  end
@@ -69,18 +69,18 @@ Attribute `foreign_key` you can pass as a `String` or a `Symbol`. Attribute `cla
69
69
 
70
70
  ## Setting enumeration value to objects
71
71
 
72
- Set enumerations, find enumerations by `symbol`:
72
+ Set enumerations:
73
73
 
74
74
  ```ruby
75
75
  @post = Post.first
76
- @post.status = Status.find(:draft)
76
+ @post.status = Status.draft
77
77
  @post.save
78
78
  ```
79
79
 
80
- Or you can set enumerations on this way:
80
+ Or you can set enumerations by `symbol`:
81
81
 
82
82
  ```ruby
83
- @post.status = Status.draft
83
+ @post.status = Status.find(:draft)
84
84
  ```
85
85
 
86
86
  Also, you can set enumeration value like this:
@@ -89,7 +89,8 @@ Also, you can set enumeration value like this:
89
89
  @post.status_draft!
90
90
  ```
91
91
 
92
- > When you include enumerations into your model, you'll get methods for setting each enumeration value. Each method name is consists from enumeration name and enumeration value name with **!** at the end. Examples:
92
+ > When you include enumerations into your model, you'll get methods for setting each enumeration value.
93
+ Each method name is consists from enumeration name and enumeration value name with **!** at the end. Examples:
93
94
 
94
95
  ```ruby
95
96
  class Post < ActiveRecord::Base
@@ -129,10 +130,10 @@ Find enumerations by `id`:
129
130
  Other finding methods:
130
131
 
131
132
  ```ruby
132
- # Find by id as a String
133
- Status.find('2') # => Review pending
133
+ # Find by key as a Symbol
134
+ Status.find(:review_pending) # => Review pending
134
135
 
135
- # Find by symbol as a String
136
+ # Find by key as a String
136
137
  Status.find('draft') # => Draft
137
138
 
138
139
  # Find by multiple attributes
@@ -143,8 +144,8 @@ Compare enumerations:
143
144
 
144
145
  ```ruby
145
146
  @post.status == :published # => true
146
- @post.status == 3 # => true
147
- @post.status == Status.find(:published) # => true
147
+ @post.status == 'published' # => true
148
+ @post.status == Status.published # => true
148
149
  @post.status.published? # => true
149
150
  ```
150
151
 
@@ -206,25 +207,26 @@ Use in forms:
206
207
 
207
208
  ```ruby
208
209
  %p
209
- = f.label :status_id
210
+ = f.label :status
210
211
  %br
211
- = f.collection_select :status_id, Status.all, :id, :name
212
+ = f.collection_select :status, Status.all, :symbol, :name
212
213
  ```
213
214
 
214
- Advance Usage
215
+ Advanced Usage
215
216
  =====
216
217
 
217
- Except `id` and `name` you can specify other attributes to your enumerations:
218
+ Except `name` you can specify any other attributes to your enumerations:
218
219
 
219
220
  ```ruby
220
221
  class Status < Enumerations::Base
221
222
  value :draft, id: 1, name: 'Draft'
222
223
  value :review_pending, id: 2, name: 'Review pending', description: 'Some description...'
223
- value :published, id: 3, name: 'Published'
224
+ value :published, id: 3, name: 'Published', published: true
224
225
  end
225
226
  ```
226
227
 
227
- Every enumeration has `id`, `name` and `description` methods. If you call method that is not in attribute list for enumeration, it will return `nil`.
228
+ Every enumeration has `id`, `name`, `description` and `published` methods.
229
+ If you call method that is not in attribute list for enumeration, it will return `nil`.
228
230
 
229
231
  ```ruby
230
232
  Status.review_pending.description # => 'Some description...'
@@ -261,7 +263,105 @@ en:
261
263
  I18n.load_path += Dir[Rails.root.join('config', 'locales', 'enumerations', '*.yml')]
262
264
  ```
263
265
 
264
- Author
265
- ======
266
+ Configuration
267
+ =============
268
+
269
+ Basically no configuration is needed.
270
+
271
+ **Enumerations** has two configuration options.
272
+ You can customize primary key and foreign key suffix.
273
+ Just add initializer file to `config/initializers/enumerations.rb`.
274
+
275
+ Example of configuration:
276
+
277
+ ```ruby
278
+ # config/initializers/enumerations.rb
279
+
280
+ Enumerations.configure do |config|
281
+ config.primary_key = :id
282
+ config.foreign_key_suffix = :id
283
+ end
284
+ ```
285
+
286
+ By default, `primary_key` and `foreign_key_suffix` are set to `nil`.
287
+
288
+ By default model enumeration value is saved to column with same name as enumeration.
289
+ If you set enumeration as:
290
+ ```ruby
291
+ enumeration :status
292
+ ```
293
+ then model should have `status`column (as `String` type).
294
+ If you want save an `ID` to this column, you can set `foreign_key_suffix` to `id`.
295
+ Then model should have `status_id` column.
296
+
297
+ If you set `primary_key` then you need provide this attribute for all enumerations values.
298
+ Also, value from `primary_key` attribute will be stored to model as enumeration value.
299
+
300
+ For example:
301
+
302
+ ```ruby
303
+ # with default configuration
304
+
305
+ post = Post.new
306
+ post.status = Status.draft # => post.status = 'draft'
307
+
308
+ # with configured primary_key and foreign_key_suffix:
309
+
310
+ Enumerations.configure do |config|
311
+ config.primary_key = :id
312
+ config.foreign_key_suffix = :id
313
+ end
314
+
315
+ class Status < Enumerations::Base
316
+ value :draft, id: 1, name: 'Draft'
317
+ value :review_pending, id: 2, name: 'Review pending',
318
+ value :published, id: 3, name: 'Published', published: true
319
+ end
320
+
321
+ post = Post.new
322
+ post.status = Status.draft # => post.status_id = 1
323
+ ```
324
+
325
+ Database Enumerations
326
+ =====================
327
+
328
+ By default, enumeration values are saved to database as `String`.
329
+ If you want, you can define `Enum` type in database:
330
+
331
+ ```sql
332
+ CREATE TYPE status AS ENUM ('draft', 'review_pending', 'published');
333
+ ```
334
+
335
+ Then you'll have enumeration as type in database and you can use it in database migrations:
336
+
337
+ ```ruby
338
+ add_column :posts, :status, :status, index: true
339
+ ```
340
+
341
+ With configuration option `primary_key`, you can store any type you want (e.g. `Integer`).
342
+
343
+ Also, for performance reasons, you should add indices to enumeration column.
344
+
345
+ [Here](https://www.postgresql.org/docs/9.1/static/datatype-enum.html) you can find more informations about ENUM types.
346
+
347
+
348
+
349
+ Contributing
350
+ ============
351
+
352
+ 1. Fork it
353
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
354
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
355
+ 4. Push to the branch (`git push origin my-new-feature`)
356
+ 5. Create new Pull Request
357
+
358
+ Credits
359
+ =======
360
+ **Enumerations** is maintained and sponsored by [Infinum](https://infinum.co)
361
+
362
+ Copyright © 2016 Infinum Ltd.
363
+
364
+ License
365
+ =======
266
366
 
267
- Copyright © 2010 Tomislav Car, Infinum Ltd.
367
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/enumerations.gemspec CHANGED
@@ -3,13 +3,20 @@ require File.expand_path('../lib/enumerations/version', __FILE__)
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'enumerations'
5
5
  s.version = Enumerations::VERSION
6
- s.date = '2016-08-15'
6
+ s.date = '2016-09-15'
7
7
  s.summary = 'Enumerations for ActiveRecord!'
8
8
  s.description = 'Extends ActiveRecord with enumeration capabilites.'
9
- s.authors = ['Tomislav Car', 'Nikica Jokic', 'Nikola Santic']
10
- s.email = ['tomislav@infinum.hr', 'nikica.jokic@infinum.hr', 'nikola.santic@infinum.hr']
11
9
  s.homepage = 'https://github.com/infinum/enumerations'
12
10
 
11
+ s.authors = [
12
+ 'Tomislav Car', 'Nikica Jokić', 'Nikola Santić', 'Stjepan Hadjić', 'Petar Ćurković'
13
+ ]
14
+
15
+ s.email = [
16
+ 'tomislav@infinum.hr', 'nikica.jokic@infinum.hr', 'nikola.santic@infinum.hr',
17
+ 'stjepan.hadjic@infinum.hr', 'petar.curkovic@infinum.hr'
18
+ ]
19
+
13
20
  s.add_dependency 'activerecord'
14
21
  s.add_dependency 'activesupport'
15
22
  s.add_dependency 'i18n'
data/lib/enumerations.rb CHANGED
@@ -3,6 +3,7 @@ require 'active_support/concern'
3
3
  require 'active_support/core_ext/class/attribute'
4
4
  require 'active_support/core_ext/string/inflections'
5
5
 
6
+ require 'enumerations/configuration'
6
7
  require 'enumerations/version'
7
8
  require 'enumerations/base'
8
9
  require 'enumerations/reflection'
@@ -25,11 +26,9 @@ module Enumerations
25
26
  # enumeration :role
26
27
  # end
27
28
  #
28
- # user.role_id = 1
29
29
  # user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>
30
30
  #
31
31
  # user.role = Role.staff
32
- # user.role_id => 2
33
32
  #
34
33
  def enumeration(name, options = {})
35
34
  reflection = Reflection.new(name, options)
@@ -67,12 +66,12 @@ module Enumerations
67
66
  #
68
67
  # Example:
69
68
  #
70
- # user.role_id = 1
69
+ # user.role = Role.admin
71
70
  # user.role => #<Enumerations::Value: @base=Role, @symbol=:admin...>
72
71
  #
73
72
  def define_getter_method(reflection)
74
73
  define_method(reflection.name) do
75
- reflection.enumerator_class.find(send(reflection.foreign_key))
74
+ reflection.enumerator_class.find(self[reflection.foreign_key])
76
75
  end
77
76
  end
78
77
 
@@ -81,11 +80,13 @@ module Enumerations
81
80
  # Example:
82
81
  #
83
82
  # user.role = Role.admin
84
- # user.role_id => 1
85
83
  #
86
84
  def define_setter_method(reflection)
87
85
  define_method("#{reflection.name}=") do |other|
88
- send("#{reflection.foreign_key}=", other.id)
86
+ enumeration_value = reflection.enumerator_class.find(other)
87
+
88
+ self[reflection.foreign_key] =
89
+ enumeration_value.send(Enumerations.configuration.primary_key || :symbol)
89
90
  end
90
91
  end
91
92
 
@@ -115,8 +116,10 @@ module Enumerations
115
116
  #
116
117
  def define_scopes(reflection)
117
118
  reflection.enumerator_class.all.each do |enumeration|
119
+ foreign_key = enumeration.send(Enumerations.configuration.primary_key || :symbol)
120
+
118
121
  scope "with_#{reflection.name}_#{enumeration.symbol}",
119
- -> { where(reflection.foreign_key => enumeration.id) }
122
+ -> { where(reflection.foreign_key => foreign_key) }
120
123
  end
121
124
  end
122
125
  end
@@ -9,6 +9,7 @@ module Enumerations
9
9
  include Enumerations::Value
10
10
 
11
11
  class_attribute :_values, :_symbol_index
12
+
12
13
  self._values = {}
13
14
  self._symbol_index = {}
14
15
 
@@ -18,17 +19,13 @@ module Enumerations
18
19
  #
19
20
  # value :admin, id: 1, name: 'Admin', description: 'Some description...'
20
21
  #
21
- # Role.admin.id => # 1
22
- # Role.find(:admin).name => # "Admin"
23
- # Role.find(1).description => # "Some description..."
22
+ # Role.find(:admin).name => # "Admin"
23
+ # Role.find(:admin).description => # "Some description..."
24
24
  #
25
25
  def self.value(symbol, attributes)
26
- raise 'Enumeration id is required' if attributes[:id].nil?
27
- raise "Duplicate symbol #{symbol}" if find(symbol)
28
- raise "Duplicate id #{attributes[:id]}" if find(attributes[:id])
26
+ validate_symbol_and_primary_key(symbol, attributes)
29
27
 
30
28
  self._values = _values.merge(symbol => new(symbol, attributes))
31
- self._symbol_index = _symbol_index.merge(symbol => attributes[:id])
32
29
 
33
30
  # Adds name base finder methods
34
31
  #
@@ -50,9 +47,9 @@ module Enumerations
50
47
  # manager: { id: 2, name: 'Manager' },
51
48
  # staff: { id: 3, name: 'Staff', description: 'Some description...' }
52
49
  #
53
- # Role.admin.id => # 1
54
- # Role.find(:manager).name => # "Manager"
55
- # Role.find(3).description => # "Some description..."
50
+ # Role.admin.id => # 1
51
+ # Role.find(:manager).name => # "Manager"
52
+ # Role.find(:manager).description => # "Some description..."
56
53
  #
57
54
  def self.values(values)
58
55
  values.each do |symbol, attributes|
@@ -82,6 +79,20 @@ module Enumerations
82
79
  _values.values
83
80
  end
84
81
 
82
+ def self.validate_symbol_and_primary_key(symbol, attributes)
83
+ raise "Duplicate symbol #{symbol}" if find(symbol)
84
+
85
+ primary_key = Enumerations.configuration.primary_key
86
+ return if primary_key.nil?
87
+
88
+ raise 'Enumeration primary key is required' if attributes[primary_key].nil?
89
+ raise "Duplicate primary key #{attributes[primary_key]}" if find(attributes[primary_key])
90
+
91
+ self._symbol_index = _symbol_index.merge(symbol => attributes[primary_key])
92
+ end
93
+
94
+ private_class_method :validate_symbol_and_primary_key
95
+
85
96
  attr_reader :symbol, :attributes
86
97
 
87
98
  def initialize(symbol, attributes)
@@ -0,0 +1,25 @@
1
+ module Enumerations
2
+ class << self
3
+ attr_accessor :configuration
4
+ end
5
+
6
+ def self.configure
7
+ self.configuration ||= Configuration.new
8
+ yield(configuration)
9
+ end
10
+
11
+ def self.restore_default_configuration
12
+ self.configuration = nil
13
+ configure {}
14
+ end
15
+
16
+ class Configuration
17
+ attr_accessor :primary_key
18
+ attr_accessor :foreign_key_suffix
19
+
20
+ def initialize
21
+ @primary_key = nil
22
+ @foreign_key_suffix = nil
23
+ end
24
+ end
25
+ end
@@ -1,20 +1,16 @@
1
1
  module Enumerations
2
2
  module FinderMethods
3
- # Finds an enumeration by symbol, id or name
3
+ # Finds an enumeration by symbol or name
4
4
  #
5
5
  # Example:
6
6
  #
7
7
  # Role.find(:admin) => #<Enumerations::Value: @base=Role, @symbol=:admin...>
8
- # Role.find(2) => #<Enumerations::Value: @base=Role, @symbol=:manager...>
9
- # Role.find('2') => #<Enumerations::Value: @base=Role, @symbol=:manager...>
10
8
  # Role.find('staff') => #<Enumerations::Value: @base=Role, @symbol=:staff...>
11
9
  #
12
10
  def find(key)
13
11
  case key
14
- when Symbol then find_by_key(key)
15
- when String then find_by_key(key.to_sym) || find_by_id(key.to_i)
16
- when Fixnum then find_by_id(key)
17
- end
12
+ when Symbol, String, Enumerations::Base then find_by_key(key.to_sym)
13
+ end || find_by_primary_key(key)
18
14
  end
19
15
 
20
16
  # Finds all enumerations which meets given attributes.
@@ -42,8 +38,14 @@ module Enumerations
42
38
  _values[key]
43
39
  end
44
40
 
45
- def find_by_id(id)
46
- _values[_symbol_index.key(id)]
41
+ def find_by_primary_key(primary_key)
42
+ value_from_symbol_index(primary_key) || value_from_symbol_index(primary_key.to_s.to_i)
43
+ end
44
+
45
+ private
46
+
47
+ def value_from_symbol_index(key)
48
+ _values[_symbol_index.key(key)]
47
49
  end
48
50
  end
49
51
  end
@@ -12,11 +12,17 @@ module Enumerations
12
12
  end
13
13
 
14
14
  def foreign_key
15
- @foreign_key ||= (@options[:foreign_key] || "#{name}_id").to_sym
15
+ @foreign_key ||= (@options[:foreign_key] || default_foreign_key_name).to_sym
16
16
  end
17
17
 
18
18
  def enumerator_class
19
19
  @enumerator_class ||= class_name.constantize
20
20
  end
21
+
22
+ private
23
+
24
+ def default_foreign_key_name
25
+ [name, Enumerations.configuration.foreign_key_suffix].compact.join('_')
26
+ end
21
27
  end
22
28
  end
@@ -1,36 +1,34 @@
1
1
  module Enumerations
2
2
  module Value
3
3
  def to_i
4
- id
4
+ _values.keys.index(symbol) + 1
5
5
  end
6
6
 
7
7
  def to_s
8
- name
8
+ symbol.to_s
9
9
  end
10
10
 
11
- def to_sym
12
- symbol
11
+ def to_param
12
+ to_s
13
13
  end
14
14
 
15
- def to_param
16
- id
15
+ def to_sym
16
+ symbol
17
17
  end
18
18
 
19
- # Comparison by id, symbol or object
19
+ # Comparison by symbol or object
20
20
  #
21
21
  # Example:
22
22
  #
23
- # Role.admin == 1 => true
24
23
  # Role.admin == :admin => true
25
24
  # Role.admin == Role.admin => true
26
- # Role.admin == 2 => false
27
25
  # Role.admin == :staff => false
28
26
  # Role.admin == Role.staff => false
29
27
  #
30
28
  # TODO: test if case..when is working with this
31
29
  def ==(other)
32
30
  case other
33
- when Fixnum then other == id
31
+ when String then other == to_s
34
32
  when Symbol then other == symbol
35
33
  else super
36
34
  end
@@ -49,8 +47,8 @@ module Enumerations
49
47
  #
50
48
  # Role.admin => #<Enumerations::Value:0x007fff45d7ec30 @base=Role, @symbol=:admin,
51
49
  # @attributes={:id=>1, :name=>"Admin", :description=>"Some description..."}>
52
- # user.role.id => # 1
53
- # user.role.name => # "Admin"
50
+ # user.role.id => # 1
51
+ # user.role.name => # "Admin"
54
52
  # user.role.description => # "Some description..."
55
53
  #
56
54
  def define_attributes_getters
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.1.0'
2
+ VERSION = '2.2.0'.freeze
3
3
  end
data/test/base_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class BaseTest < Minitest::Test
4
4
  def test_all
@@ -15,22 +15,9 @@ class BaseTest < Minitest::Test
15
15
  assert_equal status_symbols.first, Status.draft.to_sym
16
16
  end
17
17
 
18
- def test_required_id
19
- assert_raises 'Enumeration id is required' do
20
- Class.new.value draft: { name: 'Draft' }
21
- end
22
- end
23
-
24
- def test_duplicated_id
25
- assert_raises 'Duplicate id 1' do
26
- Class.new.values draft: { id: 1, name: 'Draft' },
27
- test: { id: 1, name: 'Draft' }
28
- end
29
- end
30
-
31
18
  def test_duplicated_symbol
32
19
  assert_raises 'Duplicate symbol draft' do
33
- obj = Class.new
20
+ obj = Class.new(Enumerations::Base)
34
21
 
35
22
  obj.value :draft, id: 1, name: 'Draft'
36
23
  obj.value :draft, id: 2, name: 'Draft Again'
@@ -0,0 +1,18 @@
1
+ module Configuration
2
+ Enumerations.configure do |config|
3
+ config.primary_key = :id
4
+ config.foreign_key_suffix = :id
5
+ end
6
+
7
+ ::CustomEnum = Class.new(Enumerations::Base)
8
+
9
+ CustomEnum.values draft: { id: 1, name: 'Draft' },
10
+ review: { id: 2, name: 'Review' },
11
+ published: { id: 3, name: 'Published', published: true }
12
+
13
+ ::CustomModel = Class.new(ActiveRecord::Base)
14
+
15
+ CustomModel.enumeration :custom_enum
16
+
17
+ Enumerations.restore_default_configuration
18
+ end
@@ -0,0 +1,51 @@
1
+ require_relative '../helpers/test_helper'
2
+ require_relative 'configuration'
3
+
4
+ module Configuration
5
+ class EnumerationsTest < Minitest::Test
6
+ def setup
7
+ Enumerations.configure do |config|
8
+ config.primary_key = :id
9
+ config.foreign_key_suffix = :id
10
+ end
11
+ end
12
+
13
+ def teardown
14
+ Enumerations.restore_default_configuration
15
+ end
16
+
17
+ def test_model_enumeration_assignment
18
+ model = CustomModel.new
19
+ model.custom_enum = CustomEnum.draft
20
+
21
+ assert_equal 'draft', model.custom_enum.to_s
22
+ end
23
+
24
+ def test_model_bang_assignment
25
+ model = CustomModel.new
26
+ model.custom_enum_draft!
27
+
28
+ assert_equal 'draft', model.custom_enum.to_s
29
+ end
30
+
31
+ def test_model_via_symbol_assignment
32
+ model = CustomModel.new
33
+ model.custom_enum = CustomEnum.published.symbol
34
+
35
+ assert_equal 'published', model.custom_enum.to_s
36
+ end
37
+
38
+ def test_model_via_foreign_key_assignment
39
+ model = CustomModel.new
40
+ model.custom_enum_id = CustomEnum.published.id
41
+
42
+ assert_equal 'published', model.custom_enum.to_s
43
+ end
44
+
45
+ def test_enumerated_class_scope_hash_value
46
+ query_hash = CustomModel.with_custom_enum_draft.where_values_hash.symbolize_keys
47
+
48
+ assert_equal query_hash, custom_enum_id: 1
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,41 @@
1
+ require_relative '../helpers/test_helper'
2
+ require_relative 'configuration'
3
+
4
+ module Configuration
5
+ class FinderTest < Minitest::Test
6
+ def setup
7
+ Enumerations.configure do |config|
8
+ config.primary_key = :id
9
+ config.foreign_key_suffix = :id
10
+ end
11
+ end
12
+
13
+ def teardown
14
+ Enumerations.restore_default_configuration
15
+ end
16
+
17
+ def test_lookup_by_key
18
+ enum = CustomEnum.find(:draft)
19
+
20
+ assert_equal :draft, enum.symbol
21
+ end
22
+
23
+ def test_lookup_by_string_key
24
+ enum = CustomEnum.find('draft')
25
+
26
+ assert_equal :draft, enum.symbol
27
+ end
28
+
29
+ def test_lookup_by_primary_key
30
+ enum = CustomEnum.find(1)
31
+
32
+ assert_equal :draft, enum.symbol
33
+ end
34
+
35
+ def test_lookup_by_primary_key_as_string
36
+ enum = CustomEnum.find('1')
37
+
38
+ assert_equal :draft, enum.symbol
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,53 @@
1
+ require_relative 'helpers/test_helper'
2
+
3
+ class ConfigurationTest < Minitest::Test
4
+ def setup
5
+ Enumerations.restore_default_configuration
6
+ end
7
+
8
+ def teardown
9
+ Enumerations.restore_default_configuration
10
+ end
11
+
12
+ def test_default_configuration
13
+ assert_equal nil, Enumerations.configuration.primary_key
14
+ assert_equal nil, Enumerations.configuration.foreign_key_suffix
15
+ end
16
+
17
+ def test_custom_configuration
18
+ Enumerations.configure do |config|
19
+ config.primary_key = :id
20
+ config.foreign_key_suffix = :id
21
+ end
22
+
23
+ assert_equal :id, Enumerations.configuration.primary_key
24
+ assert_equal :id, Enumerations.configuration.foreign_key_suffix
25
+ end
26
+
27
+ def test_reflection_with_configured_foreign_key_suffix
28
+ Enumerations.configure { |config| config.foreign_key_suffix = :id }
29
+
30
+ reflection = Enumerations::Reflection.new(:status)
31
+
32
+ assert_equal 'Status', reflection.class_name
33
+ assert_equal ::Status, reflection.enumerator_class
34
+ assert_equal :status_id, reflection.foreign_key
35
+ end
36
+
37
+ def test_required_primary_key_when_primary_key_configured
38
+ Enumerations.configure { |config| config.primary_key = :id }
39
+
40
+ assert_raises 'Enumeration primary key is required' do
41
+ Class.new(Enumerations::Base).value draft: { name: 'Draft' }
42
+ end
43
+ end
44
+
45
+ def test_duplicated_primary_key_when_primary_key_configured
46
+ Enumerations.configure { |config| config.primary_key = :id }
47
+
48
+ assert_raises 'Duplicate primary key 1' do
49
+ Class.new(Enumerations::Base).values draft: { id: 1, name: 'Draft' },
50
+ test: { id: 1, name: 'Draft' }
51
+ end
52
+ end
53
+ end
@@ -1,4 +1,4 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class EnumerationsTest < Minitest::Test
4
4
  def test_reflect_on_all_enumerations
@@ -8,35 +8,35 @@ class EnumerationsTest < Minitest::Test
8
8
  assert_equal :status, enumerations.first.name
9
9
  assert_equal 'Status', enumerations.first.class_name
10
10
 
11
- assert_equal :some_other_status_id, enumerations[1].foreign_key
11
+ assert_equal :some_other_status, enumerations[1].foreign_key
12
12
  end
13
13
 
14
14
  def test_model_enumeration_assignment
15
15
  p = Post.new
16
16
  p.status = Status.draft
17
17
 
18
- assert_equal 'Draft', p.status.to_s
18
+ assert_equal 'draft', p.status.to_s
19
19
  end
20
20
 
21
21
  def test_model_bang_assignment
22
22
  p = Post.new
23
23
  p.status_draft!
24
24
 
25
- assert_equal 'Draft', p.status.to_s
25
+ assert_equal 'draft', p.status.to_s
26
26
  end
27
27
 
28
28
  def test_model_bang_assignment_with_custom_name
29
29
  p = Post.new
30
30
  p.different_status_draft!
31
31
 
32
- assert_equal 'Draft', p.different_status.to_s
32
+ assert_equal 'draft', p.different_status.to_s
33
33
  end
34
34
 
35
- def test_model_via_id_assignment
35
+ def test_model_via_symbol_assignment
36
36
  p = Post.new
37
- p.some_other_status_id = Status.published.id
37
+ p.some_other_status = Status.published.symbol
38
38
 
39
- assert_equal 'Published', p.different_status.to_s
39
+ assert_equal 'published', p.some_other_status.to_s
40
40
  end
41
41
 
42
42
  def test_boolean_lookup
@@ -59,10 +59,10 @@ class EnumerationsTest < Minitest::Test
59
59
  assert_equal 2, enumerations.size
60
60
 
61
61
  assert_equal :role, enumerations.first.name
62
- assert_equal :role_id, enumerations.first.foreign_key
62
+ assert_equal :role, enumerations.first.foreign_key
63
63
 
64
64
  assert_equal :status, enumerations[1].name
65
- assert_equal :status_id, enumerations[1].foreign_key
65
+ assert_equal :status, enumerations[1].foreign_key
66
66
  end
67
67
 
68
68
  def test_multiple_enumeration_assignments_on_model
@@ -70,8 +70,8 @@ class EnumerationsTest < Minitest::Test
70
70
  u.role = Role.admin
71
71
  u.status = Status.published
72
72
 
73
- assert_equal 'Admin', u.role.to_s
74
- assert_equal 'Published', u.status.to_s
73
+ assert_equal 'admin', u.role.to_s
74
+ assert_equal 'published', u.status.to_s
75
75
  end
76
76
 
77
77
  def test_enumerated_class_has_scopes
@@ -83,6 +83,6 @@ class EnumerationsTest < Minitest::Test
83
83
  def test_enumerated_class_scope_hash_value
84
84
  query_hash = User.with_role_admin.where_values_hash.symbolize_keys
85
85
 
86
- assert_equal query_hash, role_id: 1
86
+ assert_equal query_hash, role: :admin
87
87
  end
88
88
  end
data/test/finder_test.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class FinderTest < Minitest::Test
4
4
  def test_lookup_by_symbol
@@ -13,12 +13,6 @@ class FinderTest < Minitest::Test
13
13
  refute_same :published, status.symbol
14
14
  end
15
15
 
16
- def test_lookup_by_string_id
17
- status = Status.find('1')
18
-
19
- assert_equal :draft, status.symbol
20
- end
21
-
22
16
  def test_lookup_by_string_key
23
17
  status = Status.find('draft')
24
18
 
@@ -4,12 +4,17 @@ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:'
4
4
 
5
5
  ActiveRecord::Schema.define do
6
6
  create_table :posts, force: true do |t|
7
- t.integer :status_id
8
- t.integer :some_other_status_id
7
+ t.string :status
8
+ t.string :some_other_status
9
9
  end
10
10
 
11
11
  create_table :users, force: true do |t|
12
- t.integer :role_id
13
- t.string :status_id
12
+ t.string :role
13
+ t.string :status
14
+ end
15
+
16
+ create_table :custom_models, force: true do |t|
17
+ t.integer :custom_enum_id
18
+ t.integer :custom_enum
14
19
  end
15
20
  end
@@ -6,8 +6,10 @@ require 'enumerations'
6
6
  require 'active_record'
7
7
  require 'pry'
8
8
 
9
- require_relative 'helpers/database_helper'
10
- require_relative 'helpers/locale_helper'
9
+ require_relative 'database_helper'
10
+ require_relative 'locale_helper'
11
+
12
+ Enumerations.configure { |_| }
11
13
 
12
14
  class Status < Enumerations::Base
13
15
  values draft: { id: 1, name: 'Draft' },
@@ -19,9 +21,9 @@ class Status < Enumerations::Base
19
21
  end
20
22
 
21
23
  class Role < Enumerations::Base
22
- value :admin, id: 1, name: 'Admin', admin: true, active: true
23
- value :editor, id: 2, name: 'Editor', admin: true, active: false, description: 'Edits newspapers'
24
- value :author, id: 3, name: 'Author'
24
+ value :admin, name: 'Admin', admin: true, active: true
25
+ value :editor, name: 'Editor', admin: true, active: false, description: 'Edits newspapers'
26
+ value :author, name: 'Author'
25
27
 
26
28
  def my_custom_name
27
29
  ['user', name].join('_')
@@ -29,15 +31,11 @@ class Role < Enumerations::Base
29
31
  end
30
32
 
31
33
  class Post < ActiveRecord::Base
32
- attr_accessor :status_id, :some_other_status_id
33
-
34
34
  enumeration :status
35
- enumeration :different_status, foreign_key: :some_other_status_id, class_name: 'Status'
35
+ enumeration :different_status, foreign_key: :some_other_status, class_name: 'Status'
36
36
  end
37
37
 
38
38
  class User < ActiveRecord::Base
39
- attr_accessor :role_id, :status_id
40
-
41
39
  enumeration :role
42
40
  enumeration :status
43
41
  end
@@ -1,32 +1,32 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class ReflectionTest < Minitest::Test
4
4
  def test_reflection_with_all_attributes
5
5
  reflection = Enumerations::Reflection.new(:status, class_name: 'Status',
6
- foreign_key: :status_id)
6
+ foreign_key: :status)
7
7
 
8
- assert_equal :status, reflection.name
8
+ assert_equal :status, reflection.name
9
9
  assert_equal 'Status', reflection.class_name
10
- assert_equal :status_id, reflection.foreign_key
10
+ assert_equal :status, reflection.foreign_key
11
11
  assert_equal ::Status, reflection.enumerator_class
12
12
  end
13
13
 
14
14
  def test_reflection_without_class_name_and_foreign_key
15
15
  reflection = Enumerations::Reflection.new(:status)
16
16
 
17
- assert_equal :status, reflection.name
18
- assert_equal 'Status', reflection.class_name
19
- assert_equal :status_id, reflection.foreign_key
20
- assert_equal ::Status, reflection.enumerator_class
17
+ assert_equal :status, reflection.name
18
+ assert_equal 'Status', reflection.class_name
19
+ assert_equal :status, reflection.foreign_key
20
+ assert_equal ::Status, reflection.enumerator_class
21
21
  end
22
22
 
23
23
  def test_reflection_with_custom_name_and_without_foreign_key
24
24
  reflection = Enumerations::Reflection.new(:my_status, class_name: 'Status')
25
25
 
26
- assert_equal :my_status, reflection.name
27
- assert_equal 'Status', reflection.class_name
28
- assert_equal :my_status_id, reflection.foreign_key
29
- assert_equal ::Status, reflection.enumerator_class
26
+ assert_equal :my_status, reflection.name
27
+ assert_equal 'Status', reflection.class_name
28
+ assert_equal :my_status, reflection.foreign_key
29
+ assert_equal ::Status, reflection.enumerator_class
30
30
  end
31
31
 
32
32
  def test_reflection_with_class_name_as_constant
@@ -1,4 +1,4 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class TranslationTest < Minitest::Test
4
4
  def test_translated_value_with_default_locale
data/test/value_test.rb CHANGED
@@ -1,16 +1,16 @@
1
- require_relative 'test_helper'
1
+ require_relative 'helpers/test_helper'
2
2
 
3
3
  class ValueTest < Minitest::Test
4
- def test_equal_by_id
5
- status = Status.find(:draft)
4
+ def test_equal_by_symbol
5
+ status = Status.draft
6
6
 
7
- assert_equal true, status == 1
7
+ assert_equal true, status == :draft
8
8
  end
9
9
 
10
- def test_equal_by_symbol
10
+ def test_equal_by_string
11
11
  status = Status.draft
12
12
 
13
- assert_equal true, status == :draft
13
+ assert_equal true, status == 'draft'
14
14
  end
15
15
 
16
16
  def test_equal_by_enumeration
@@ -43,10 +43,12 @@ class ValueTest < Minitest::Test
43
43
  assert_equal nil, status.visible
44
44
  end
45
45
 
46
- def test_enumeration_to_i
47
- status = Status.find(:draft)
46
+ def test_enumeration_to_i_return_ordinal
47
+ first_status = Status.find(:draft)
48
+ second_status = Status.find(:review_pending)
48
49
 
49
- assert_equal status.to_i, 1
50
+ assert_equal first_status.to_i, 1
51
+ assert_equal second_status.to_i, 2
50
52
  end
51
53
 
52
54
  def test_enumeration_to_sym
@@ -58,6 +60,6 @@ class ValueTest < Minitest::Test
58
60
  def test_enumeration_to_param
59
61
  status = Status.find(:draft)
60
62
 
61
- assert_equal status.to_param, 1
63
+ assert_equal status.to_param, 'draft'
62
64
  end
63
65
  end
metadata CHANGED
@@ -1,16 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomislav Car
8
- - Nikica Jokic
9
- - Nikola Santic
8
+ - Nikica Jokić
9
+ - Nikola Santić
10
+ - Stjepan Hadjić
11
+ - Petar Ćurković
10
12
  autorequire:
11
13
  bindir: bin
12
14
  cert_chain: []
13
- date: 2016-08-15 00:00:00.000000000 Z
15
+ date: 2016-09-15 00:00:00.000000000 Z
14
16
  dependencies:
15
17
  - !ruby/object:Gem::Dependency
16
18
  name: activerecord
@@ -115,6 +117,8 @@ email:
115
117
  - tomislav@infinum.hr
116
118
  - nikica.jokic@infinum.hr
117
119
  - nikola.santic@infinum.hr
120
+ - stjepan.hadjic@infinum.hr
121
+ - petar.curkovic@infinum.hr
118
122
  executables: []
119
123
  extensions: []
120
124
  extra_rdoc_files: []
@@ -126,21 +130,25 @@ files:
126
130
  - enumerations.gemspec
127
131
  - lib/enumerations.rb
128
132
  - lib/enumerations/base.rb
133
+ - lib/enumerations/configuration.rb
129
134
  - lib/enumerations/finder_methods.rb
130
135
  - lib/enumerations/reflection.rb
131
136
  - lib/enumerations/value.rb
132
137
  - lib/enumerations/version.rb
133
138
  - test/base_test.rb
139
+ - test/configuration/configuration.rb
140
+ - test/configuration/enumerations_test.rb
141
+ - test/configuration/finder_test.rb
142
+ - test/configuration_test.rb
134
143
  - test/enumerations_test.rb
135
144
  - test/finder_test.rb
136
145
  - test/helpers/database_helper.rb
137
146
  - test/helpers/locale_helper.rb
147
+ - test/helpers/test_helper.rb
138
148
  - test/locales/hr.yml
139
149
  - test/reflection_test.rb
140
- - test/test_helper.rb
141
150
  - test/translation_test.rb
142
151
  - test/value_test.rb
143
- - test/version_test.rb
144
152
  homepage: https://github.com/infinum/enumerations
145
153
  licenses: []
146
154
  metadata: {}
@@ -166,13 +174,16 @@ specification_version: 4
166
174
  summary: Enumerations for ActiveRecord!
167
175
  test_files:
168
176
  - test/base_test.rb
177
+ - test/configuration/configuration.rb
178
+ - test/configuration/enumerations_test.rb
179
+ - test/configuration/finder_test.rb
180
+ - test/configuration_test.rb
169
181
  - test/enumerations_test.rb
170
182
  - test/finder_test.rb
171
183
  - test/helpers/database_helper.rb
172
184
  - test/helpers/locale_helper.rb
185
+ - test/helpers/test_helper.rb
173
186
  - test/locales/hr.yml
174
187
  - test/reflection_test.rb
175
- - test/test_helper.rb
176
188
  - test/translation_test.rb
177
189
  - test/value_test.rb
178
- - test/version_test.rb
data/test/version_test.rb DELETED
@@ -1,7 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class VersionTest < Minitest::Test
4
- def test_version
5
- assert_equal '2.1.0', Enumerations::VERSION
6
- end
7
- end