Class: CustomAttributes::FieldType
- Inherits:
-
Object
- Object
- CustomAttributes::FieldType
- Defined in:
- lib/custom_attributes/field_type.rb
Class Method Summary collapse
Instance Method Summary collapse
- #after_save_custom_value(custom_field, custom_value) ⇒ Object
- #before_custom_field_save(custom_field) ⇒ Object
-
#cast_custom_value(custom_value) ⇒ Object
Cast the value of an existing CustomValue.
- #cast_single_value(_custom_field, value, _customizable = nil) ⇒ Object
-
#cast_value(custom_field, value, customizable = nil) ⇒ Object
Cast the value according to field type rules.
-
#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object
Returns the HTML Tag to edit this field format.
- #label ⇒ Object
- #name ⇒ Object
-
#possible_custom_value_options(custom_value) ⇒ Object
Use to get the possible values of a CustomValue.
-
#possible_values_options(_custom_field, _object = nil) ⇒ Object
Override this in subclass to specify the possible values for the field with this type.
-
#set_custom_field_value(_custom_field, _custom_field_value, value) ⇒ Object
Prepares and sets CustomFieldValues value.
-
#validate_custom_field(_custom_field) ⇒ Object
Overide this in subclass to validate custom fields.
-
#validate_custom_value(custom_value) ⇒ Object
Validate a CustomValue according to the type roles.
-
#validate_single_value(_custom_field, _value, _customizable = nil) ⇒ Object
Override this in subclass to validate custom values.
-
#value_from_keyword(custom_field, keyword, object) ⇒ Object
Checks if a keyword is in the possible values and returns the possible value if found If there are no possible values, returns the keyword.
Class Method Details
.available_types ⇒ Object
20 21 22 |
# File 'lib/custom_attributes/field_type.rb', line 20 def self.available_types descendants end |
.find(name) ⇒ Object
24 25 26 27 28 |
# File 'lib/custom_attributes/field_type.rb', line 24 def self.find(name) "CustomAttributes::#{name}FieldType".constantize.instance rescue NameError nil end |
Instance Method Details
#after_save_custom_value(custom_field, custom_value) ⇒ Object
53 |
# File 'lib/custom_attributes/field_type.rb', line 53 def after_save_custom_value(custom_field, custom_value); end |
#before_custom_field_save(custom_field) ⇒ Object
55 |
# File 'lib/custom_attributes/field_type.rb', line 55 def before_custom_field_save(custom_field); end |
#cast_custom_value(custom_value) ⇒ Object
Cast the value of an existing CustomValue
70 71 72 |
# File 'lib/custom_attributes/field_type.rb', line 70 def cast_custom_value(custom_value) cast_value(custom_value.custom_field, custom_value.value, custom_value.customizable) end |
#cast_single_value(_custom_field, value, _customizable = nil) ⇒ Object
88 89 90 |
# File 'lib/custom_attributes/field_type.rb', line 88 def cast_single_value(_custom_field, value, _customizable = nil) value.to_s end |
#cast_value(custom_field, value, customizable = nil) ⇒ Object
Cast the value according to field type rules
75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/custom_attributes/field_type.rb', line 75 def cast_value(custom_field, value, customizable = nil) if value.blank? nil elsif value.is_a?(Array) casted = value.map do |v| cast_single_value(custom_field, v, customizable) end casted.compact.sort else cast_single_value(custom_field, value, customizable) end end |
#edit_tag(view, tag_id, tag_name, custom_value, options = {}) ⇒ Object
Returns the HTML Tag to edit this field format.
148 149 150 |
# File 'lib/custom_attributes/field_type.rb', line 148 def edit_tag(view, tag_id, tag_name, custom_value, = {}) view.text_field_tag(tag_name, custom_value.value, .merge(id: tag_id)) end |
#label ⇒ Object
16 17 18 |
# File 'lib/custom_attributes/field_type.rb', line 16 def label "label_#{name}" end |
#name ⇒ Object
12 13 14 |
# File 'lib/custom_attributes/field_type.rb', line 12 def name self.class.type_name end |
#possible_custom_value_options(custom_value) ⇒ Object
Use to get the possible values of a CustomValue
138 139 140 |
# File 'lib/custom_attributes/field_type.rb', line 138 def (custom_value) (custom_value.custom_field, custom_value.customizable) end |
#possible_values_options(_custom_field, _object = nil) ⇒ Object
Override this in subclass to specify the possible values for the field with this type
143 144 145 |
# File 'lib/custom_attributes/field_type.rb', line 143 def (_custom_field, _object = nil) [] end |
#set_custom_field_value(_custom_field, _custom_field_value, value) ⇒ Object
Prepares and sets CustomFieldValues value.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/custom_attributes/field_type.rb', line 58 def set_custom_field_value(_custom_field, _custom_field_value, value) if value.is_a?(Array) value = value.map(&:to_s).reject { |v| v == '' }.uniq value << '' if value.empty? else value = value.to_s end value end |
#validate_custom_field(_custom_field) ⇒ Object
Overide this in subclass to validate custom fields
49 50 51 |
# File 'lib/custom_attributes/field_type.rb', line 49 def validate_custom_field(_custom_field) [] end |
#validate_custom_value(custom_value) ⇒ Object
Validate a CustomValue according to the type roles. This method can be overridden by field types if the way bulk handling should happen changes (e.g. List type)
33 34 35 36 37 38 39 40 41 |
# File 'lib/custom_attributes/field_type.rb', line 33 def validate_custom_value(custom_value) # 1. Wrap in Array so we can pass Array of values and single values, reject empty values values = Array.wrap(custom_value.value).reject { |value| value.to_s == '' } # 2. Validate each value errors = values.map do |value| validate_single_value(custom_value.custom_field, value, custom_value.customizable) end errors.flatten.uniq end |
#validate_single_value(_custom_field, _value, _customizable = nil) ⇒ Object
Override this in subclass to validate custom values
44 45 46 |
# File 'lib/custom_attributes/field_type.rb', line 44 def validate_single_value(_custom_field, _value, _customizable = nil) [] end |
#value_from_keyword(custom_field, keyword, object) ⇒ Object
Checks if a keyword is in the possible values and returns the possible value if found If there are no possible values, returns the keyword
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/custom_attributes/field_type.rb', line 94 def value_from_keyword(custom_field, keyword, object) = (custom_field, object) if .present? parse_keyword(custom_field, keyword) do |k| if v = .detect { |text, _id| k.casecmp(text) == 0 } if v.is_a?(Array) v.last else v end end end else keyword end end |