Sha256: d04f71be94559d86fad09f0bd638d43f74e8c121ec0f874724fe0ed91241fd0c

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

require 'action_controller/base'

module Motr
  module Controller #:nodoc:
    
    ##
    # 
    # A custom responder which extends the default functionality of respond_with to handle
    # more advanced javascript functionality, as well as flash messages.
    # 
    # @see The ActionController::Metal::Responder (https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/responder.rb) for more info
    # 
    class Responder <  ActionController::Responder #:nodoc:
      
      # @private
      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
      
      ##
      # Override to_js to set a +X-Flash-Messages+ header to be utilized by an ajax response.
      # @private
      # 
      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
      
    end
    
  end  
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
motr-0.1.1 lib/motr/controller/responder.rb
motr-0.1.0 lib/motr/controller/responder.rb
motr-0.0.9 lib/motr/controller/responder.rb
motr-0.0.8 lib/motr/controller/responder.rb
motr-0.0.7 lib/motr/controller/responder.rb
motr-0.0.6 lib/motr/controller/responder.rb
motr-0.0.5 lib/motr/controller/responder.rb
motr-0.0.4 lib/motr/controller/responder.rb