module CustomAttributes::ActsAsCustomField::ClassMethods

Public Instance Methods

acts_as_custom_field(_options = {}) click to toggle source
# File lib/custom_attributes/acts_as/acts_as_custom_field.rb, line 9
def acts_as_custom_field(_options = {})
  include CustomAttributes::ActsAsCustomField::InstanceMethods

  scope :sorted, -> { order(:position) }

  serialize :possible_values

  attr_accessor :edit_tag_style

  has_many :custom_values, dependent: :delete_all

  validates_numericality_of :min_length, only_integer: true, greater_than_or_equal_to: 0
  validates_numericality_of :max_length, only_integer: true, greater_than_or_equal_to: 0
  validates_presence_of :name, :field_type
  validates_length_of :name, maximum: 30
  validates_inclusion_of :field_type, in: proc { CustomAttributes::FieldType.available_types.map { |ft| ft.name.gsub(/CustomAttributes::|FieldType/, '') } }
  validate :validate_custom_field

  before_create do |field|
    field.set_slug

    field.ensure_position_integrity
    field.set_position
  end

  before_save do |field|
    field.type.before_custom_field_save(field)
  end
end