Module: CustomAttributes::ActsAsCustomField::InstanceMethods
- Defined in:
- lib/custom_attributes/acts_as/acts_as_custom_field.rb
Instance Method Summary collapse
-
#after_save_custom_value(custom_value) ⇒ Object
Called after CustomValue has been saved Overrideable through FieldType.
-
#cast_value(value) ⇒ Object
Returns the value in type specific form (Integer, Float, …).
- #decrement_position ⇒ Object
- #field_type=(arg) ⇒ Object
- #increment_position ⇒ Object
-
#possible_custom_value_options(custom_value) ⇒ Object
Returns possible options that are determined by the FieldType Don’t mistake for possible_values, which is a CustomField specific dynamic setting.
-
#possible_values ⇒ Object
Serializer for possible values attribute.
- #possible_values=(arg) ⇒ Object
-
#set_custom_field_value(custom_field_value, value) ⇒ Object
Used to set the value of CustomFieldValue.
- #type ⇒ Object
-
#valid_field_value?(value) ⇒ Boolean
Helper function to check if a value is a valid value for this field.
-
#validate_custom_field ⇒ Object
Validate the CustomField according to type rules and check if the selected default value is indeed a valid value for this field.
-
#validate_custom_value(custom_value) ⇒ Object
Called upon Customizable Model validation Actual validation handled by FieldType.
-
#validate_field_value(value) ⇒ Object
Helper function used in validate_custom_field.
-
#value_from_keyword(keyword, customized) ⇒ Object
Finds a value in a field that has predefined possible values.
Instance Method Details
#after_save_custom_value(custom_value) ⇒ Object
Called after CustomValue has been saved Overrideable through FieldType
113 114 115 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 113 def after_save_custom_value(custom_value) type.after_save_custom_value(self, custom_value) end |
#cast_value(value) ⇒ Object
Returns the value in type specific form (Integer, Float, …)
131 132 133 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 131 def cast_value(value) type.cast_value(self, value) end |
#decrement_position ⇒ Object
159 160 161 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 159 def decrement_position change_position_by(-1) end |
#field_type=(arg) ⇒ Object
142 143 144 145 146 147 148 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 142 def field_type=(arg) # cannot change type of a saved custom field if new_record? @type = nil super end end |
#increment_position ⇒ Object
163 164 165 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 163 def increment_position change_position_by(1) end |
#possible_custom_value_options(custom_value) ⇒ Object
Returns possible options that are determined by the FieldType Don’t mistake for possible_values, which is a CustomField specific dynamic setting
68 69 70 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 68 def (custom_value) type.(custom_value) end |
#possible_values ⇒ Object
Serializer for possible values attribute
118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 118 def possible_values values = read_attribute(:possible_values) if values.is_a?(Array) values.each do |value| value.to_s.force_encoding('UTF-8') end values else [] end end |
#possible_values=(arg) ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 150 def possible_values=(arg) if arg.is_a?(Array) values = arg.compact.map { |a| a.to_s.strip }.reject(&:blank?) write_attribute(:possible_values, values) else self.possible_values = arg.to_s.split(/[\n\r]+/) end end |
#set_custom_field_value(custom_field_value, value) ⇒ Object
Used to set the value of CustomFieldValue. No database persistance happening. A convenient way to override how values are being parsed via FieldType
107 108 109 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 107 def set_custom_field_value(custom_field_value, value) type.set_custom_field_value(self, custom_field_value, value) end |
#type ⇒ Object
40 41 42 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 40 def type @type ||= CustomAttributes::FieldType.find(field_type) end |
#valid_field_value?(value) ⇒ Boolean
Helper function to check if a value is a valid value for this field
101 102 103 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 101 def valid_field_value?(value) validate_field_value(value).empty? end |
#validate_custom_field ⇒ Object
Validate the CustomField according to type rules and check if the selected default value is indeed a valid value for this field
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 74 def validate_custom_field if type.nil? errors.add :default, ::I18n.t('activerecord.errors.messages.invalid_type') return end type.validate_custom_field(self).each do |attribute, | errors.add attribute, end if default.present? validate_field_value(default).each do || errors.add :default, end end if position.present? && self.class.where(position: position, model_type: model_type).where.not(id: id).count > 0 errors.add :position, ::I18n.t('activerecord.errors.messages.invalid_position') end end |
#validate_custom_value(custom_value) ⇒ Object
Called upon Customizable Model validation Actual validation handled by FieldType
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 46 def validate_custom_value(custom_value) value = custom_value.value errs = type.validate_custom_value(custom_value) unless errs.any? if value.is_a?(Array) errs << ::I18n.t('activerecord.errors.messages.invalid') unless multiple? if is_required? && value.detect(&:present?).nil? errs << ::I18n.t('activerecord.errors.messages.blank') end else if is_required? && value.blank? errs << ::I18n.t('activerecord.errors.messages.blank') end end end errs end |
#validate_field_value(value) ⇒ Object
Helper function used in validate_custom_field
96 97 98 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 96 def validate_field_value(value) validate_custom_value(CustomAttributes::CustomFieldValue.new(custom_field: self, value: value)) end |
#value_from_keyword(keyword, customized) ⇒ Object
Finds a value in a field that has predefined possible values. Returns array of values if the field supports multiple values Comma delimited keywords possible
138 139 140 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 138 def value_from_keyword(keyword, customized) type.value_from_keyword(self, keyword, customized) end |