Sha256: d4ffa1f8595f08dc4fbe1b82fbdb964d61970598f397a06b38cf7abd39bd42d5

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

module LucyCMS::ControllerMethods
  
  def self.included(base)
    
    # If application controller doesn't have template associated with it
    # CMS will attempt to find one. This is so you don't have to explicitly
    # call render :cms_page => '/something'
    base.rescue_from 'ActionView::MissingTemplate' do |e|
      begin
        render :cms_page => request.path
      rescue ActionView::MissingTemplate
        raise e
      end
    end
    
    # Now you can render cms_page simply by calling:
    #   render :cms_page => '/path/to/page'
    # This way application controllers can use CMS content while populating
    # instance variables that can be used in partials (that are included by
    # by the cms page and/or layout)
    def render(options = {}, locals = {}, &block)
      if options.is_a?(Hash) && path = options.delete(:cms_page)
        site = CmsSite.find_by_hostname(request.host.downcase)
        page = CmsPage.load_from_file(site, path) if site && LucyCMS.configuration.seed_data_path
        page ||= site && site.cms_pages.find_by_full_path(path)
        if page
          cms_app_layout = page.cms_layout.try(:app_layout)
          options[:layout] ||= cms_app_layout.blank?? nil : cms_app_layout
          options[:inline] = page.content
          @cms_page = page
          super(options, locals, &block)
        else
          raise ActionView::MissingTemplate.new([path], path, "CMS page not found", nil)
        end
      else
        super(options, locals, &block)
      end
    end
  end
end

ActionController::Base.send :include, LucyCMS::ControllerMethods

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lucy_cms-0.0.3 lib/LucyCMS/controller_methods.rb
lucy_cms-0.0.2 lib/LucyCMS/controller_methods.rb
lucy_cms-0.0.1 lib/LucyCMS/controller_methods.rb