cirandas.net

ref: master

plugins/custom_forms/lib/custom_forms_plugin/field.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class CustomFormsPlugin::Field < ApplicationRecord
  self.table_name = :custom_forms_plugin_fields

  validates_presence_of :name
  validates_length_of :default_value, :maximum => 255

  attr_accessible :name, :form, :mandatory, :type, :position, :default_value, :show_as, :alternatives_attribute

  belongs_to :form, :class_name => 'CustomFormsPlugin::Form'
  has_many :answers, :class_name => 'CustomFormsPlugin::Answer', :dependent => :destroy

  has_many :alternatives, -> { order 'position' }, class_name: 'CustomFormsPlugin::Alternative'
  accepts_nested_attributes_for :alternatives, :allow_destroy => true
  #FIXME This validation should be in the subclass, but since we are using Single Table
  # Inheritance we are instantiating a Field object with the type as a param. So the validation
  # had to go here or rails would skip it.
  validates_length_of :alternatives, :minimum => 1, :message => 'can\'t be empty', :if => Proc.new { |f| f.type == 'CustomFormsPlugin::SelectField' }

  before_validation do |field|
    field.slug = field.name.to_slug if field.name.present?
  end

  private

  def attributes_protected_by_default
    super - [self.class.inheritance_column]
  end

end