custom_fielder 0.5.0 → 0.5.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57116a86043be57d309bdb3d0bf477e5bca1fc12
|
4
|
+
data.tar.gz: 09b44f4232247891266e7b088868dbc4594ea7d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed4013e514ea6bcad03c3e702b58e95baa94d19cf85109deddfb88c83e9f7bcc058aee51c06d112919749a6d7a7915e60c0a0e4ef33402b7ba3a06234edd859d
|
7
|
+
data.tar.gz: 5da4f4f282d96bcaefeef25bbe57d4270af6d6ccb6b731fe225461c8ae9da09f9f6114e8a711c9022fe6d4f9c72e66791fb871cf59741ef30a94706e4993232a
|
@@ -9,8 +9,13 @@ module CustomFielder
|
|
9
9
|
validate :check_allow_blank
|
10
10
|
validate :check_allowed_values
|
11
11
|
|
12
|
-
belongs_to :field_attributes, dependent: :destroy, class_name: 'FieldAttributes'
|
13
12
|
belongs_to :custom_fieldable, polymorphic: true
|
13
|
+
belongs_to :field_attributes, dependent: :destroy, class_name: 'FieldAttributes'
|
14
|
+
|
15
|
+
scope :active, -> { where(active: true) }
|
16
|
+
scope :inactive, -> { where(active: false) }
|
17
|
+
|
18
|
+
scope :fields_with_attributes, -> (field_attrs) { where(field_attributes: field_attrs) }
|
14
19
|
|
15
20
|
##
|
16
21
|
# Because everything is stored as string with ActiveRecord
|
@@ -22,22 +27,29 @@ module CustomFielder
|
|
22
27
|
alias_method :raw_value, :value
|
23
28
|
def value; deserialize_value; end;
|
24
29
|
|
25
|
-
##
|
26
|
-
# Adds class methods to deactivate or activate all the fields
|
27
|
-
# that belong to the passed attribute object
|
28
|
-
#
|
29
30
|
class << self
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
31
|
+
##
|
32
|
+
# Activates all CustomFields that belong to passed field attributes
|
33
|
+
#
|
34
|
+
# @param field_attrs [CustomField::FieldAttributes]
|
35
|
+
#
|
36
|
+
# @return [Nil]
|
37
|
+
#
|
38
|
+
def activate_field_for_all(field_attrs)
|
39
|
+
fields = all.where(field_attributes: field_attrs)
|
40
|
+
fields.each { |field| field.active = true; field.save }
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Activates all CustomFields that belong to passed field attributes
|
45
|
+
#
|
46
|
+
# @param field_attrs [CustomField::FieldAttributes]
|
47
|
+
#
|
48
|
+
# @return [Nil]
|
49
|
+
#
|
50
|
+
def deactivate_field_for_all(field_attrs)
|
51
|
+
fields = all.where(field_attributes: field_attrs)
|
52
|
+
fields.each { |field| field.active = false; field.save }
|
41
53
|
end
|
42
54
|
end
|
43
55
|
|
@@ -100,57 +112,37 @@ module CustomFielder
|
|
100
112
|
# @return [Nil]
|
101
113
|
#
|
102
114
|
def set_default
|
103
|
-
|
104
|
-
update_attribute(:value, default)
|
105
|
-
end
|
115
|
+
update_attribute(:value, default) if default? and raw_value_blank?
|
106
116
|
end
|
107
117
|
|
108
118
|
##
|
109
|
-
# @return [
|
119
|
+
# @return [Nil] Adds error if invalid
|
110
120
|
#
|
111
121
|
def check_type
|
112
|
-
if
|
113
|
-
|
114
|
-
end
|
115
|
-
|
116
|
-
unless is_correct_type?(type, raw_value)
|
117
|
-
value_mismatch_error
|
118
|
-
else
|
119
|
-
true
|
120
|
-
end
|
122
|
+
return if raw_value_blank?
|
123
|
+
value_mismatch_error unless is_correct_type?(type, raw_value)
|
121
124
|
end
|
122
125
|
|
123
126
|
##
|
124
|
-
# @return [
|
127
|
+
# @return [Nil] Adds error if invalid
|
125
128
|
#
|
126
129
|
def check_allow_blank
|
127
|
-
if allow_blank
|
128
|
-
value_blank_error
|
129
|
-
else
|
130
|
-
true
|
131
|
-
end
|
130
|
+
value_blank_error if !allow_blank and raw_value_blank?
|
132
131
|
end
|
133
132
|
|
134
133
|
##
|
135
|
-
# @return [
|
134
|
+
# @return [Nil] Adds error if invalid
|
136
135
|
#
|
137
136
|
def check_allowed_values
|
138
|
-
|
139
|
-
|
140
|
-
end
|
141
|
-
|
142
|
-
unless allowed_values.include?(value)
|
143
|
-
value_not_allowed_error
|
144
|
-
else
|
145
|
-
true
|
146
|
-
end
|
137
|
+
return true unless allowed_values
|
138
|
+
value_not_allowed_error unless allowed_values.include?(value)
|
147
139
|
end
|
148
140
|
|
149
141
|
##
|
150
142
|
# If a value is passed that does not match field type adds
|
151
143
|
# a mismatch error
|
152
144
|
#
|
153
|
-
# @return [
|
145
|
+
# @return [Nil]
|
154
146
|
#
|
155
147
|
def value_mismatch_error
|
156
148
|
errors.add(:value, 'value passed does not match field type')
|
@@ -160,7 +152,7 @@ module CustomFielder
|
|
160
152
|
# If a value is passed that is blank and allow_blank is false
|
161
153
|
# adds a value blank error
|
162
154
|
#
|
163
|
-
# @return [
|
155
|
+
# @return [Nil]
|
164
156
|
#
|
165
157
|
def value_blank_error
|
166
158
|
errors.add(:value, 'blank value passed when allow blank is false')
|
@@ -170,11 +162,28 @@ module CustomFielder
|
|
170
162
|
# If a value is passed that is not included in allowed values
|
171
163
|
# adds a value not allowed error
|
172
164
|
#
|
173
|
-
# @return [
|
165
|
+
# @return [Nil]
|
174
166
|
#
|
175
167
|
def value_not_allowed_error
|
176
168
|
errors.add(:value, 'value passed not in allowed values')
|
177
169
|
end
|
178
170
|
|
171
|
+
##
|
172
|
+
# Return true if raw_value is nil or an empty string
|
173
|
+
#
|
174
|
+
# @return [Boolean]
|
175
|
+
#
|
176
|
+
def raw_value_blank?
|
177
|
+
raw_value == nil or raw_value == ''
|
178
|
+
end
|
179
|
+
|
180
|
+
##
|
181
|
+
# Return true if default is set and is not an empty string
|
182
|
+
#
|
183
|
+
# @return [Boolean]
|
184
|
+
#
|
185
|
+
def default?
|
186
|
+
default and default != ''
|
187
|
+
end
|
179
188
|
end
|
180
189
|
end
|
@@ -5,7 +5,8 @@ class CreateCustomFielderFieldAttributes < ActiveRecord::Migration
|
|
5
5
|
t.string :field_type, null: false
|
6
6
|
t.string :default
|
7
7
|
t.boolean :allow_blank, null: false
|
8
|
-
t.
|
8
|
+
t.string :allowed_values
|
9
|
+
t.text :note
|
9
10
|
|
10
11
|
t.timestamps null: false
|
11
12
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: custom_fielder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Hurst
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-05-
|
11
|
+
date: 2016-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 4.2
|
19
|
+
version: '4.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 4.2
|
26
|
+
version: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '1.3'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '1.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec-rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '3.0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: byebug
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
description:
|
70
56
|
email:
|
71
57
|
- hurst.178@osu.edu
|