Module: AjaxPagination::ControllerAdditions::ClassMethods

Adds default render behaviour for AJAX requests for a section with matching
a certain name.

Instance Method Details

#ajax_respond(options = {}) ⇒ Object

Adds default render behaviour for AJAX requests for a section with matching
a certain name. By default, this name is the empty string. However, it can
be changed.

An AJAX request for the matched section will render a partial, which by
default, is the template named “_#ajax_section_name”, or if it does not
exist, the view template matching the controller/action name. By default
there is no layout used.

Options:

:section_id

The AJAX section name which should be matched to invoke AJAX Pagination
response. Defaults to “global”.

:render

Overrides default render behaviour for AJAX Pagination, which is to render
the partial with name matching the section_id option, or if it does not
exist, renders the default template

:only

The actions that may trigger this render behaviour for AJAX requests. If an
action is not included, it will not trigger this behaviour.

:except

Actions which will not trigger this render behaviour for AJAX requests.

# File 'lib/ajax_pagination/controller_additions.rb', line 33defajax_respond(options={});# instead of defining default render normally, we save an unbound reference to original function in case it was already defined, since we want to retain the original behaviour, and add to it (if the method is redefined after, this new behaviour is lost, but at least we don't remove others' behaviour - note that this also allows multiple invocations of this with different parameters)
section_id=options[:section_id]||"global"section=section_id.to_symview=options[:render]||{}view={:action=>view}ifview.kind_of?Stringonly=options[:only]?Array(options[:only]):nilexcept=Array(options[:except])||[]if@_ajax_respond.nil?# has this method been called before?
@_ajax_respond={}_ajax_respond=@_ajax_respond# create reference to class instance variable
# create default_render intercepting method (if not already defined)
default_render=self.instance_method(:default_render)# get a reference to original method
define_method(:default_render)do|*args|action=action_name.to_symrender=nil# nil means call original default_render method
ifajax_section&&_ajax_respond.has_key?(ajax_section)&&request.format=="html"# might override render... let's find out
if_ajax_respond[ajax_section][1].has_key?(action)render=_ajax_respond[ajax_section][1][action]# use action-specific render hash for this ajax_section (can still be nil)
elserender=_ajax_respond[ajax_section][0]# use default render hash for this ajax_section (can still be nil)
endendif!render.nil?# AJAX response overriding normal default_render behaviour
respond_todo|format|ajax_respondformat,:section_id=>ajax_section,:render=>renderendelse# otherwise do what would have been done
default_render.bind(self).call(*args)# call original method of the same name
endendend@_ajax_respond[section]||=[nil,{}]ifonly.nil?except.each{|x|@_ajax_respond[section][1][x.to_sym]=@_ajax_respond[section][0]unless@_ajax_respond[section][1].has_key?x.to_sym}# sets excluded actions to old default (if not already set)
@_ajax_respond[section][0]=view# creates default for all other actions
else@_ajax_respond[section][1]=Hash[(only-except).map{|x|[x.to_sym,view]}]# sets specific actions to view render options
endend