enumerations 2.2.1 → 2.2.2

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: 6d3706af5a24b06cfd32e5313ae93aed2d141e1c
4
- data.tar.gz: 84e6d7f1536f2fda8bf380dfb8aefd93251bf7fe
3
+ metadata.gz: 2df0679b8e64a8f5308716c2267da923343c3e25
4
+ data.tar.gz: 94e557e82f4a8e4b0b14c3c353e1a35c59041253
5
5
  SHA512:
6
- metadata.gz: a33613627715d0897356efcef395ba8a9aaaa037a09c10164304c4fa852521c41f11d650cb3d99caa11e96ee41849c750e490fa154516177b23d231d93d61886
7
- data.tar.gz: 8c79d5499ace432b42be7ba39a0ddac3a425dd23fd1c7da6401340bc6571e0bb13c8a673c4a1899ad98857ab861318753ed9ae9f8b747b7f4c0fc42aa17aaf50
6
+ metadata.gz: 487e7348405aa95e9cdbc1fa481b8bcf792aa991bcd34b0eebc4bdded31ccca85ec9d517b2fec77c9f133948f068e8d85095d832881c1203fe135515b6b8c9d5
7
+ data.tar.gz: 570bf8b1f44b99ec33c194dd9fc68b40c7cf3f6cf6e154e2d38d1229fc596c817dfdbc8a3ab065e7cf5ee6c60f1e91997f8a536e68587a01a5a9a64fe770e220
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  *.gem
3
3
  Gemfile.lock
4
4
  .todo
5
+ coverage
@@ -0,0 +1,99 @@
1
+ How to migrate from 1.x.x
2
+ =========================
3
+
4
+ Changes in the namespace
5
+ ========================
6
+
7
+ First off, start by renaming your namespaces, what used to be
8
+
9
+ ```ruby
10
+ class MyClass < Enumeration::Base
11
+ ```
12
+
13
+ will now be
14
+
15
+ ```ruby
16
+ class MyClass < Enumerations::Base
17
+ ```
18
+
19
+ Configuration
20
+ =============
21
+
22
+ Since v2 has a breaking change of default values in the database columns, you have 2 options:
23
+
24
+ 1. Create an initializer that you will configure to have the old naming conventions
25
+ 2. Create data migrations and convert your data to be alligned with the new Enumerations
26
+
27
+ First option might be tempting, but you need to weigh in the advantages of the new enumerations.
28
+ With the second option, you can get enumerations in the database instead of ID's, which may prove quite usefull.
29
+
30
+ First way
31
+ =========
32
+
33
+ You need to create the following initializer
34
+
35
+ ```ruby
36
+ # config/initializers/enumerations.rb
37
+
38
+ Enumerations.configure do |config|
39
+ config.primary_key = :id
40
+ config.foreign_key_suffix = :id
41
+ end
42
+ ```
43
+
44
+ And voilà! You can carry on with work knowing your enumerations are up to date.
45
+
46
+ Second way
47
+ ==========
48
+
49
+ You can use a gem for the migration, or just write raw SQL.
50
+
51
+ Your migrations will look something like this:
52
+
53
+ First to change the column types
54
+ ```ruby
55
+ class MoveMyEnumToNewEnumerationsColumns
56
+ def up
57
+ rename_column :my_table, :my_enum_id, :my_enum
58
+ change_column :my_table, :my_enum, :string
59
+ end
60
+
61
+ def down
62
+ change_column :my_table, :my_enum, 'integer USING CAST(my_enum AS integer)'
63
+ rename_column :my_table, :my_enum, :my_enum_id
64
+ end
65
+ end
66
+ ```
67
+
68
+ And now for the actual data
69
+
70
+ ```ruby
71
+ class MoveMyEnumToNewEnumerations < ActiveRecord::Migration
72
+ def up
73
+ execute(<<-SQL
74
+ UPDATE my_table
75
+ SET my_enum =
76
+ CASE my_enum
77
+ WHEN '1' THEN 'first'
78
+ WHEN '2' THEN 'second'
79
+ WHEN '3' THEN 'third'
80
+ END
81
+ SQL
82
+ )
83
+ end
84
+
85
+ def down
86
+ execute(<<-SQL
87
+ UPDATE my_table
88
+ SET my_enum =
89
+ CASE my_enum
90
+ WHEN 'first' THEN '1'
91
+ WHEN 'second' THEN '2'
92
+ WHEN 'third' THEN '3'
93
+ END
94
+ SQL
95
+ )
96
+ end
97
+ ```
98
+
99
+ And that's it, you won't need an initializer for this to work anymore, because these are the default options Enumeration gem ships with.
data/Readme.md CHANGED
@@ -222,6 +222,7 @@ class Status < Enumerations::Base
222
222
  value :draft, id: 1, name: 'Draft'
223
223
  value :review_pending, id: 2, name: 'Review pending', description: 'Some description...'
224
224
  value :published, id: 3, name: 'Published', published: true
225
+ value :other # passing no attributes is also allowed
225
226
  end
226
227
  ```
227
228
 
@@ -22,7 +22,7 @@ Gem::Specification.new do |s|
22
22
  s.add_dependency 'i18n'
23
23
  s.add_development_dependency 'pry-byebug'
24
24
  s.add_development_dependency 'rake'
25
- s.add_development_dependency 'codeclimate-test-reporter'
25
+ s.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
26
26
  s.add_development_dependency 'sqlite3'
27
27
 
28
28
  s.files = `git ls-files`.split("\n")
@@ -7,6 +7,7 @@ require 'enumerations/configuration'
7
7
  require 'enumerations/version'
8
8
  require 'enumerations/base'
9
9
  require 'enumerations/reflection'
10
+ require 'enumerations/enumerations_error'
10
11
 
11
12
  module Enumerations
12
13
  extend ActiveSupport::Concern
@@ -86,6 +87,7 @@ module Enumerations
86
87
  enumeration_value = reflection.enumerator_class.find(other)
87
88
 
88
89
  self[reflection.foreign_key] =
90
+ enumeration_value &&
89
91
  enumeration_value.send(Enumerations.configuration.primary_key || :symbol)
90
92
  end
91
93
  end
@@ -22,7 +22,7 @@ module Enumerations
22
22
  # Role.find(:admin).name => # "Admin"
23
23
  # Role.find(:admin).description => # "Some description..."
24
24
  #
25
- def self.value(symbol, attributes)
25
+ def self.value(symbol, attributes = {})
26
26
  validate_symbol_and_primary_key(symbol, attributes)
27
27
 
28
28
  self._values = _values.merge(symbol => new(symbol, attributes))
@@ -80,13 +80,13 @@ module Enumerations
80
80
  end
81
81
 
82
82
  def self.validate_symbol_and_primary_key(symbol, attributes)
83
- raise "Duplicate symbol #{symbol}" if find(symbol)
83
+ raise EnumerationsError, "Duplicate symbol #{symbol}" if find(symbol)
84
84
 
85
85
  primary_key = Enumerations.configuration.primary_key
86
86
  return if primary_key.nil?
87
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])
88
+ raise EnumerationsError, 'Enumeration primary key is required' if attributes[primary_key].nil?
89
+ raise EnumerationsError, "Duplicate primary key #{attributes[primary_key]}" if find(attributes[primary_key])
90
90
 
91
91
  self._symbol_index = _symbol_index.merge(symbol => attributes[primary_key])
92
92
  end
@@ -0,0 +1,2 @@
1
+ class EnumerationsError < RuntimeError
2
+ end
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.2.1'.freeze
2
+ VERSION = '2.2.2'.freeze
3
3
  end
@@ -16,7 +16,7 @@ class BaseTest < Minitest::Test
16
16
  end
17
17
 
18
18
  def test_duplicated_symbol
19
- assert_raises 'Duplicate symbol draft' do
19
+ assert_raises EnumerationsError, 'Duplicate symbol draft' do
20
20
  obj = Class.new(Enumerations::Base)
21
21
 
22
22
  obj.value :draft, id: 1, name: 'Draft'
@@ -10,8 +10,8 @@ class ConfigurationTest < Minitest::Test
10
10
  end
11
11
 
12
12
  def test_default_configuration
13
- assert_equal nil, Enumerations.configuration.primary_key
14
- assert_equal nil, Enumerations.configuration.foreign_key_suffix
13
+ assert_nil Enumerations.configuration.primary_key
14
+ assert_nil Enumerations.configuration.foreign_key_suffix
15
15
  end
16
16
 
17
17
  def test_custom_configuration
@@ -37,15 +37,15 @@ class ConfigurationTest < Minitest::Test
37
37
  def test_required_primary_key_when_primary_key_configured
38
38
  Enumerations.configure { |config| config.primary_key = :id }
39
39
 
40
- assert_raises 'Enumeration primary key is required' do
41
- Class.new(Enumerations::Base).value draft: { name: 'Draft' }
40
+ assert_raises EnumerationsError, 'Enumeration primary key is required' do
41
+ Class.new(Enumerations::Base).value :draft, name: 'Draft'
42
42
  end
43
43
  end
44
44
 
45
45
  def test_duplicated_primary_key_when_primary_key_configured
46
46
  Enumerations.configure { |config| config.primary_key = :id }
47
47
 
48
- assert_raises 'Duplicate primary key 1' do
48
+ assert_raises EnumerationsError, 'Duplicate primary key 1' do
49
49
  Class.new(Enumerations::Base).values draft: { id: 1, name: 'Draft' },
50
50
  test: { id: 1, name: 'Draft' }
51
51
  end
@@ -1,4 +1,5 @@
1
1
  require_relative 'helpers/test_helper'
2
+ require 'enumerations/enumerations_error'
2
3
 
3
4
  class EnumerationsTest < Minitest::Test
4
5
  def test_reflect_on_all_enumerations
@@ -75,8 +76,8 @@ class EnumerationsTest < Minitest::Test
75
76
  end
76
77
 
77
78
  def test_enumerated_class_has_scopes
78
- Role.all do |role|
79
- assert_respond_to User, ['with_role', role.name].join('_').to_sym
79
+ Role.all.each do |role|
80
+ assert_respond_to User, ['with_role', role.to_s].join('_').to_sym
80
81
  end
81
82
  end
82
83
 
@@ -85,4 +86,14 @@ class EnumerationsTest < Minitest::Test
85
86
 
86
87
  assert_equal query_hash, role: :admin
87
88
  end
89
+
90
+ def test_nonexistent_value_assignment
91
+ user = User.new(role: :nonexistent_value)
92
+ assert_nil user.role
93
+ end
94
+
95
+ def test_on_nil_value_assignment
96
+ user = User.new(role: nil)
97
+ assert_nil user.role
98
+ end
88
99
  end
@@ -28,7 +28,7 @@ class FinderTest < Minitest::Test
28
28
  def test_fail_find_by
29
29
  status = Status.find_by(name: 'Draft1')
30
30
 
31
- assert_equal nil, status
31
+ assert_nil status
32
32
  end
33
33
 
34
34
  def test_where_by_name
@@ -1,5 +1,5 @@
1
- require 'codeclimate-test-reporter'
2
- CodeClimate::TestReporter.start
1
+ require 'simplecov'
2
+ SimpleCov.start
3
3
 
4
4
  require 'minitest/autorun'
5
5
  require 'enumerations'
@@ -22,6 +22,7 @@ class Role < Enumerations::Base
22
22
  value :admin, name: 'Admin', admin: true, active: true
23
23
  value :editor, name: 'Editor', admin: true, active: false, description: 'Edits newspapers'
24
24
  value :author, name: 'Author'
25
+ value :lecturer
25
26
 
26
27
  def my_custom_name
27
28
  ['user', name].join('_')
@@ -69,13 +69,13 @@ class TranslationTest < Minitest::Test
69
69
  def test_empty_custom_attribute_with_custom_locale
70
70
  role = Role.find(:admin)
71
71
 
72
- assert_equal nil, role.description(locale: :hr)
72
+ assert_nil role.description(locale: :hr)
73
73
  end
74
74
 
75
75
  def test_empty_custom_attribute_with_custom_locale_and_defined_translation
76
76
  role = Role.find(:author)
77
77
 
78
- assert_equal nil, role.description(locale: :hr)
78
+ assert_nil role.description(locale: :hr)
79
79
  end
80
80
 
81
81
  def test_find_by_localized_name
@@ -89,6 +89,6 @@ class TranslationTest < Minitest::Test
89
89
  status = Status.find_by(name: 'Nacrt')
90
90
  I18n.locale = :en
91
91
 
92
- assert_equal nil, status
92
+ assert_nil status
93
93
  end
94
94
  end
@@ -40,7 +40,7 @@ class ValueTest < Minitest::Test
40
40
  def test_without_defined_custom_attributes
41
41
  status = Status.find(:draft)
42
42
 
43
- assert_equal nil, status.visible
43
+ assert_nil status.visible
44
44
  end
45
45
 
46
46
  def test_enumeration_to_i_return_ordinal
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enumerations
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomislav Car
@@ -88,16 +88,16 @@ dependencies:
88
88
  name: codeclimate-test-reporter
89
89
  requirement: !ruby/object:Gem::Requirement
90
90
  requirements:
91
- - - ">="
91
+ - - "~>"
92
92
  - !ruby/object:Gem::Version
93
- version: '0'
93
+ version: '1.0'
94
94
  type: :development
95
95
  prerelease: false
96
96
  version_requirements: !ruby/object:Gem::Requirement
97
97
  requirements:
98
- - - ">="
98
+ - - "~>"
99
99
  - !ruby/object:Gem::Version
100
- version: '0'
100
+ version: '1.0'
101
101
  - !ruby/object:Gem::Dependency
102
102
  name: sqlite3
103
103
  requirement: !ruby/object:Gem::Requirement
@@ -125,12 +125,14 @@ extra_rdoc_files: []
125
125
  files:
126
126
  - ".gitignore"
127
127
  - Gemfile
128
+ - Migrate.md
128
129
  - Rakefile
129
130
  - Readme.md
130
131
  - enumerations.gemspec
131
132
  - lib/enumerations.rb
132
133
  - lib/enumerations/base.rb
133
134
  - lib/enumerations/configuration.rb
135
+ - lib/enumerations/enumerations_error.rb
134
136
  - lib/enumerations/finder_methods.rb
135
137
  - lib/enumerations/reflection.rb
136
138
  - lib/enumerations/value.rb