Module: RenderRadiant

Defined in:
lib/render_radiant.rb

Constant Summary collapse

RADIANT_DEFAULT_METHOD_ASSIGNS =
[:flash, :params]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(kls) ⇒ Object

Override the default render method to render for Radiant



6
7
8
# File 'lib/render_radiant.rb', line 6

def self.included(kls)
  kls.send(:alias_method_chain, :render, :render_radiant)
end

Instance Method Details

#render_for_radiant(page, local_assigns) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/render_radiant.rb', line 92

def render_for_radiant(page, local_assigns)

  # Collect values to assign
  values = {}

  # Collect values returned from methods
  RADIANT_DEFAULT_METHOD_ASSIGNS.each do |m|
    values[m] = self.send(m)
  end

  # Collect values assigned via the locals option
  local_assigns ||= {}
  local_assigns.each do |name, value|
    values[name] = value
  end

  # Collect ivars from the action
  ivars = self.instance_variable_names
  ivars -= self.protected_instance_variables
  ivars.each do |name|
    values[name.gsub(/@/,'')] = self.instance_variable_get(name)
  end

  # Assign each value to the page context
  page.send(:lazy_initialize_parser_and_context)
  context = page.instance_variable_get(:@context)
  values.each do |k,v|
    context.globals.send "#{k}=", v
  end

  # WillPaginate loves this
  page.instance_variable_set(
    :@url,
    ActionController::UrlRewriter.new(request, params.clone)
  )

  # Let ActionController know we're rendering the page
  @performed_render = true

  page.process(request, response)
end

#render_with_render_radiant(options = nil, extra_options = {}, &block) ⇒ Object

Use Radiant to render the page matching the passed in URL.

Usage:

EventsController < ActionController::Base

  def index
    @events = Event.all
    render :radiant
  end

  def show
    @event = Event.find(params[:id])
    render :radiant, :locals => { :cool_event => @event.cool? }
  end

end

Page attributes can be overridden and/or set by passing in through the :radiant options hash:

render :radiant => { :title => @event.name, :breadcrumb => @event.name }

By default all instance variables declared in the action are assigned to the Radiant page’s context to be called from your Radius tags.

The url matching the controller and action name is used for rendering.

To use a different url for rendering, pass in the :action option:

render :radiant, :action => 'myaction'

Of course you will need to create a page tree in Radiant that matches the url of the page you are wanting to render.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/render_radiant.rb', line 45

def render_with_render_radiant(options = nil, extra_options = {}, &block)
  if options &&
     ( (options.is_a?(Symbol) && options == :radiant) ||
       (options.is_a?(Hash) && options.keys.first == :radiant) )

    # Bringing this in from original render
    raise DoubleRenderError, "Can only render or redirect once per action" if performed?
    validate_render_arguments(options, extra_options, block_given?)

    options = (options.is_a?(Hash) ? options[:radiant] : nil)

    # Retrieve the action and controller to form a URL
    split_action = extra_options[:action].split('/') if extra_options[:action]
    if split_action
      action, controller = split_action
    else
      action, controller = params[:action], params[:controller]
    end

    # Assume the URL will be formatted like /controller_name/action_name or
    # /controller_name if calling the index action
    url = "/#{controller}"
    url << "/#{action}" if action != 'index'

    page = Page.find_by_url(url)

    # Collect page overrides
    # Set cache to false by default
    page_overrides = {
      :cache => false
    }

    page_overrides.merge!(options) if options

    # Override the page instance with any passed in customizations
    page_overrides.each do |k,v|
      blk = proc { v }
      kls = (class << page; self; end)
      kls.send(:define_method, k, blk)
    end

    render_for_radiant(page, extra_options[:locals])
  else
    render_without_render_radiant(options, extra_options, &block)
  end
end