Module: Reststop::Base

Defined in:
lib/reststop.rb

Instance Method Summary collapse

Instance Method Details

#basic_render(action) ⇒ Object

Performs a basic camping rendering (without use of a layout) This method was added since the addition of Tilt support in camping is assuming layout.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/reststop.rb', line 174

def basic_render(action)
	app_name = self.class.name.split("::").first							# @techarch : get the name of the app
       	mab = (app_name + '::Mab').constantize								# @techarch : get the Mab class
  		m = mab.new({}, self)														# @techarch : instantiate Mab
	
	tpl = lookup(action)	# check if we have a Tilt template
	
	raise "Can't find action #{action}" unless tpl
	
	s = (tpl == true) ? m.capture{m.send(action)} : tpl.render(self, {}) # if so render it
	if /^_/!~action.to_s
		layout_tpl = lookup(:layout)
		if layout_tpl
			b = Proc.new { s }
			s = (layout_tpl == true) ? m.capture{send(:layout){s}} : layout_tpl.render(m, {},&b) # if so render it
		end
	end
	s
end

#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

#reststop_service(*a) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/reststop.rb', line 58

def reststop_service(*a)
  if @env['REQUEST_METHOD'] == 'POST' && (input['_method'] == 'put' || input['_method'] == 'delete')
    @env['REQUEST_METHOD'] = input._method.upcase
    @method = input._method
  end
 @a0=a[0] if !a.empty?
  camping_service(*a)
end