Sha256: 37ce9e19a470bea44ba5fb9bc968d83010df73ab6cb7aa73b893a78fa5fbb541

Contents?: true

Size: 1.23 KB

Versions: 1

Compression:

Stored size: 1.23 KB

Contents

require 'action_controller/base'

module Motr
  module Controller
    
    ##
    # 
    # A custom responder which extends the default functionality of respond_with to handle
    # more advanced javascript functionality, as well as flash messages.
    # 
    # Slightly modeled after José Valim's Responders gem (https://github.com/plataformatec/responders)
    # 
    # @see (https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/responder.rb) for more info
    # 
    class Responder <  ActionController::Responder
      
      def initialize(controller, resources, options = {}) #:nodoc:
        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.
      # 
      def to_js #:nodoc:
        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

1 entries across 1 versions & 1 rubygems

Version Path
motr-0.0.3 lib/motr/controller/responder.rb