Module: CustomAttributes::ActsAsCustomField::InstanceMethods

Defined in:
lib/custom_attributes/acts_as/acts_as_custom_field.rb

Instance Method Summary collapse

Instance Method Details

#after_save_custom_value(custom_value) ⇒ Object

Called after CustomValue has been saved Overrideable through FieldType



114
115
116
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 114

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, …)



132
133
134
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 132

def cast_value(value)
  type.cast_value(self, value)
end

#customizable_classObject



168
169
170
171
172
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 168

def customizable_class
  model_type.gsub('CustomField', '').constantize
rescue NameError
  false
end

#decrement_positionObject



160
161
162
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 160

def decrement_position
  change_position_by(-1)
end

#field_type=(arg) ⇒ Object



143
144
145
146
147
148
149
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 143

def field_type=(arg)
  # cannot change type of a saved custom field
  if new_record?
    @type = nil
    super
  end
end

#increment_positionObject



164
165
166
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 164

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



69
70
71
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 69

def possible_custom_value_options(custom_value)
  type.possible_custom_value_options(custom_value)
end

#possible_valuesObject

Serializer for possible values attribute



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 119

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



151
152
153
154
155
156
157
158
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 151

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



108
109
110
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 108

def set_custom_field_value(custom_field_value, value)
  type.set_custom_field_value(self, custom_field_value, value)
end

#typeObject



41
42
43
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 41

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

Returns:

  • (Boolean)


102
103
104
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 102

def valid_field_value?(value)
  validate_field_value(value).empty?
end

#validate_custom_fieldObject

Validate the CustomField according to type rules and check if the selected default value is indeed a valid value for this field



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 75

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, message|
    errors.add attribute, message
  end

  if default.present?
    validate_field_value(default).each do |message|
      errors.add :default, message
    end
  end

  if position.present? && self.class.where(position: position, model_type: model_type).where.not(id: id).where(custom_scope).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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 47

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



97
98
99
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 97

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



139
140
141
# File 'lib/custom_attributes/acts_as/acts_as_custom_field.rb', line 139

def value_from_keyword(keyword, customized)
  type.value_from_keyword(self, keyword, customized)
end