custom_fielder 0.6.3 → 0.6.4
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: 3e99d0877255c6feab7652bd4fa0cc3e5ae95a76
|
4
|
+
data.tar.gz: 0d825d0f7af1a89e0bf680b399b3e7e7168521c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86626bfeb0a08b957ffad288338260add169e423f57e6781792ee03404db2dd20db82b9ac223fccbeba3e1a3d6c9edfa4958d0751cad938d34b772ed4fabd74e
|
7
|
+
data.tar.gz: 881e26cf496358268982bc42381a973e4e50e033782e3547eeb8fa8a84b734c7ecb75ccae64ef66e56bbc62e52976e897f76208793ab2b1010506ca00eb245e2
|
@@ -1,14 +1,49 @@
|
|
1
1
|
module CustomFielder
|
2
2
|
class Field < ActiveRecord::Base
|
3
|
+
include TypeCheckingHelper
|
3
4
|
include DeserializationHelper
|
4
5
|
|
6
|
+
FIELD_TYPE_MAP = {
|
7
|
+
'Text' => 'String',
|
8
|
+
'Integer' => 'Fixnum',
|
9
|
+
'Decmial' => 'Float',
|
10
|
+
'Date' => 'Date',
|
11
|
+
'Date & Time' => 'DateTime',
|
12
|
+
'True/False' => 'Boolean',
|
13
|
+
'List' => 'Array'
|
14
|
+
}
|
15
|
+
|
16
|
+
FORM_FIELD_TYPES = ['Text Entry', 'Multiple Choice', 'Date', 'DateTime', 'Boolean', 'List']
|
17
|
+
|
5
18
|
has_many :values
|
6
19
|
|
7
|
-
validates :name,
|
8
|
-
validates :field_type,
|
9
|
-
|
10
|
-
validates :fieldable_type,
|
11
|
-
validates :allow_blank,
|
20
|
+
validates :name, presence: true
|
21
|
+
validates :field_type, presence: true,
|
22
|
+
inclusion: { in: %w[Array Fixnum Float Date DateTime Boolean String] }
|
23
|
+
validates :fieldable_type, presence: true
|
24
|
+
validates :allow_blank, inclusion: { in: [true, false] }
|
25
|
+
validate :type_of_allowed_values
|
26
|
+
|
27
|
+
scope :text_entry_fields, -> { all.where(field_type: ['String', 'Fixnum', 'Float'], allowed_values: nil) }
|
28
|
+
scope :multiple_choice_fields, -> { all.where.not(allowed_values: nil) }
|
29
|
+
scope :date_fields, -> { all.where(field_type: 'Date', allowed_values: nil) }
|
30
|
+
scope :date_time_fields, -> { all.where(field_type: 'DateTime', allowed_values: nil) }
|
31
|
+
scope :boolean_fields, -> { all.where(field_type: 'Boolean', allowed_values: nil) }
|
32
|
+
scope :list_fields, -> { all.where(field_type: 'Array') }
|
33
|
+
|
34
|
+
##
|
35
|
+
# Creates a hash of form field types mapped to array of field, field id pairs
|
36
|
+
# meant for use in a form select dropdown with options group
|
37
|
+
#
|
38
|
+
# @return [Hash]
|
39
|
+
#
|
40
|
+
def self.all_fields_hashed_for_opt_group
|
41
|
+
FORM_FIELD_TYPES.inject(Hash.new) do |hash, form_field|
|
42
|
+
fields = send("#{form_field.delete(' ').underscore}_fields")
|
43
|
+
hash[form_field] = fields.map { |field| [field, field.id] }
|
44
|
+
hash
|
45
|
+
end.delete_if { |k, v| v == [] }
|
46
|
+
end
|
12
47
|
|
13
48
|
##
|
14
49
|
# Returns the deserialized version of allowed_values
|
@@ -19,5 +54,27 @@ module CustomFielder
|
|
19
54
|
allowed_values.nil? ? nil : deserialize('Array', allowed_values)
|
20
55
|
end
|
21
56
|
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
##
|
61
|
+
# Checks to make sure all values in allowed_values are of the correct type
|
62
|
+
#
|
63
|
+
# @return [Nil]
|
64
|
+
#
|
65
|
+
def type_of_allowed_values
|
66
|
+
return if allowed_values.nil?
|
67
|
+
options.each { |v| allowed_values_error unless v.class.to_s == field_type }
|
68
|
+
end
|
69
|
+
|
70
|
+
##
|
71
|
+
# Adds error if there is an allowed_values_error
|
72
|
+
#
|
73
|
+
# @return [Nil]
|
74
|
+
#
|
75
|
+
def allowed_values_error
|
76
|
+
errors.add(:allowed_values, 'Value in options is not the correct type')
|
77
|
+
end
|
78
|
+
|
22
79
|
end
|
23
80
|
end
|