jt-rails-enum 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/jt-rails-enum.gemspec +1 -1
- data/lib/jt-rails-enum.rb +28 -17
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dcc16a272ed8f7564c3b0889155b20c049c1d2bb
|
4
|
+
data.tar.gz: 45e1a082c8c1aa99826bc69e3721b80c9dddd6ab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 942354ccbbaa5a671294779add44d779b0f261dca8c42dd179a75846faac6aa210b238669dae922c43bb23ed7c27f94d868f2136ae3eef91d5040adcb639c2e5
|
7
|
+
data.tar.gz: 51b5383f617d89a5c7df8856a50bd136ab60a035820c63d3b770280f38747d69cf2ba992c8086987e8e0e9624eca1c465580ad57f00bcd4db82dbd67dc76deb6
|
data/jt-rails-enum.gemspec
CHANGED
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
|
|
3
3
|
s.summary = "JTRailsEnum let you use enum in your models"
|
4
4
|
s.description = "JTRailsEnum let you use enum in your models"
|
5
5
|
s.homepage = 'https://github.com/jonathantribouharet/jt-rails-enum'
|
6
|
-
s.version = '1.0.
|
6
|
+
s.version = '1.0.2'
|
7
7
|
s.files = `git ls-files`.split("\n")
|
8
8
|
s.require_paths = ['lib']
|
9
9
|
s.authors = ['Jonathan TRIBOUHARET']
|
data/lib/jt-rails-enum.rb
CHANGED
@@ -1,37 +1,48 @@
|
|
1
1
|
module JT
|
2
2
|
module Rails
|
3
3
|
module Enum
|
4
|
-
extend ActiveSupport::Concern
|
5
4
|
|
6
|
-
|
5
|
+
module Enumerable
|
6
|
+
extend ActiveSupport::Concern
|
7
7
|
|
8
|
-
|
9
|
-
klass = self
|
8
|
+
class_methods do
|
10
9
|
|
11
|
-
definitions
|
10
|
+
def jt_enum(definitions)
|
11
|
+
klass = self
|
12
12
|
|
13
|
-
|
13
|
+
definitions.each do |field, values|
|
14
14
|
|
15
|
-
|
15
|
+
enum_values = ActiveSupport::HashWithIndifferentAccess.new
|
16
16
|
|
17
|
-
|
18
|
-
value_method_name = "#{field}_#{value}"
|
17
|
+
klass.singleton_class.send(:define_method, field.to_s.pluralize) { enum_values }
|
19
18
|
|
20
|
-
|
19
|
+
values.each_with_index do |value, i|
|
20
|
+
value_method_name = "#{field}_#{value}"
|
21
21
|
|
22
|
-
|
23
|
-
define_method("#{value_method_name}!") { update! field => i }
|
22
|
+
enum_values[value] = i
|
24
23
|
|
25
|
-
|
26
|
-
|
24
|
+
define_method("#{value_method_name}?") { self[field] == i }
|
25
|
+
define_method("#{value_method_name}!") { update! field => i }
|
26
|
+
|
27
|
+
klass.scope value_method_name, -> { klass.where field => i }
|
28
|
+
end
|
29
|
+
|
30
|
+
validates field, allow_nil: true, inclusion: { in: klass.send(field.to_s.pluralize).values }
|
27
31
|
|
28
|
-
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
29
36
|
|
37
|
+
class Railtie < Rails::Railtie
|
38
|
+
initializer 'jt_rails_enum.insert_into_active_record' do |app|
|
39
|
+
ActiveSupport.on_load :active_record do
|
40
|
+
ActiveRecord::Base.send(:include, JT::Rails::Enum::Enumerable)
|
30
41
|
end
|
31
42
|
end
|
43
|
+
|
32
44
|
end
|
45
|
+
|
33
46
|
end
|
34
47
|
end
|
35
48
|
end
|
36
|
-
|
37
|
-
ActiveRecord::Base.send :include, JT::Rails::Enum
|