Sha256: 5730dbbb570fe92d174c9ca6103e4ca4ea76bb8ad5ca090fe3b385e7efa003fc

Contents?: true

Size: 1.4 KB

Versions: 13

Compression:

Stored size: 1.4 KB

Contents

class Layout < LiquidTemplate

  ## associations ##
  has_many_related :pages
  embeds_many :parts, :class_name => 'PagePart'
  
  ## callbacks ##
  before_save :build_parts_from_value
  after_save :update_parts_in_pages
  
  ## validations ##
  validates_format_of :value, :with => Locomotive::Regexps::CONTENT_FOR_LAYOUT, :message => :missing_content_for_layout
  
  ## methods ##
    
  protected
  
  def build_parts_from_value
    if self.value_changed? || self.new_record?
      self.parts.each { |p| p.disabled = true }
      
      self.value.scan(Locomotive::Regexps::CONTENT_FOR).each do |attributes|
        slug = attributes[0].strip.downcase
        name = attributes[1].strip.gsub("\"", '')    
        name = nil if name.empty?
        name ||= I18n.t('attributes.defaults.page_parts.name') if slug == 'layout'
        
        if part = self.parts.detect { |p| p.slug == slug }
          part.name = name if name.present?
          part.disabled = false
        else
          self.parts.build :slug => slug, :name => name || slug
        end        
      end
      
      # body always first
      body = self.parts.detect { |p| p.slug == 'layout' }
      self.parts.delete(body)
      self.parts.insert(0, body)
      
      @_update_pages = true if self.value_changed?
    end
  end
  
  def update_parts_in_pages
    self.pages.each { |p| p.send(:update_parts!, self.parts) } if @_update_pages
  end  
  
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
locomotive_cms-0.0.2.7 app/models/layout.rb
locomotive_cms-0.0.2.6 app/models/layout.rb
locomotive_cms-0.0.2.5 app/models/layout.rb
locomotive_cms-0.0.2.4 app/models/layout.rb
locomotive_cms-0.0.2.3 app/models/layout.rb
locomotive_cms-0.0.2.2 app/models/layout.rb
locomotive_cms-0.0.2.1 app/models/layout.rb
locomotive_cms-0.0.2 app/models/layout.rb
locomotive_cms-0.0.1.4 app/models/layout.rb
locomotive_cms-0.0.1.3 app/models/layout.rb
locomotive_cms-0.0.1.2 app/models/layout.rb
locomotive_cms-0.0.1.1 app/models/layout.rb
locomotive_cms-0.0.1 app/models/layout.rb