Sha256: fb8126da6049e9b60890208ca4bcad5f9025db1944246e0ff0ff74f19f0220ab

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

module ComfortableMexicanSofa::ControllerMethods
  
  def self.included(base)
    base.alias_method_chain :render, :cms
    
    # 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
  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_with_cms(options = {}, locals = {}, &block)
    if path = options.delete(:cms_page)
      site = CmsSite.find_by_hostname(request.host.downcase)
      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
        render_without_cms(options, locals, &block)
      else
        raise ActionView::MissingTemplate.new([path], path, "CMS page not found", nil)
      end
    else
      render_without_cms(options, locals, &block)
    end
  end
end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.0.7 lib/comfortable_mexican_sofa/controller_methods.rb
comfortable_mexican_sofa-1.0.6 lib/comfortable_mexican_sofa/controller_methods.rb
comfortable_mexican_sofa-1.0.5 lib/comfortable_mexican_sofa/controller_methods.rb
comfortable_mexican_sofa-1.0.4 lib/comfortable_mexican_sofa/controller_methods.rb
comfortable_mexican_sofa-1.0.3 lib/comfortable_mexican_sofa/controller_methods.rb
comfortable_mexican_sofa-1.0.2 lib/comfortable_mexican_sofa/controller_methods.rb