enumerations 2.5.4 → 2.6.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: 96fda6cfaff4da9dfd29920b976700cb73ac86717712db80e65f255188ee5c42
4
- data.tar.gz: 2bc62be14144f04d162423d9d36f9949e397f6a325464d4e4fd5488e16688b83
3
+ metadata.gz: 2d4e5a6f8e18a9d70f988c555ea97c303467d965a1cf55a9e02f0406ddebdb85
4
+ data.tar.gz: 7cdec6ecd8e4ad23732ea23a5b41185b2ad1deb6cd44cedc96d73f2a28136925
5
5
  SHA512:
6
- metadata.gz: fd955f210f7747cfd692f7ee2f333e66aeca3a280ab20d1eee9fb96c9287505f95b438cc0576339b03ac171d7abbb362bef81de212bad37acb799f14a256d13c
7
- data.tar.gz: c9b384db06f64b3b10e335416fbb7855949d0d859273776de100865ee5ed858d7efc80ff9daf69b827eaecd40831dc372014203bc9b82fa95215957c4fb357b0
6
+ metadata.gz: 29193d63c19d102f702ecdbe16f9c1031cdd43a6a33768c977992749b6a722c7b3aaaa1e6011786c5bd9be7d20543b37b6611d930d1a205119984a9a91d6f2cc
7
+ data.tar.gz: 20b7ed5790969bb7b9ef820cc9ff89968cfef42560e46f3dbbc83b765a48bd4cef82b1d0092496ea7709db92aa4e0749d05d8b516b2d20a591807982f8c31f51
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Change Log
2
2
 
3
+ ## [v2.6.0](https://github.com/infinum/enumerations/tree/v2.6.0) (2024-10-22)
4
+ [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.4...v2.6.0)
5
+
6
+ **Implemented enhancements:**
7
+
8
+ - Add ability to specify foreign key suffix on enum class.
9
+
3
10
  ## [v2.5.4](https://github.com/infinum/enumerations/tree/v2.5.4) (2023-01-09)
4
11
  [Full Changelog](https://github.com/infinum/enumerations/compare/v2.5.3...v2.5.4)
5
12
 
data/README.md CHANGED
@@ -379,11 +379,12 @@ post = Post.new
379
379
  post.status = Status.draft # => post.status_id = 1
380
380
  ```
381
381
 
382
- If you want to configure primary key per enumeration class, you can use `primary_key=` class method:
382
+ If you want to configure primary key or foreign key suffix per enumeration class, you can use `primary_key=` and `foreign_key_suffix=` class method:
383
383
 
384
384
  ```ruby
385
385
  class Status < Enumerations::Base
386
386
  self.primary_key = :id
387
+ self.foreign_key_suffix = :id
387
388
 
388
389
  value :draft, id: 1, name: 'Draft'
389
390
  value :review_pending, id: 2, name: 'Review pending'
data/enumerations.gemspec CHANGED
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
24
24
  s.add_development_dependency 'pry-byebug'
25
25
  s.add_development_dependency 'rake'
26
26
  s.add_development_dependency 'simplecov'
27
- s.add_development_dependency 'sqlite3', '~> 1.4.0'
27
+ s.add_development_dependency 'sqlite3'
28
28
 
29
29
  s.files = `git ls-files`.split("\n")
30
30
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -8,11 +8,12 @@ module Enumerations
8
8
  extend Enumerations::FinderMethods
9
9
  include Enumerations::Value
10
10
 
11
- class_attribute :_values, :_symbol_index, :_primary_key
11
+ class_attribute :_values, :_symbol_index, :_primary_key, :_foreign_key_suffix
12
12
 
13
13
  self._values = {}
14
14
  self._symbol_index = {}
15
15
  self._primary_key = nil
16
+ self._foreign_key_suffix = nil
16
17
 
17
18
  # Adding new value to enumeration
18
19
  #
@@ -108,6 +109,34 @@ module Enumerations
108
109
  _primary_key || Enumerations.configuration.primary_key
109
110
  end
110
111
 
112
+ # Sets foreign key suffix for enumeration class.
113
+ #
114
+ # Example:
115
+ #
116
+ # class Status < Enumeration::Base
117
+ # foreign_key_suffix = :id
118
+ # end
119
+
120
+ def self.foreign_key_suffix=(suffix)
121
+ self._foreign_key_suffix = suffix && suffix.to_sym
122
+ end
123
+
124
+ # Gets foreign key suffix for enumeration class.
125
+ #
126
+ # Example:
127
+ #
128
+ # Status.foreign_key_suffix => # nil
129
+ #
130
+ # class Status < Enumeration::Base
131
+ # foreign_key_suffix = :id
132
+ # end
133
+ #
134
+ # Status.foreign_key_suffix => # :id
135
+ #
136
+ def self.foreign_key_suffix
137
+ _foreign_key_suffix || Enumerations.configuration.foreign_key_suffix
138
+ end
139
+
111
140
  def self.validate_symbol_and_primary_key(symbol, attributes)
112
141
  raise Enumerations::DuplicatedSymbolError if find(symbol)
113
142
 
@@ -22,7 +22,7 @@ module Enumerations
22
22
  private
23
23
 
24
24
  def default_foreign_key_name
25
- [name, Enumerations.configuration.foreign_key_suffix].compact.join('_')
25
+ [name, enumerator_class.foreign_key_suffix].compact.join('_')
26
26
  end
27
27
  end
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module Enumerations
2
- VERSION = '2.5.4'.freeze
2
+ VERSION = '2.6.0'.freeze
3
3
  end
@@ -47,5 +47,15 @@ module Configuration
47
47
 
48
48
  assert_equal query_hash, custom_enum_id: 1
49
49
  end
50
+
51
+ def test_enumerations_overrides
52
+ assert_equal :id, OverridableStatus.primary_key
53
+ assert_equal :id, OverridableStatus.foreign_key_suffix
54
+
55
+ model = OverridableModel.new
56
+ model.overridable_status = OverridableStatus.draft
57
+
58
+ assert_equal 1, model.overridable_status_id
59
+ end
50
60
  end
51
61
  end
@@ -17,4 +17,8 @@ ActiveRecord::Schema.define do
17
17
  t.integer :custom_enum_id
18
18
  t.integer :custom_enum
19
19
  end
20
+
21
+ create_table :overridable_models, force: true do |t|
22
+ t.integer :overridable_status_id
23
+ end
20
24
  end
@@ -40,3 +40,14 @@ class User < ActiveRecord::Base
40
40
  enumeration :role
41
41
  enumeration :status
42
42
  end
43
+
44
+ class OverridableStatus < Enumerations::Base
45
+ self.primary_key = :id
46
+ self.foreign_key_suffix = :id
47
+
48
+ value :draft, id: 1
49
+ end
50
+
51
+ class OverridableModel < ActiveRecord::Base
52
+ enumeration :overridable_status
53
+ end
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.5.4
4
+ version: 2.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomislav Car
@@ -9,7 +9,7 @@ authors:
9
9
  - Nikola Santić
10
10
  - Stjepan Hadjić
11
11
  - Petar Ćurković
12
- autorequire:
12
+ autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
15
  date: 2017-09-08 00:00:00.000000000 Z
@@ -102,16 +102,16 @@ dependencies:
102
102
  name: sqlite3
103
103
  requirement: !ruby/object:Gem::Requirement
104
104
  requirements:
105
- - - "~>"
105
+ - - ">="
106
106
  - !ruby/object:Gem::Version
107
- version: 1.4.0
107
+ version: '0'
108
108
  type: :development
109
109
  prerelease: false
110
110
  version_requirements: !ruby/object:Gem::Requirement
111
111
  requirements:
112
- - - "~>"
112
+ - - ">="
113
113
  - !ruby/object:Gem::Version
114
- version: 1.4.0
114
+ version: '0'
115
115
  description: Extends ActiveRecord with enumeration capabilites.
116
116
  email:
117
117
  - tomislav@infinum.hr
@@ -160,7 +160,7 @@ homepage: https://github.com/infinum/enumerations
160
160
  licenses:
161
161
  - MIT
162
162
  metadata: {}
163
- post_install_message:
163
+ post_install_message:
164
164
  rdoc_options: []
165
165
  require_paths:
166
166
  - lib
@@ -175,23 +175,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
175
175
  - !ruby/object:Gem::Version
176
176
  version: '0'
177
177
  requirements: []
178
- rubygems_version: 3.4.5
179
- signing_key:
178
+ rubygems_version: 3.5.15
179
+ signing_key:
180
180
  specification_version: 4
181
181
  summary: Enumerations for ActiveRecord!
182
- test_files:
183
- - test/base_test.rb
184
- - test/configuration/configuration.rb
185
- - test/configuration/custom_configuration_test.rb
186
- - test/configuration/enumerations_test.rb
187
- - test/configuration/finder_test.rb
188
- - test/configuration_test.rb
189
- - test/enumerations_test.rb
190
- - test/finder_test.rb
191
- - test/helpers/database_helper.rb
192
- - test/helpers/locale_helper.rb
193
- - test/helpers/test_helper.rb
194
- - test/locales/hr.yml
195
- - test/reflection_test.rb
196
- - test/translation_test.rb
197
- - test/value_test.rb
182
+ test_files: []