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, …).
- #customizable_class ⇒ Object
- #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
112 113 114 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 112 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, …)
130 131 132 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 130 def cast_value(value) type.cast_value(self, value) end |
#customizable_class ⇒ Object
166 167 168 169 170 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 166 def customizable_class model_type.gsub('CustomField', '').constantize rescue NameError false end |
#decrement_position ⇒ Object
158 159 160 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 158 def decrement_position change_position_by(-1) end |
#field_type=(arg) ⇒ Object
141 142 143 144 145 146 147 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 141 def field_type=(arg) # cannot change type of a saved custom field if new_record? @type = nil super end end |
#increment_position ⇒ Object
162 163 164 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 162 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
67 68 69 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 67 def (custom_value) type.(custom_value) end |
#possible_values ⇒ Object
Serializer for possible values attribute
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 117 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
149 150 151 152 153 154 155 156 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 149 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
106 107 108 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 106 def set_custom_field_value(custom_field_value, value) type.set_custom_field_value(self, custom_field_value, value) end |
#type ⇒ Object
39 40 41 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 39 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
100 101 102 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 100 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
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 73 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 45 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
95 96 97 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 95 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
137 138 139 |
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 137 def value_from_keyword(keyword, customized) type.value_from_keyword(self, keyword, customized) end |