Module: CustomAttributes::ActsAsCustomField::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#acts_as_custom_field(_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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