Module: Ajax::ActionController::Rails2

Defined in:
lib/ajax/action_controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



47
48
49
50
51
52
# File 'lib/ajax/action_controller.rb', line 47

def self.included(klass)
  klass.class_eval do
    alias_method_chain :redirect_to_full_url, :ajax
    alias_method_chain :render, :ajax
  end
end

Instance Method Details

#redirect_to_full_url_with_ajax(url, status) ⇒ Object

Redirect to hashed URLs unless the path is excepted.

Store the URL that we are redirecting to in the session. If we then have a request for the root URL we know to render this URL instead.

If redirecting back to the referer, use the referer in the Ajax-Info header because it includes the hashed part of the URL. Otherwise the referer is always the root url.

For AJAX requests, respond with an AJAX-suitable redirect.

This method only applies to Rails < 3



69
70
71
72
73
# File 'lib/ajax/action_controller.rb', line 69

def redirect_to_full_url_with_ajax(url, status)
  if !_ajax_redirect(url)
    redirect_to_full_url_without_ajax(url, status)
  end
end

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

Intercept rendering to customize the headers and layout handling



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/ajax/action_controller.rb', line 78

def render_with_ajax(options = nil, extra_options = {}, &block)
  return render_without_ajax(options, extra_options, &block) unless Ajax.is_enabled?

  original_args = [options, extra_options]
  if request.xhr?

    # Options processing taken from ActionController::Base#render
    if options.nil?
      options = { :template => default_template, :layout => true }
    elsif options == :update
      options = extra_options.merge({ :update => true })
    elsif options.is_a?(String) || options.is_a?(Symbol)
      case options.to_s.index('/')
      when 0
        extra_options[:file] = options
      when nil
        extra_options[:action] = options
      else
        extra_options[:template] = options
      end
      options = extra_options
    elsif !options.is_a?(Hash)
      extra_options[:partial] = options
      options = extra_options
    end

    default = pick_layout(options)
    default = default.path_without_format_and_extension unless default.nil?
    if ajax_layout = _layout_for_ajax(default)
      if ajax_layout = _find_ajax_layout(ajax_layout)
        options[:layout] = ajax_layout.path_without_format_and_extension
      end
    end
  end
  render_without_ajax(options, extra_options, &block)
end