Method: Reststop::Base#reststop_render

Defined in:
lib/reststop.rb

#reststop_render(action, format = nil) ⇒ Object

Overrides Camping’s render method to add the ability to specify a format module when rendering a view.

The format can also be specified in other ways (shown in this order of precedence):

  1. By providing a second parameter to render() (eg: render(:foo, :HTML))

  2. By setting the @format variable

  3. By providing a ‘format’ parameter in the request (i.e. input)

  4. By adding a file-format extension to the url (e.g. /items.xml or /items/2.html).

For example, you could have:

module Foobar::Views

  module HTML
    def foo
      # ... render some HTML content
    end
  end

  module RSS
    def foo
      # ... render some RSS content
    end
  end

end

Then in your controller, you would call render() like this:

render(:foo, :HTML) # render the HTML version of foo

or

render(:foo, :RSS) # render the RSS version of foo

or

@format = :RSS
render(:foo) # render the RSS version of foo

or

# url is /foobar/1?format=RSS
render(:foo) # render the RSS version of foo

or

# url is /foobar/1.rss
render(:foo) # render the RSS version of foo

If no format is specified, render() will behave like it normally does in Camping, by looking for a matching view method directly in the Views module.

You can also specify a default format module by calling default_format after the format module definition. For example:

module Foobar::Views
  module HTML
    # ... etc.
  end
  default_format :HTML
end


136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/reststop.rb', line 136

def reststop_render(action, format = nil)
	  format = nil unless format.is_a? Symbol

	  app_name = self.class.name.split("::").first							# @techarch : get the name of the app
	  format ||= @format

  if format.nil?
    begin
      ct = CONTENT_TYPE
    rescue NameError
      ct = 'text/html'
    end

    @headers['Content-Type'] ||= ct
	basic_render(action) # @techarch
  else
    mab = (app_name + '::Mab').constantize								# @techarch : get the Mab class
	m = mab.new({}, self)																# @techarch : instantiate Mab
	mod = (app_name + "::Views::#{format.to_s}").constantize	# @techarch : get the right Views format class

    m.extend mod

    begin
      ct = mod::CONTENT_TYPE
    rescue NameError
      ct = "#{format == :HTML ? 'text' : 'application'}/#{format.to_s.downcase}" #@techarch - used to be text/***
    end
    @headers['Content-Type'] = ct

    s = m.capture{m.send(action)}
    s = m.capture{send(:layout){s}} if /^_/!~action.to_s and m.respond_to?(:layout)	
    s
  end
end