Class: Motr::Controller::Responder

Inherits:
ActionController::Responder
  • Object
show all
Defined in:
lib/motr/controller/responder.rb

Overview

A custom responder which extends the default functionality of respond_with to handle more advanced javascript functionality, as well as flash messages.

See Also:

  • ActionController::Metal::Responder (https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/responder.rb) for more info

Instance Method Summary collapse

Constructor Details

#initialize(controller, resources, options = {}) ⇒ Responder

Returns a new instance of Responder.



16
17
18
19
20
21
22
# File 'lib/motr/controller/responder.rb', line 16

def initialize(controller, resources, options = {})
  super
  flash = (format == :js) ? controller.flash : controller.flash.now
  [:success, :notice, :error].each do |key|
    flash[key] = options.delete(key) if options[key].present?
  end        
end

Instance Method Details

#to_jsObject

Override to_js to set a X-Flash-Messages header to be utilized by an ajax response.



28
29
30
31
32
# File 'lib/motr/controller/responder.rb', line 28

def to_js
  flash = controller.flash.now
  controller.response['X-Flash-Messages'] = flash.to_json if [:success, :notice, :error].detect{ |key| flash[key].present? }
  defined?(super) ? super : to_format        
end