Sha256: 974269bacb7e8e438a2454d4cd5ddb9d5d80350a36e50365ca1e81ed1b2d6ec0

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

module Smithy
  class Template < ActiveRecord::Base
    attr_accessible :name, :content, :template_type

    validates_presence_of :name
    validates_uniqueness_of :name, :scope => :template_type
    validates_presence_of :content, :on => :update

    has_many :pages
    has_many :containers, :class_name => "TemplateContainer"

    before_save :uncache_liquid_template_if_content_changed
    after_save :load_containers
    after_save :touch_pages

    default_scope order(:name)
    scope :javascripts, where(:template_type => "javascript")
    scope :partials, where(:template_type => "include")
    scope :stylesheets, where(:template_type => "stylesheet")
    scope :templates, where(:template_type => "template")

    class << self
      def types
        %w(template include javascript stylesheet)
      end
    end
    validates_inclusion_of :template_type, :in => types # has to be after the class method definition

    def liquid_template
      @liquid_template ||= Rails.cache.fetch("#{self.cache_key}-liquid_template") do
        ::Liquid::Template.parse(self.content)
      end
    end

    private
      def load_containers
        return unless self.template_type == 'template'
        container_names = liquid_template.root.nodelist.select{|n| n.is_a?(::Liquid::Variable) && n.name.match(/^page\.container\.(.*)/) }.map{|n| n.name.match(/^page\.container\.(.*)/)[1] }
        # self.containers = container_names.map{|container_name| Smithy::TemplateContainer.new(:name => container_name) }
        self.containers = container_names.inject([]) do |containers, container_name|
          containers.push Smithy::TemplateContainer.new(:name => container_name, :position => containers.size)
        end
      end

      def touch_pages
        self.pages.each(&:touch)
      end

      def uncache_liquid_template_if_content_changed
        @liquid_template = nil if self.content_changed?
      end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
smithycms-0.0.3 app/models/smithy/template.rb
smithycms-0.0.2 app/models/smithy/template.rb
smithycms-0.0.1 app/models/smithy/template.rb