Sha256: 8bd7c37e7e36ab32f22ea6bc7a8594e1d501ddebdee3359221710e5125595fca

Contents?: true

Size: 1.27 KB

Versions: 3

Compression:

Stored size: 1.27 KB

Contents

module Cardboard
  class PagePart < ActiveRecord::Base
    has_many :fields, :as => :object_with_field, class_name: "Cardboard::Field", :dependent => :destroy, :inverse_of => :object_with_field

    belongs_to :page

    accepts_nested_attributes_for :fields #, :allow_destroy => true (maybe for super admin?)

    validates :identifier, :format => {:with => /\A[a-z\_0-9]+\z/, :message => "Only downcase letters, numbers and underscores are allowed"} # uniqueness: {:case_sensitive => false, :scope => :page_id}, 
    validates :page, :identifier, presence: true                  
    # validates_associated :fields


    #gem
    include RankedModel
    ranks :part_position, :with_same => :page_id, :column => :position
    default_scope {order("position ASC")}


    def repeatable?
      template_hash[:repeatable]
    end

    def template_hash
      @template ||= self.page.template.fields[self.identifier.to_sym]
    end

    def template
      template_hash[:fields]
    end

    def attr(field)
      field = field.to_s
      @attr ||= {}
      @attr[field] ||= begin
        f = self.fields.where(identifier: field).first
        return nil unless f
        out = f.value_uid.nil? ? nil : f.value
        out = f.default if f.required? && out.nil?
        out
      end
    end

    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cardboard_cms-0.3.1 app/models/cardboard/page_part.rb
cardboard_cms-0.2.2 app/models/cardboard/page_part.rb
cardboard_cms-0.2.1 app/models/cardboard/page_part.rb