module CustomAttributes::ActsAsCustomField::InstanceMethods

Public Instance Methods

after_save_custom_value(custom_value) click to toggle source

Called after CustomValue has been saved Overrideable through FieldType

# 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) click to toggle source

Returns the value in type specific form (Integer, Float, …)

# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 132
def cast_value(value)
  type.cast_value(self, value)
end
customizable_class() click to toggle source
# 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_position() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 160
def decrement_position
  change_position_by(-1)
end
field_type=(arg) click to toggle source
Calls superclass method
# 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_position() click to toggle source
# 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) click to toggle source

Returns possible options that are determined by the FieldType Don't mistake for possible_values, which is a CustomField specific dynamic setting

# 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_values() click to toggle source

Serializer for possible values attribute

# 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) click to toggle source
# 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) click to toggle source

Used to set the value of CustomFieldValue. No database persistance happening. A convenient way to override how values are being parsed via FieldType

# 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
type() click to toggle source
# 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) click to toggle source

Helper function to check if a value is a valid value for this field

# 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_field() click to toggle source

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

# 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) click to toggle source

Called upon Customizable Model validation Actual validation handled by FieldType

# 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) click to toggle source

Helper function used in validate_custom_field

# 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) click to toggle source

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

# 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

Protected Instance Methods

change_position_by(diff) click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 188
def change_position_by(diff)
  new_pos = position + diff unless position.nil?

  swap_field = self.class.find_by(model_type: model_type, position: new_pos).where(custom_scope)

  if swap_field.present?
    swap_field.position = position
    swap_field.save(validate: false)

    self.position = new_pos
    save
  end
end
create_slug(iterator = 0) click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 202
def create_slug(iterator = 0)
  new_slug = name.strip.gsub(/([^A-Za-z0-9])+/) { '_' }.downcase

  new_slug = "#{new_slug}_#{iterator}" unless iterator == 0
  new_slug = create_slug(iterator += 1) unless CustomField.where(slug: new_slug).where(custom_scope).count == 0
  new_slug
end
custom_scope() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 210
def custom_scope
  ""
end
ensure_position_integrity() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 214
def ensure_position_integrity
  expected_position = 1
  self.class.where(model_type: model_type).order(position: :asc).where(custom_scope).each do |field|
    if expected_position != field.position
      field.position = expected_position
      field.save
    end

    expected_position += 1
  end
end
set_position() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 180
def set_position
  return unless position.nil?

  last_position = self.class.where(model_type: model_type).where(custom_scope).order(position: :desc).first.try(:position)
  self.position = 1
  self.position = last_position + 1 unless last_position.nil?
end
set_slug() click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 176
def set_slug
  self.slug = create_slug
end