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



43
44
45
# File 'lib/alchemy/on_page_layout.rb', line 43

def self.callbacks
  @callbacks
end

.register_callback(page_layout, callback) ⇒ Object

Registers a callback for given page layout



48
49
50
51
52
# File 'lib/alchemy/on_page_layout.rb', line 48

def self.register_callback(page_layout, callback)
  @callbacks ||= {}
  @callbacks[page_layout] ||= Set.new
  @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.



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

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