Module: AjaxPagination::ControllerAdditions::ClassMethods

Defined in:
lib/ajax_pagination/controller_additions.rb

Instance Method Summary collapse

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.



33
34
35
36
37
38
39
40
41
42
43
44
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
# File 'lib/ajax_pagination/controller_additions.rb', line 33

def ajax_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_sym
  view = options[:render] || {}
  view = { :action => view } if view.kind_of? String
  only = options[:only] ? Array(options[:only]) : nil
  except = 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_sym
      render = nil # nil means call original default_render method
      if ajax_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)
        else
          render = _ajax_respond[ajax_section][0] # use default render hash for this ajax_section (can still be nil)
        end
      end
      if !render.nil?
        # AJAX response overriding normal default_render behaviour
        respond_to do |format|
          ajax_respond format, :section_id => ajax_section, :render => render
        end
      else # otherwise do what would have been done
        default_render.bind(self).call(*args) # call original method of the same name
      end
    end
  end

  @_ajax_respond[section] ||= [nil,{}]
  if only.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
  end
end