Module: Alchemy::OnPageLayout

Defined in:
lib/alchemy/on_page_layout.rb,
lib/alchemy/on_page_layout/callbacks_runner.rb

Overview

Provides a DSL to define callbacks run in a before filter on pages show action

Use this mixin to add the on_page_layout class method into your ApplicationController.

Pass a block or method name in which you have the @page object available and can do everything as if you were in a normal controller action.

You can pass a Alchemy::PageLayout name, an array of names, or :all to evaluate the callback on either some specific or all the pages.

Example:

class ApplicationController < ActionController::Base
  extend Alchemy::OnPageLayout

  on_page_layout :all do
    @my_stuff = Stuff.all
  end

  on_page_layout :contact, :do_something
  on_page_layout [:standard, :home, :news], :do_something_else

  private

  def do_something
    @contacts = Contact.all
    if @page.tag_list.include?('something')
      ...
    end
  end

  def do_something_else
    ...
  end
end

Defined Under Namespace

Modules: CallbacksRunner

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.callbacksObject

All registered callbacks



41
42
43
# File 'lib/alchemy/on_page_layout.rb', line 41

def self.callbacks
  @callbacks
end

.register_callback(page_layout, callback) ⇒ Object

Registers a callback for given page layout



46
47
48
49
50
# File 'lib/alchemy/on_page_layout.rb', line 46

def self.register_callback(page_layout, callback)
  @callbacks ||= {}
  @callbacks[page_layout] ||= []
  @callbacks[page_layout] << callback
end

Instance Method Details

#on_page_layout(page_layouts, callback = nil, &block) ⇒ Object

Define a page layout callback

Pass a block or method name in which you have the @page object available and can do everything as if you were in a normal controller action.

Pass a Alchemy::PageLayout name, an array of names, or :all to evaluate the callback on either some specific or all the pages.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/alchemy/on_page_layout.rb', line 60

def on_page_layout(page_layouts, callback = nil, &block)
  callback = block || callback
  [page_layouts].flatten.each do |page_layout|
    if callback
      OnPageLayout.register_callback(page_layout, callback)
    else
      raise ArgumentError,
        "You need to either pass a block or method name as a callback for `on_page_layout`"
    end
  end
end