Sha256: ad65ef54bcaf0d28a597679d3c9f291cc0f8e3d1a19194e76e9b50d519d6a69d

Contents?: true

Size: 945 Bytes

Versions: 3

Compression:

Stored size: 945 Bytes

Contents

module Blacksand
  class Field < ActiveRecord::Base
    extend Enumerize

    belongs_to :prototype
    has_many :properties, dependent: :destroy

    validates :name, :description, :field_type, presence: true
    validates :options, presence: true, if: 'select?'

    enumerize :field_type, in: %w{date number string textarea rich_text image gallery array slide file select page}
    serialize :options, JSON

    default_scope { order(:id) }

    after_save :check_properties

    protected

    def select?
      field_type == 'select'
    end

    def check_properties
      # 根据 prototype 查询 page 会更高效 @gaohui 2015.11.25
      self.prototype.pages.find_each do |page|
        (self.prototype.fields.map(&:id) - page.properties.map(&:field_id)).each do |field_id|
          field = Field.find field_id
          Property.build_property(page, field)
        end
        page.save(validate: false)
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
blacksand-2.5.0 app/models/blacksand/field.rb
blacksand-2.4.1 app/models/blacksand/field.rb
blacksand-2.4.0 app/models/blacksand/field.rb