jt-rails-enum 1.0.1 → 1.0.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: ca5be5149ec48c4e3f647633c7fc82afb7b1f1b1
4
- data.tar.gz: ab1bbe3192442aa030c7f11b222659dd190599cd
3
+ metadata.gz: dcc16a272ed8f7564c3b0889155b20c049c1d2bb
4
+ data.tar.gz: 45e1a082c8c1aa99826bc69e3721b80c9dddd6ab
5
5
  SHA512:
6
- metadata.gz: 463c6a1d8c18fc548288dffda6a4ad3860112a463719241e2ca84af6efa2667240523ead8fdd7d08cdd47a18a1e4425fba343ca9721f27fd2a7263382455b334
7
- data.tar.gz: 147fcb38b49c3c13d3cd273667525ca95356ae78baec8ff7989d116c18e34d43c3e05f6547ac920368a8e14d995bfedb49024630bc65b96acd926d3100242cf2
6
+ metadata.gz: 942354ccbbaa5a671294779add44d779b0f261dca8c42dd179a75846faac6aa210b238669dae922c43bb23ed7c27f94d868f2136ae3eef91d5040adcb639c2e5
7
+ data.tar.gz: 51b5383f617d89a5c7df8856a50bd136ab60a035820c63d3b770280f38747d69cf2ba992c8086987e8e0e9624eca1c465580ad57f00bcd4db82dbd67dc76deb6
@@ -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.1'
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
- class_methods do
5
+ module Enumerable
6
+ extend ActiveSupport::Concern
7
7
 
8
- def jt_enum(definitions)
9
- klass = self
8
+ class_methods do
10
9
 
11
- definitions.each do |field, values|
10
+ def jt_enum(definitions)
11
+ klass = self
12
12
 
13
- enum_values = ActiveSupport::HashWithIndifferentAccess.new
13
+ definitions.each do |field, values|
14
14
 
15
- klass.singleton_class.send(:define_method, field.to_s.pluralize) { enum_values }
15
+ enum_values = ActiveSupport::HashWithIndifferentAccess.new
16
16
 
17
- values.each_with_index do |value, i|
18
- value_method_name = "#{field}_#{value}"
17
+ klass.singleton_class.send(:define_method, field.to_s.pluralize) { enum_values }
19
18
 
20
- enum_values[value] = i
19
+ values.each_with_index do |value, i|
20
+ value_method_name = "#{field}_#{value}"
21
21
 
22
- define_method("#{value_method_name}?") { self[field] == i }
23
- define_method("#{value_method_name}!") { update! field => i }
22
+ enum_values[value] = i
24
23
 
25
- klass.scope value_method_name, -> { klass.where field => i }
26
- end
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
- validates field, allow_nil: true, inclusion: { in: klass.send(field.to_s.pluralize).values }
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jt-rails-enum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonathan TRIBOUHARET