enumerize 0.8.0 → 2.6.1
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 +5 -5
- data/.github/workflows/ruby.yml +73 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +210 -0
- data/Gemfile +4 -21
- data/Gemfile.global +12 -0
- data/Gemfile.mongo_mapper +6 -0
- data/Gemfile.rails60 +6 -0
- data/Gemfile.rails61 +6 -0
- data/Gemfile.rails70 +9 -0
- data/Gemfile.railsmaster +5 -0
- data/README.md +366 -73
- data/Rakefile +4 -4
- data/enumerize.gemspec +2 -1
- data/lib/enumerize/activemodel.rb +47 -0
- data/lib/enumerize/activerecord.rb +102 -27
- data/lib/enumerize/attribute.rb +59 -15
- data/lib/enumerize/attribute_map.rb +2 -0
- data/lib/enumerize/base.rb +32 -17
- data/lib/enumerize/hooks/formtastic.rb +7 -9
- data/lib/enumerize/hooks/sequel_dataset.rb +17 -0
- data/lib/enumerize/hooks/simple_form.rb +9 -12
- data/lib/enumerize/hooks/uniqueness.rb +7 -8
- data/lib/enumerize/integrations/rails_admin.rb +3 -1
- data/lib/enumerize/integrations/rspec/matcher.rb +112 -26
- data/lib/enumerize/integrations/rspec.rb +3 -0
- data/lib/enumerize/module.rb +2 -0
- data/lib/enumerize/module_attributes.rb +2 -0
- data/lib/enumerize/mongoid.rb +36 -0
- data/lib/enumerize/predicatable.rb +10 -17
- data/lib/enumerize/predicates.rb +5 -1
- data/lib/enumerize/scope/activerecord.rb +53 -0
- data/lib/enumerize/scope/mongoid.rb +50 -0
- data/lib/enumerize/scope/sequel.rb +56 -0
- data/lib/enumerize/sequel.rb +62 -0
- data/lib/enumerize/set.rb +20 -8
- data/lib/enumerize/utils.rb +12 -0
- data/lib/enumerize/value.rb +20 -20
- data/lib/enumerize/version.rb +3 -1
- data/lib/enumerize.rb +33 -2
- data/lib/sequel/plugins/enumerize.rb +18 -0
- data/spec/enumerize/integrations/rspec/matcher_spec.rb +261 -0
- data/spec/spec_helper.rb +30 -0
- data/test/activemodel_test.rb +114 -0
- data/test/activerecord_test.rb +434 -58
- data/test/attribute_map_test.rb +9 -7
- data/test/attribute_test.rb +52 -23
- data/test/base_test.rb +118 -66
- data/test/formtastic_test.rb +28 -12
- data/test/module_attributes_test.rb +10 -8
- data/test/mongo_mapper_test.rb +26 -11
- data/test/mongoid_test.rb +100 -15
- data/test/multiple_test.rb +41 -12
- data/test/predicates_test.rb +34 -26
- data/test/rails_admin_test.rb +8 -6
- data/test/sequel_test.rb +342 -0
- data/test/set_test.rb +42 -26
- data/test/simple_form_test.rb +25 -1
- data/test/support/mock_controller.rb +6 -0
- data/test/support/shared_enums.rb +43 -0
- data/test/support/view_test_helper.rb +18 -1
- data/test/test_helper.rb +33 -2
- data/test/value_test.rb +79 -28
- metadata +51 -12
- data/.travis.yml +0 -19
- data/Gemfile.rails4 +0 -23
- data/lib/enumerize/form_helper.rb +0 -23
- data/test/rspec_matcher_test.rb +0 -76
- data/test/rspec_spec.rb +0 -13
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumerize
|
4
|
+
module ActiveModelAttributesSupport
|
5
|
+
def enumerize(name, options={})
|
6
|
+
super
|
7
|
+
|
8
|
+
_enumerize_module.dependent_eval do
|
9
|
+
if self.included_modules.include? ::ActiveModel::Attributes
|
10
|
+
include InstanceMethods
|
11
|
+
|
12
|
+
attribute name, Enumerize::ActiveModelAttributesSupport::Type.new(enumerized_attributes[name])
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module InstanceMethods
|
18
|
+
# https://github.com/brainspec/enumerize/issues/74
|
19
|
+
def write_attribute(attr_name, value, *options)
|
20
|
+
if self.class.enumerized_attributes[attr_name]
|
21
|
+
_enumerized_values_for_validation[attr_name.to_s] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class Type < ActiveModel::Type::Value
|
29
|
+
def type
|
30
|
+
:enumerize
|
31
|
+
end
|
32
|
+
|
33
|
+
def initialize(attr)
|
34
|
+
@attr = attr
|
35
|
+
end
|
36
|
+
|
37
|
+
def serialize(value)
|
38
|
+
v = @attr.find_value(value)
|
39
|
+
v && v.value
|
40
|
+
end
|
41
|
+
|
42
|
+
def deserialize(value)
|
43
|
+
@attr.find_value(value)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -1,51 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerize
|
2
4
|
module ActiveRecordSupport
|
3
5
|
def enumerize(name, options={})
|
4
6
|
super
|
5
7
|
|
6
8
|
_enumerize_module.dependent_eval do
|
7
|
-
if
|
8
|
-
if options[:scope]
|
9
|
-
_define_scope_methods!(name, options)
|
10
|
-
end
|
11
|
-
|
9
|
+
if self < ::ActiveRecord::Base
|
12
10
|
include InstanceMethods
|
13
11
|
|
12
|
+
const_get(:ActiveRecord_Relation).include(RelationMethods)
|
13
|
+
const_get(:ActiveRecord_AssociationRelation).include(RelationMethods)
|
14
|
+
const_get(:ActiveRecord_Associations_CollectionProxy).include(RelationMethods)
|
15
|
+
|
14
16
|
# Since Rails use `allocate` method on models and initializes them with `init_with` method.
|
15
17
|
# This way `initialize` method is not being called, but `after_initialize` callback always gets triggered.
|
16
18
|
after_initialize :_set_default_value_for_enumerized_attributes
|
17
19
|
|
18
20
|
# https://github.com/brainspec/enumerize/issues/111
|
19
21
|
require 'enumerize/hooks/uniqueness'
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def _define_scope_methods!(name, options)
|
27
|
-
scope_name = options[:scope] == true ? "with_#{name}" : options[:scope]
|
28
22
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
23
|
+
unless options[:multiple]
|
24
|
+
if ::ActiveRecord.version >= ::Gem::Version.new("7.0.0.alpha")
|
25
|
+
attribute(name) do |subtype|
|
26
|
+
Type.new(enumerized_attributes[name], subtype)
|
27
|
+
end
|
28
|
+
elsif ::ActiveRecord.version >= ::Gem::Version.new("6.1.0.alpha")
|
29
|
+
decorate_attribute_type(name.to_s) do |subtype|
|
30
|
+
Type.new(enumerized_attributes[name], subtype)
|
31
|
+
end
|
32
|
+
else
|
33
|
+
decorate_attribute_type(name, :enumerize) do |subtype|
|
34
|
+
Type.new(enumerized_attributes[name], subtype)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
40
38
|
end
|
41
39
|
end
|
42
40
|
end
|
43
41
|
|
44
42
|
module InstanceMethods
|
45
43
|
# https://github.com/brainspec/enumerize/issues/74
|
46
|
-
def write_attribute(attr_name, value)
|
44
|
+
def write_attribute(attr_name, value, *options)
|
47
45
|
if self.class.enumerized_attributes[attr_name]
|
48
|
-
_enumerized_values_for_validation[attr_name] = value
|
46
|
+
_enumerized_values_for_validation[attr_name.to_s] = value
|
49
47
|
end
|
50
48
|
|
51
49
|
super
|
@@ -55,13 +53,90 @@ module Enumerize
|
|
55
53
|
def becomes(klass)
|
56
54
|
became = super
|
57
55
|
klass.enumerized_attributes.each do |attr|
|
58
|
-
|
56
|
+
# Rescue when column associated to the enum does not exist.
|
57
|
+
begin
|
59
58
|
became.send("#{attr.name}=", send(attr.name))
|
59
|
+
rescue ActiveModel::MissingAttributeError
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
63
|
became
|
64
64
|
end
|
65
|
+
|
66
|
+
def reload(options = nil)
|
67
|
+
reloaded = super
|
68
|
+
|
69
|
+
reloaded.class.enumerized_attributes.each do |attr|
|
70
|
+
begin
|
71
|
+
# Checks first if the enumerized attribute is in ActiveRecord::Store
|
72
|
+
store_attr, _ = reloaded.class.stored_attributes.detect do |_store_attr, keys|
|
73
|
+
keys.include?(attr.name)
|
74
|
+
end
|
75
|
+
|
76
|
+
if store_attr.present?
|
77
|
+
reloaded.send("#{attr.name}=", reloaded.send(store_attr).with_indifferent_access[attr.name])
|
78
|
+
else
|
79
|
+
reloaded.send("#{attr.name}=", reloaded[attr.name])
|
80
|
+
end
|
81
|
+
rescue ActiveModel::MissingAttributeError
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
reloaded
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
module RelationMethods
|
90
|
+
def update_all(updates)
|
91
|
+
if updates.is_a?(Hash)
|
92
|
+
enumerized_attributes.each do |attr|
|
93
|
+
next if updates[attr.name].blank? || attr.kind_of?(Enumerize::Multiple)
|
94
|
+
enumerize_value = attr.find_value(updates[attr.name])
|
95
|
+
updates[attr.name] = enumerize_value && enumerize_value.value
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
super(updates)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Type < ActiveRecord::Type::Value
|
104
|
+
delegate :type, to: :@subtype
|
105
|
+
|
106
|
+
def initialize(attr, subtype)
|
107
|
+
@attr = attr
|
108
|
+
@subtype = subtype
|
109
|
+
end
|
110
|
+
|
111
|
+
def serialize(value)
|
112
|
+
v = @attr.find_value(value)
|
113
|
+
(v && v.value) || value
|
114
|
+
end
|
115
|
+
|
116
|
+
alias type_cast_for_database serialize
|
117
|
+
|
118
|
+
def deserialize(value)
|
119
|
+
@attr.find_value(value)
|
120
|
+
end
|
121
|
+
|
122
|
+
alias type_cast_from_database deserialize
|
123
|
+
|
124
|
+
def as_json(options = nil)
|
125
|
+
{attr: @attr.name}.as_json(options)
|
126
|
+
end
|
127
|
+
|
128
|
+
def encode_with(coder)
|
129
|
+
coder[:class_name] = @attr.klass.name
|
130
|
+
coder[:attr_name] = @attr.name
|
131
|
+
coder[:subtype] = @subtype
|
132
|
+
end
|
133
|
+
|
134
|
+
def init_with(coder)
|
135
|
+
initialize(
|
136
|
+
coder[:class_name].constantize.enumerized_attributes[coder[:attr_name]],
|
137
|
+
coder[:subtype]
|
138
|
+
)
|
139
|
+
end
|
65
140
|
end
|
66
141
|
end
|
67
142
|
end
|
data/lib/enumerize/attribute.rb
CHANGED
@@ -1,27 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerize
|
2
4
|
class Attribute
|
3
|
-
attr_reader :name, :values, :default_value, :i18n_scope
|
5
|
+
attr_reader :klass, :name, :values, :default_value, :i18n_scope, :skip_validations_value
|
4
6
|
|
5
7
|
def initialize(klass, name, options={})
|
6
8
|
raise ArgumentError, ':in option is required' unless options[:in]
|
9
|
+
raise ArgumentError, ':scope option does not work with option :multiple' if options[:multiple] && options[:scope]
|
7
10
|
|
8
11
|
extend Multiple if options[:multiple]
|
9
12
|
|
10
13
|
@klass = klass
|
11
14
|
@name = name.to_sym
|
12
|
-
@values = Array(options[:in]).map { |v| Value.new(self, *v) }
|
13
|
-
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
|
14
|
-
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
|
15
15
|
|
16
16
|
if options[:i18n_scope]
|
17
17
|
raise ArgumentError, ':i18n_scope option accepts only String or Array of strings' unless Array(options[:i18n_scope]).all? { |s| s.is_a?(String) }
|
18
18
|
@i18n_scope = options[:i18n_scope]
|
19
19
|
end
|
20
20
|
|
21
|
+
value_class = options.fetch(:value_class, Value)
|
22
|
+
@values = Array(options[:in]).map { |v| value_class.new(self, *v).freeze }
|
23
|
+
|
24
|
+
@value_hash = Hash[@values.map { |v| [v.value.to_s, v] }]
|
25
|
+
@value_hash.merge! Hash[@values.map { |v| [v.to_s, v] }]
|
26
|
+
|
21
27
|
if options[:default]
|
22
28
|
@default_value = find_default_value(options[:default])
|
23
29
|
raise ArgumentError, 'invalid default value' unless @default_value
|
24
30
|
end
|
31
|
+
|
32
|
+
@skip_validations_value = options.fetch(:skip_validations, false)
|
25
33
|
end
|
26
34
|
|
27
35
|
def find_default_value(value)
|
@@ -36,11 +44,23 @@ module Enumerize
|
|
36
44
|
@value_hash[value.to_s] unless value.nil?
|
37
45
|
end
|
38
46
|
|
47
|
+
def find_values(*values)
|
48
|
+
values.map { |value| find_value(value) }.compact
|
49
|
+
end
|
50
|
+
|
51
|
+
def each_value
|
52
|
+
values.each { |value| yield value }
|
53
|
+
end
|
54
|
+
|
55
|
+
def value?(value)
|
56
|
+
values.include?(value)
|
57
|
+
end
|
58
|
+
|
39
59
|
def i18n_scopes
|
40
60
|
@i18n_scopes ||= if i18n_scope
|
41
|
-
|
61
|
+
Array(i18n_scope)
|
42
62
|
elsif @klass.respond_to?(:model_name)
|
43
|
-
|
63
|
+
["enumerize.#{@klass.model_name.i18n_key}.#{name}"]
|
44
64
|
else
|
45
65
|
[]
|
46
66
|
end
|
@@ -67,6 +87,10 @@ module Enumerize
|
|
67
87
|
values.map { |v| [v.text, v.to_s] }
|
68
88
|
end
|
69
89
|
|
90
|
+
def respond_to_missing?(method, include_private=false)
|
91
|
+
@value_hash.include?(method.to_s) || super
|
92
|
+
end
|
93
|
+
|
70
94
|
def define_methods!(mod)
|
71
95
|
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
72
96
|
def #{name}
|
@@ -84,8 +108,6 @@ module Enumerize
|
|
84
108
|
end
|
85
109
|
|
86
110
|
def #{name}=(new_value)
|
87
|
-
_enumerized_values_for_validation[:#{name}] = new_value.nil? ? nil : new_value.to_s
|
88
|
-
|
89
111
|
allowed_value_or_nil = self.class.enumerized_attributes[:#{name}].find_value(new_value)
|
90
112
|
allowed_value_or_nil = allowed_value_or_nil.value unless allowed_value_or_nil.nil?
|
91
113
|
|
@@ -96,6 +118,10 @@ module Enumerize
|
|
96
118
|
else
|
97
119
|
@#{name} = allowed_value_or_nil
|
98
120
|
end
|
121
|
+
|
122
|
+
_enumerized_values_for_validation['#{name}'] = new_value.nil? ? nil : new_value.to_s
|
123
|
+
|
124
|
+
allowed_value_or_nil
|
99
125
|
end
|
100
126
|
|
101
127
|
def #{name}_text
|
@@ -107,9 +133,27 @@ module Enumerize
|
|
107
133
|
end
|
108
134
|
RUBY
|
109
135
|
end
|
136
|
+
|
137
|
+
private
|
138
|
+
|
139
|
+
def method_missing(method)
|
140
|
+
if @value_hash.include?(method.to_s)
|
141
|
+
find_value(method)
|
142
|
+
else
|
143
|
+
super
|
144
|
+
end
|
145
|
+
end
|
110
146
|
end
|
111
147
|
|
112
148
|
module Multiple
|
149
|
+
def find_default_value(value)
|
150
|
+
if value.respond_to?(:call)
|
151
|
+
value
|
152
|
+
else
|
153
|
+
find_values(*value)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
113
157
|
def define_methods!(mod)
|
114
158
|
mod.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
115
159
|
def #{name}
|
@@ -131,20 +175,20 @@ module Enumerize
|
|
131
175
|
end
|
132
176
|
|
133
177
|
def #{name}=(values)
|
134
|
-
_enumerized_values_for_validation[:#{name}] = values.respond_to?(:map) ? values.map(&:to_s) : values
|
135
|
-
|
136
178
|
@_#{name}_enumerized_set = Enumerize::Set.new(self, self.class.enumerized_attributes[:#{name}], values)
|
137
|
-
|
179
|
+
raw_values = self.#{name}.values.map(&:value)
|
138
180
|
|
139
181
|
if defined?(super)
|
140
|
-
super
|
182
|
+
super raw_values
|
141
183
|
elsif respond_to?(:write_attribute, true)
|
142
|
-
write_attribute '#{name}',
|
184
|
+
write_attribute '#{name}', raw_values
|
143
185
|
else
|
144
|
-
@#{name} =
|
186
|
+
@#{name} = raw_values
|
145
187
|
end
|
146
188
|
|
147
|
-
#{name}
|
189
|
+
_enumerized_values_for_validation['#{name}'] = values.respond_to?(:map) ? values.reject(&:blank?).map(&:to_s) : values
|
190
|
+
|
191
|
+
self.#{name}
|
148
192
|
end
|
149
193
|
RUBY
|
150
194
|
end
|
data/lib/enumerize/base.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerize
|
2
4
|
module Base
|
3
5
|
def self.included(base)
|
4
6
|
base.extend ClassMethods
|
7
|
+
base.singleton_class.prepend ClassMethods::Hook
|
5
8
|
|
6
9
|
if base.respond_to?(:validate)
|
7
10
|
base.validate :_validate_enumerized_attributes
|
@@ -28,9 +31,11 @@ module Enumerize
|
|
28
31
|
@enumerized_attributes ||= AttributeMap.new
|
29
32
|
end
|
30
33
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
+
module Hook
|
35
|
+
def inherited(subclass)
|
36
|
+
enumerized_attributes.add_dependant subclass.enumerized_attributes
|
37
|
+
super subclass
|
38
|
+
end
|
34
39
|
end
|
35
40
|
|
36
41
|
private
|
@@ -44,16 +49,20 @@ module Enumerize
|
|
44
49
|
end
|
45
50
|
end
|
46
51
|
|
47
|
-
def initialize(
|
52
|
+
def initialize(...)
|
48
53
|
super
|
49
54
|
_set_default_value_for_enumerized_attributes
|
50
55
|
end
|
51
56
|
|
52
57
|
def read_attribute_for_validation(key)
|
58
|
+
key = key.to_s
|
59
|
+
|
53
60
|
if _enumerized_values_for_validation.has_key?(key)
|
54
61
|
_enumerized_values_for_validation[key]
|
55
|
-
|
62
|
+
elsif defined?(super)
|
56
63
|
super
|
64
|
+
else
|
65
|
+
send(key)
|
57
66
|
end
|
58
67
|
end
|
59
68
|
|
@@ -65,6 +74,9 @@ module Enumerize
|
|
65
74
|
|
66
75
|
def _validate_enumerized_attributes
|
67
76
|
self.class.enumerized_attributes.each do |attr|
|
77
|
+
skip_validations = Utils.call_if_callable(attr.skip_validations_value, self)
|
78
|
+
next if skip_validations
|
79
|
+
|
68
80
|
value = read_attribute_for_validation(attr.name)
|
69
81
|
next if value.blank?
|
70
82
|
|
@@ -78,23 +90,26 @@ module Enumerize
|
|
78
90
|
|
79
91
|
def _set_default_value_for_enumerized_attributes
|
80
92
|
self.class.enumerized_attributes.each do |attr|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
public_send(attr.name)
|
85
|
-
rescue ActiveModel::MissingAttributeError
|
86
|
-
nil
|
87
|
-
end
|
93
|
+
_set_default_value_for_enumerized_attribute(attr)
|
94
|
+
end
|
95
|
+
end
|
88
96
|
|
89
|
-
|
90
|
-
|
97
|
+
def _set_default_value_for_enumerized_attribute(attr)
|
98
|
+
return if attr.default_value.nil?
|
99
|
+
begin
|
100
|
+
if respond_to?(attr.name)
|
101
|
+
attr_value = public_send(attr.name)
|
102
|
+
else
|
103
|
+
return
|
104
|
+
end
|
91
105
|
|
92
|
-
|
93
|
-
value = value.arity == 0 ? value.call : value.call(self)
|
94
|
-
end
|
106
|
+
value_for_validation = _enumerized_values_for_validation[attr.name.to_s]
|
95
107
|
|
108
|
+
if (!attr_value || attr_value.empty?) && (!value_for_validation || value_for_validation.empty?)
|
109
|
+
value = Utils.call_if_callable(attr.default_value, self)
|
96
110
|
public_send("#{attr.name}=", value)
|
97
111
|
end
|
112
|
+
rescue ActiveModel::MissingAttributeError
|
98
113
|
end
|
99
114
|
end
|
100
115
|
end
|
@@ -1,16 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module Enumerize
|
4
6
|
module Hooks
|
5
7
|
module FormtasticFormBuilderExtension
|
6
|
-
extend ActiveSupport::Concern
|
7
|
-
|
8
|
-
included do
|
9
|
-
alias_method_chain :input, :enumerize
|
10
|
-
end
|
11
8
|
|
12
|
-
def
|
13
|
-
|
9
|
+
def input(method, options={})
|
10
|
+
enumerized_object = convert_to_model(object)
|
11
|
+
klass = enumerized_object.class
|
14
12
|
|
15
13
|
if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[method])
|
16
14
|
options[:collection] ||= attr.options
|
@@ -20,10 +18,10 @@ module Enumerize
|
|
20
18
|
end
|
21
19
|
end
|
22
20
|
|
23
|
-
|
21
|
+
super(method, options)
|
24
22
|
end
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
::Formtastic::FormBuilder.send :
|
27
|
+
::Formtastic::FormBuilder.send :prepend, Enumerize::Hooks::FormtasticFormBuilderExtension
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Enumerize
|
4
|
+
module Hooks
|
5
|
+
module SequelDataset
|
6
|
+
def literal_append(sql, v)
|
7
|
+
if v.is_a?(Enumerize::Value)
|
8
|
+
super(sql, v.value)
|
9
|
+
else
|
10
|
+
super(sql, v)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
::Sequel::Dataset.send :prepend, Enumerize::Hooks::SequelDataset
|
@@ -1,29 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module Enumerize
|
4
6
|
module Hooks
|
5
7
|
module SimpleFormBuilderExtension
|
6
|
-
extend ActiveSupport::Concern
|
7
|
-
|
8
|
-
included do
|
9
|
-
alias_method_chain :input, :enumerize
|
10
|
-
alias_method_chain :input_field, :enumerize
|
11
|
-
end
|
12
8
|
|
13
|
-
def
|
9
|
+
def input(attribute_name, options={}, &block)
|
14
10
|
add_input_options_for_enumerized_attribute(attribute_name, options)
|
15
|
-
|
11
|
+
super(attribute_name, options, &block)
|
16
12
|
end
|
17
13
|
|
18
|
-
def
|
14
|
+
def input_field(attribute_name, options={})
|
19
15
|
add_input_options_for_enumerized_attribute(attribute_name, options)
|
20
|
-
|
16
|
+
super(attribute_name, options)
|
21
17
|
end
|
22
18
|
|
23
19
|
private
|
24
20
|
|
25
21
|
def add_input_options_for_enumerized_attribute(attribute_name, options)
|
26
|
-
|
22
|
+
enumerized_object = convert_to_model(object)
|
23
|
+
klass = enumerized_object.class
|
27
24
|
|
28
25
|
if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[attribute_name])
|
29
26
|
options[:collection] ||= attr.options
|
@@ -37,4 +34,4 @@ module Enumerize
|
|
37
34
|
end
|
38
35
|
end
|
39
36
|
|
40
|
-
::SimpleForm::FormBuilder.send :
|
37
|
+
::SimpleForm::FormBuilder.send :prepend, Enumerize::Hooks::SimpleFormBuilderExtension
|
@@ -1,23 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'active_support/concern'
|
2
4
|
|
3
5
|
module Enumerize
|
4
6
|
module Hooks
|
5
7
|
module UniquenessValidator
|
6
|
-
extend ActiveSupport::Concern
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
end
|
9
|
+
def validate_each(record, name, value)
|
10
|
+
klass = record.to_model.class
|
11
11
|
|
12
|
-
|
13
|
-
if record.class.respond_to?(:enumerized_attributes) && (attr = record.class.enumerized_attributes[name])
|
12
|
+
if klass.respond_to?(:enumerized_attributes) && (attr = klass.enumerized_attributes[name])
|
14
13
|
value = attr.find_value(value).try(:value)
|
15
14
|
end
|
16
15
|
|
17
|
-
|
16
|
+
super(record, name, value)
|
18
17
|
end
|
19
18
|
end
|
20
19
|
end
|
21
20
|
end
|
22
21
|
|
23
|
-
::ActiveRecord::Validations::UniquenessValidator.send :
|
22
|
+
::ActiveRecord::Validations::UniquenessValidator.send :prepend, Enumerize::Hooks::UniquenessValidator
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Enumerize
|
2
4
|
module Integrations
|
3
5
|
module RailsAdmin
|
@@ -7,7 +9,7 @@ module Enumerize
|
|
7
9
|
|
8
10
|
_enumerize_module.module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
9
11
|
def #{name}_enum
|
10
|
-
self.class.enumerized_attributes[:#{name}].
|
12
|
+
self.class.enumerized_attributes[:#{name}].options
|
11
13
|
end
|
12
14
|
RUBY
|
13
15
|
end
|