custom_fields 2.12.1 → 2.13.0
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/MIT-LICENSE +1 -1
- data/README.md +4 -5
- data/lib/custom_fields/extensions/active_support.rb +22 -23
- data/lib/custom_fields/extensions/carrierwave.rb +9 -40
- data/lib/custom_fields/extensions/mongoid/association/referenced/has_many.rb +49 -0
- data/lib/custom_fields/extensions/mongoid/association/referenced/has_one.rb +17 -0
- data/lib/custom_fields/extensions/mongoid/association/relatable.rb +30 -0
- data/lib/custom_fields/extensions/mongoid/criteria/queryable/smash.rb +3 -6
- data/lib/custom_fields/extensions/mongoid/document.rb +4 -8
- data/lib/custom_fields/extensions/mongoid/factory.rb +7 -9
- data/lib/custom_fields/extensions/mongoid/fields/i18n.rb +17 -17
- data/lib/custom_fields/extensions/mongoid/fields/localized.rb +21 -21
- data/lib/custom_fields/extensions/mongoid/fields.rb +4 -7
- data/lib/custom_fields/extensions/mongoid/validatable/collection_size.rb +86 -0
- data/lib/custom_fields/extensions/mongoid/{validations → validatable}/macros.rb +3 -2
- data/lib/custom_fields/extensions/origin/smash.rb +4 -5
- data/lib/custom_fields/field.rb +39 -36
- data/lib/custom_fields/source.rb +46 -44
- data/lib/custom_fields/target.rb +11 -12
- data/lib/custom_fields/target_helpers.rb +21 -23
- data/lib/custom_fields/types/belongs_to.rb +6 -20
- data/lib/custom_fields/types/boolean.rb +5 -12
- data/lib/custom_fields/types/color.rb +4 -12
- data/lib/custom_fields/types/date.rb +20 -22
- data/lib/custom_fields/types/date_time.rb +16 -18
- data/lib/custom_fields/types/default.rb +16 -24
- data/lib/custom_fields/types/email.rb +6 -13
- data/lib/custom_fields/types/file.rb +26 -35
- data/lib/custom_fields/types/float.rb +10 -13
- data/lib/custom_fields/types/has_many.rb +12 -16
- data/lib/custom_fields/types/integer.rb +10 -13
- data/lib/custom_fields/types/json.rb +18 -29
- data/lib/custom_fields/types/many_to_many.rb +14 -19
- data/lib/custom_fields/types/money.rb +34 -35
- data/lib/custom_fields/types/password.rb +19 -23
- data/lib/custom_fields/types/relationship_default.rb +4 -16
- data/lib/custom_fields/types/select.rb +43 -51
- data/lib/custom_fields/types/string.rb +5 -13
- data/lib/custom_fields/types/tags.rb +11 -11
- data/lib/custom_fields/types/text.rb +4 -16
- data/lib/custom_fields/version.rb +4 -2
- data/lib/custom_fields.rb +16 -16
- metadata +38 -38
- data/lib/custom_fields/extensions/mongoid/relations/options.rb +0 -19
- data/lib/custom_fields/extensions/mongoid/relations/referenced/in.rb +0 -18
- data/lib/custom_fields/extensions/mongoid/relations/referenced/many.rb +0 -30
- data/lib/custom_fields/extensions/mongoid/validations/collection_size.rb +0 -43
@@ -1,17 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module Color
|
6
|
-
|
7
6
|
module Field; end
|
8
7
|
|
9
8
|
module Target
|
10
|
-
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
|
13
11
|
module ClassMethods
|
14
|
-
|
15
12
|
# Add a color field
|
16
13
|
#
|
17
14
|
# @param [ Class ] klass The class to modify
|
@@ -30,7 +27,7 @@ module CustomFields
|
|
30
27
|
# @return [ Hash ] field name => raw value
|
31
28
|
#
|
32
29
|
def color_attribute_get(instance, name)
|
33
|
-
|
30
|
+
default_attribute_get(instance, name)
|
34
31
|
end
|
35
32
|
|
36
33
|
# Set the value for the instance and the color field specified by
|
@@ -41,15 +38,10 @@ module CustomFields
|
|
41
38
|
# @param [ Hash ] attributes The attributes used to fetch the values
|
42
39
|
#
|
43
40
|
def color_attribute_set(instance, name, attributes)
|
44
|
-
|
41
|
+
default_attribute_set(instance, name, attributes)
|
45
42
|
end
|
46
|
-
|
47
43
|
end
|
48
|
-
|
49
44
|
end
|
50
|
-
|
51
45
|
end
|
52
|
-
|
53
46
|
end
|
54
|
-
|
55
47
|
end
|
@@ -1,17 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module Date
|
6
|
-
|
7
6
|
module Field; end
|
8
7
|
|
9
8
|
module Target
|
10
|
-
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
|
13
11
|
module ClassMethods
|
14
|
-
|
15
12
|
# Adds a date field
|
16
13
|
#
|
17
14
|
# @param [ Class ] klass The class to modify
|
@@ -26,9 +23,9 @@ module CustomFields
|
|
26
23
|
klass.send(:define_method, :"formatted_#{name}") { _get_formatted_date(name) }
|
27
24
|
klass.send(:define_method, :"formatted_#{name}=") { |value| _set_formatted_date(name, value) }
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
return unless rule['required']
|
27
|
+
|
28
|
+
klass.validates_presence_of name, :"formatted_#{name}"
|
32
29
|
end
|
33
30
|
|
34
31
|
# Build a hash storing the formatted value for
|
@@ -61,37 +58,38 @@ module CustomFields
|
|
61
58
|
|
62
59
|
instance.send(:"formatted_#{name}=", value)
|
63
60
|
end
|
64
|
-
|
65
61
|
end
|
66
62
|
|
67
63
|
protected
|
68
64
|
|
69
65
|
def _set_formatted_date(name, value)
|
70
66
|
if value.is_a?(::String) && !value.blank?
|
71
|
-
date = ::Date._strptime(value,
|
72
|
-
|
73
|
-
if date
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
67
|
+
date = ::Date._strptime(value, _formatted_date_format)
|
68
|
+
|
69
|
+
value = if date
|
70
|
+
::Date.new(date[:year], date[:mon], date[:mday])
|
71
|
+
else
|
72
|
+
begin
|
73
|
+
::Date.parse(value)
|
74
|
+
rescue StandardError
|
75
|
+
nil
|
76
|
+
end
|
77
|
+
end
|
78
78
|
end
|
79
79
|
|
80
|
-
|
80
|
+
send(:"#{name}=", value)
|
81
81
|
end
|
82
82
|
|
83
83
|
def _get_formatted_date(name)
|
84
|
-
|
84
|
+
send(name.to_sym).strftime(_formatted_date_format)
|
85
|
+
rescue StandardError
|
86
|
+
nil
|
85
87
|
end
|
86
88
|
|
87
89
|
def _formatted_date_format
|
88
90
|
I18n.t('date.formats.default')
|
89
91
|
end
|
90
|
-
|
91
92
|
end
|
92
|
-
|
93
93
|
end
|
94
|
-
|
95
94
|
end
|
96
|
-
|
97
95
|
end
|
@@ -1,17 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module DateTime
|
6
|
-
|
7
6
|
module Field; end
|
8
7
|
|
9
8
|
module Target
|
10
|
-
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
|
13
11
|
module ClassMethods
|
14
|
-
|
15
12
|
# Adds a date_time field
|
16
13
|
#
|
17
14
|
# @param [ Class ] klass The class to modify
|
@@ -26,9 +23,9 @@ module CustomFields
|
|
26
23
|
klass.send(:define_method, :"formatted_#{name}") { _get_formatted_date_time(name) }
|
27
24
|
klass.send(:define_method, :"formatted_#{name}=") { |value| _set_formatted_date_time(name, value) }
|
28
25
|
|
29
|
-
|
30
|
-
|
31
|
-
|
26
|
+
return unless rule['required']
|
27
|
+
|
28
|
+
klass.validates_presence_of name, :"formatted_#{name}"
|
32
29
|
end
|
33
30
|
|
34
31
|
# Build a hash storing the formatted value for
|
@@ -61,37 +58,38 @@ module CustomFields
|
|
61
58
|
|
62
59
|
instance.send(:"formatted_#{name}=", value)
|
63
60
|
end
|
64
|
-
|
65
61
|
end
|
66
62
|
|
67
63
|
protected
|
68
64
|
|
69
65
|
def _set_formatted_date_time(name, value)
|
70
66
|
if value.is_a?(::String) && !value.blank?
|
71
|
-
date_time = ::DateTime._strptime(value,
|
67
|
+
date_time = ::DateTime._strptime(value, _formatted_date_time_format)
|
72
68
|
|
73
69
|
if date_time
|
74
|
-
value = ::Time.zone.local(date_time[:year], date_time[:mon], date_time[:mday], date_time[:hour], date_time[:min], date_time[:sec] || 0)
|
70
|
+
value = ::Time.zone.local(date_time[:year], date_time[:mon], date_time[:mday], date_time[:hour], date_time[:min], date_time[:sec] || 0) # , date_time[:zone] || "")
|
75
71
|
else
|
76
|
-
value =
|
72
|
+
value = begin
|
73
|
+
::Time.zone.parse(value)
|
74
|
+
rescue StandardError
|
75
|
+
nil
|
76
|
+
end
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
-
|
80
|
+
send(:"#{name}=", value)
|
81
81
|
end
|
82
82
|
|
83
83
|
def _get_formatted_date_time(name)
|
84
|
-
|
84
|
+
send(name.to_sym).strftime(_formatted_date_time_format)
|
85
|
+
rescue StandardError
|
86
|
+
nil
|
85
87
|
end
|
86
88
|
|
87
89
|
def _formatted_date_time_format
|
88
90
|
I18n.t('time.formats.default')
|
89
91
|
end
|
90
|
-
|
91
92
|
end
|
92
|
-
|
93
93
|
end
|
94
|
-
|
95
94
|
end
|
96
|
-
|
97
95
|
end
|
@@ -1,11 +1,9 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module Default
|
6
|
-
|
7
6
|
module Field
|
8
|
-
|
9
7
|
# Build the mongodb updates based on
|
10
8
|
# the new state of the field
|
11
9
|
#
|
@@ -15,30 +13,27 @@ module CustomFields
|
|
15
13
|
#
|
16
14
|
def collect_default_diff(memo)
|
17
15
|
# puts "collect_default_diff #{self.name}: #{self.persisted?} / #{self.destroyed?}" # DEBUG
|
18
|
-
if
|
19
|
-
if
|
20
|
-
memo['$unset'][
|
21
|
-
elsif
|
22
|
-
if
|
23
|
-
old_name, new_name =
|
16
|
+
if persisted?
|
17
|
+
if destroyed?
|
18
|
+
memo['$unset'][name] = 1
|
19
|
+
elsif changed?
|
20
|
+
if changes.key?('name')
|
21
|
+
old_name, new_name = changes['name']
|
24
22
|
memo['$rename'][old_name] = new_name
|
25
23
|
end
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
(memo['$set']['custom_fields_recipe.rules'] ||= []) <<
|
27
|
+
(memo['$set']['custom_fields_recipe.rules'] ||= []) << to_recipe
|
30
28
|
|
31
29
|
memo
|
32
30
|
end
|
33
|
-
|
34
31
|
end
|
35
32
|
|
36
33
|
module Target
|
37
|
-
|
38
34
|
extend ActiveSupport::Concern
|
39
35
|
|
40
36
|
module ClassMethods
|
41
|
-
|
42
37
|
# Modify the target class according to the rule.
|
43
38
|
# By default, it declares the field and a validator
|
44
39
|
# if specified by the rule
|
@@ -51,9 +46,11 @@ module CustomFields
|
|
51
46
|
|
52
47
|
klass.validates_presence_of rule['name'] if rule['required']
|
53
48
|
|
49
|
+
return unless rule['unique']
|
50
|
+
|
54
51
|
klass.validates_uniqueness_of rule['name'],
|
55
|
-
|
56
|
-
|
52
|
+
scope: :_type,
|
53
|
+
allow_blank: !rule['required']
|
57
54
|
end
|
58
55
|
|
59
56
|
# Build a hash storing the formatted (or not) values
|
@@ -70,10 +67,10 @@ module CustomFields
|
|
70
67
|
# @return [ Hash ] field name => formatted value or empty hash if no value
|
71
68
|
#
|
72
69
|
def default_attribute_get(instance, name)
|
73
|
-
|
74
|
-
{ name => instance.send(name.to_sym) }
|
75
|
-
else
|
70
|
+
if (value = instance.send(name.to_sym)).nil?
|
76
71
|
{}
|
72
|
+
else
|
73
|
+
{ name => instance.send(name.to_sym) }
|
77
74
|
end
|
78
75
|
end
|
79
76
|
|
@@ -94,13 +91,8 @@ module CustomFields
|
|
94
91
|
# simple assign
|
95
92
|
instance.send(:"#{name}=", attributes[name])
|
96
93
|
end
|
97
|
-
|
98
94
|
end
|
99
|
-
|
100
95
|
end
|
101
|
-
|
102
96
|
end
|
103
|
-
|
104
97
|
end
|
105
|
-
|
106
98
|
end
|
@@ -1,17 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module Email
|
6
|
-
|
7
6
|
module Field; end
|
8
7
|
|
9
8
|
module Target
|
10
|
-
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
|
13
11
|
module ClassMethods
|
14
|
-
|
15
12
|
# Add a string field
|
16
13
|
#
|
17
14
|
# @param [ Class ] klass The class to modify
|
@@ -22,7 +19,8 @@ module CustomFields
|
|
22
19
|
|
23
20
|
klass.field name, type: ::String, localize: rule['localized'] || false, default: rule['default']
|
24
21
|
klass.validates_presence_of name if rule['required']
|
25
|
-
klass.validates_format_of name, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/,
|
22
|
+
klass.validates_format_of name, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/,
|
23
|
+
allow_blank: !rule['required']
|
26
24
|
klass.validates_uniqueness_of rule['name'], scope: :_type if rule['unique']
|
27
25
|
end
|
28
26
|
|
@@ -35,7 +33,7 @@ module CustomFields
|
|
35
33
|
# @return [ Hash ] field name => raw value
|
36
34
|
#
|
37
35
|
def email_attribute_get(instance, name)
|
38
|
-
|
36
|
+
default_attribute_get(instance, name)
|
39
37
|
end
|
40
38
|
|
41
39
|
# Set the value for the instance and the string field specified by
|
@@ -46,15 +44,10 @@ module CustomFields
|
|
46
44
|
# @param [ Hash ] attributes The attributes used to fetch the values
|
47
45
|
#
|
48
46
|
def email_attribute_set(instance, name, attributes)
|
49
|
-
|
47
|
+
default_attribute_set(instance, name, attributes)
|
50
48
|
end
|
51
|
-
|
52
49
|
end
|
53
|
-
|
54
50
|
end
|
55
|
-
|
56
51
|
end
|
57
|
-
|
58
52
|
end
|
59
|
-
|
60
53
|
end
|
@@ -1,17 +1,14 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
+
module CustomFields
|
3
4
|
module Types
|
4
|
-
|
5
5
|
module File
|
6
|
-
|
7
6
|
module Field; end
|
8
7
|
|
9
8
|
module Target
|
10
|
-
|
11
9
|
extend ActiveSupport::Concern
|
12
10
|
|
13
11
|
module ClassMethods
|
14
|
-
|
15
12
|
# Adds a file field (using carrierwave)
|
16
13
|
#
|
17
14
|
# @param [ Class ] klass The class to modify
|
@@ -23,16 +20,14 @@ module CustomFields
|
|
23
20
|
klass.mount_uploader name, FileUploader
|
24
21
|
klass.field :"#{name}_size", type: ::Hash, default: {}
|
25
22
|
|
26
|
-
if rule['localized'] == true
|
27
|
-
|
28
|
-
|
23
|
+
klass.replace_field name, ::String, true if rule['localized'] == true
|
24
|
+
|
25
|
+
return unless rule['required']
|
29
26
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
UploaderPresenceValidator.new(object, name).validate
|
35
|
-
end
|
27
|
+
# FIXME: previously, we called "klass.validates_presence_of name"
|
28
|
+
# but it didn't work well with localized fields.
|
29
|
+
klass.validate do |object|
|
30
|
+
UploaderPresenceValidator.new(object, name).validate
|
36
31
|
end
|
37
32
|
end
|
38
33
|
|
@@ -44,7 +39,7 @@ module CustomFields
|
|
44
39
|
# @return [ Hash ] field name => url or empty hash if no file
|
45
40
|
#
|
46
41
|
def file_attribute_get(instance, name)
|
47
|
-
if instance.send(:"#{name}?") #"
|
42
|
+
if instance.send(:"#{name}?") # "
|
48
43
|
value = instance.send(name.to_sym).url
|
49
44
|
{ name => value, "#{name}_url" => value }
|
50
45
|
else
|
@@ -61,32 +56,32 @@ module CustomFields
|
|
61
56
|
#
|
62
57
|
def file_attribute_set(instance, name, attributes)
|
63
58
|
[name, "remote_#{name}_url", "remove_#{name}"].each do |_name|
|
64
|
-
|
59
|
+
default_attribute_set(instance, _name, attributes)
|
65
60
|
end.compact
|
66
61
|
end
|
67
|
-
|
68
62
|
end
|
69
|
-
|
70
63
|
end
|
71
64
|
|
72
65
|
class UploaderPresenceValidator
|
73
|
-
|
74
66
|
def initialize(document, name)
|
75
|
-
@document
|
67
|
+
@document = document
|
68
|
+
@name = name
|
76
69
|
end
|
77
70
|
|
78
71
|
def validate
|
79
|
-
|
80
|
-
@document.errors.add(@name, :blank)
|
81
|
-
end
|
82
|
-
end
|
72
|
+
return unless @document.send(@name).blank?
|
83
73
|
|
74
|
+
@document.errors.add(@name, :blank)
|
75
|
+
end
|
84
76
|
end
|
85
77
|
|
86
78
|
class FileUploader < ::CarrierWave::Uploader::Base
|
87
|
-
|
88
79
|
process :set_size_in_model
|
89
80
|
|
81
|
+
def present?
|
82
|
+
serializable_hash.present?
|
83
|
+
end
|
84
|
+
|
90
85
|
def filename
|
91
86
|
if original_filename && model.fields[mounted_as.to_s].localized?
|
92
87
|
_original_filename, extension = original_filename.split('.')
|
@@ -99,19 +94,15 @@ module CustomFields
|
|
99
94
|
def set_size_in_model
|
100
95
|
size_field_name = :"#{mounted_as}_size"
|
101
96
|
|
102
|
-
|
103
|
-
is_localized = model.fields[mounted_as.to_s].options[:localize]
|
104
|
-
key = is_localized ? ::Mongoid::Fields::I18n.locale.to_s : 'default'
|
105
|
-
values = model.send(size_field_name)
|
97
|
+
return unless model.respond_to?(size_field_name)
|
106
98
|
|
107
|
-
|
108
|
-
|
109
|
-
|
99
|
+
is_localized = model.fields[mounted_as.to_s].options[:localize]
|
100
|
+
key = is_localized ? ::Mongoid::Fields::I18n.locale.to_s : 'default'
|
101
|
+
values = model.send(size_field_name)
|
110
102
|
|
103
|
+
values[key] = file.size
|
104
|
+
end
|
111
105
|
end
|
112
|
-
|
113
106
|
end
|
114
|
-
|
115
107
|
end
|
116
|
-
|
117
108
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CustomFields
|
2
4
|
module Types
|
3
5
|
module Float
|
4
|
-
|
5
6
|
module Field; end
|
6
7
|
|
7
8
|
module Target
|
8
9
|
extend ActiveSupport::Concern
|
9
10
|
|
10
11
|
module ClassMethods
|
11
|
-
|
12
12
|
# Add a string field
|
13
13
|
#
|
14
14
|
# @param [ Class ] klass The class to modify
|
@@ -17,7 +17,7 @@ module CustomFields
|
|
17
17
|
def apply_float_custom_field(klass, rule)
|
18
18
|
klass.field rule['name'], type: ::Float, localize: rule['localized'] || false, default: rule['default']
|
19
19
|
klass.validates_presence_of rule['name'] if rule['required']
|
20
|
-
klass.validates rule['name'], numericality: true, if: ->(
|
20
|
+
klass.validates rule['name'], numericality: true, if: ->(_x) { rule['required'] }
|
21
21
|
end
|
22
22
|
|
23
23
|
# Build a hash storing the raw value for
|
@@ -29,7 +29,7 @@ module CustomFields
|
|
29
29
|
# @return [ Hash ] field name => raw value
|
30
30
|
#
|
31
31
|
def float_attribute_get(instance, name)
|
32
|
-
|
32
|
+
default_attribute_get(instance, name)
|
33
33
|
end
|
34
34
|
|
35
35
|
# Set the value for the instance and the string field specified by
|
@@ -40,13 +40,10 @@ module CustomFields
|
|
40
40
|
# @param [ Hash ] attributes The attributes used to fetch the values
|
41
41
|
#
|
42
42
|
def float_attribute_set(instance, name, attributes)
|
43
|
-
|
43
|
+
default_attribute_set(instance, name, attributes)
|
44
44
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end # Float
|
51
|
-
end # Types
|
52
|
-
end # CustomFields
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CustomFields
|
2
4
|
module Types
|
3
5
|
module HasMany
|
@@ -5,13 +7,12 @@ module CustomFields
|
|
5
7
|
extend ActiveSupport::Concern
|
6
8
|
|
7
9
|
included do
|
8
|
-
|
9
10
|
def has_many_to_recipe
|
10
|
-
{ 'class_name' =>
|
11
|
+
{ 'class_name' => class_name, 'inverse_of' => inverse_of, 'order_by' => order_by }
|
11
12
|
end
|
12
13
|
|
13
14
|
def has_many_is_relationship?
|
14
|
-
|
15
|
+
type == 'has_many'
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -19,7 +20,6 @@ module CustomFields
|
|
19
20
|
module Target
|
20
21
|
extend ActiveSupport::Concern
|
21
22
|
module ClassMethods
|
22
|
-
|
23
23
|
# Adds a has_many relationship between 2 mongoid models
|
24
24
|
#
|
25
25
|
# @param [ Class ] klass The class to modify
|
@@ -32,25 +32,21 @@ module CustomFields
|
|
32
32
|
_order_by = rule['order_by'] || position_name.to_sym.asc
|
33
33
|
_inverse_of = rule['inverse_of'].blank? ? nil : rule['inverse_of'] # an empty String can cause weird behaviours
|
34
34
|
|
35
|
-
klass.has_many rule['name'], class_name: rule['class_name'], inverse_of: _inverse_of, order: _order_by,
|
36
|
-
|
35
|
+
klass.has_many rule['name'], class_name: rule['class_name'], inverse_of: _inverse_of, order: _order_by,
|
36
|
+
validate: false do
|
37
37
|
def filtered(conditions = {}, order_by = nil)
|
38
|
-
list = conditions.empty? ?
|
38
|
+
list = conditions.empty? ? unscoped : where(conditions)
|
39
39
|
|
40
|
-
|
41
|
-
list.order_by(order_by)
|
42
|
-
else
|
43
|
-
list.order_by(relation_metadata.order)
|
44
|
-
end
|
40
|
+
list.order_by(order_by || association.options[:order])
|
45
41
|
end
|
46
|
-
|
42
|
+
alias_method :ordered, :filtered # backward compatibility + semantic purpose
|
47
43
|
end
|
48
44
|
|
49
45
|
klass.accepts_nested_attributes_for rule['name'], allow_destroy: true
|
50
46
|
|
51
|
-
|
52
|
-
|
53
|
-
|
47
|
+
return unless rule['required']
|
48
|
+
|
49
|
+
klass.validates_collection_size_of rule['name'], minimum: 1, message: :at_least_one_element, on: :update
|
54
50
|
end
|
55
51
|
end
|
56
52
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module CustomFields
|
2
4
|
module Types
|
3
5
|
module Integer
|
4
|
-
|
5
6
|
module Field; end
|
6
7
|
|
7
8
|
module Target
|
8
9
|
extend ActiveSupport::Concern
|
9
10
|
|
10
11
|
module ClassMethods
|
11
|
-
|
12
12
|
# Add a integer field
|
13
13
|
#
|
14
14
|
# @param [ Class ] klass The class to modify
|
@@ -19,7 +19,7 @@ module CustomFields
|
|
19
19
|
|
20
20
|
klass.field name, type: ::Integer, localize: rule['localized'] || false, default: rule['default']
|
21
21
|
klass.validates_presence_of name if rule['required']
|
22
|
-
klass.validates name, numericality: { only_integer: true }, if: ->(
|
22
|
+
klass.validates name, numericality: { only_integer: true }, if: ->(_x) { rule['required'] }
|
23
23
|
end
|
24
24
|
|
25
25
|
# Build a hash storing the raw value for
|
@@ -31,7 +31,7 @@ module CustomFields
|
|
31
31
|
# @return [ Hash ] field name => raw value
|
32
32
|
#
|
33
33
|
def integer_attribute_get(instance, name)
|
34
|
-
|
34
|
+
default_attribute_get(instance, name)
|
35
35
|
end
|
36
36
|
|
37
37
|
# Set the value for the instance and the string field specified by
|
@@ -42,13 +42,10 @@ module CustomFields
|
|
42
42
|
# @param [ Hash ] attributes The attributes used to fetch the values
|
43
43
|
#
|
44
44
|
def integer_attribute_set(instance, name, attributes)
|
45
|
-
|
45
|
+
default_attribute_set(instance, name, attributes)
|
46
46
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end # Integer
|
53
|
-
end # Types
|
54
|
-
end # CustomFields
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|