Sha256: 48bb1c8eaadae72d00f27e5c8e0b71fa21bc0a85830d748b059146cf398b1992

Contents?: true

Size: 1.27 KB

Versions: 7

Compression:

Stored size: 1.27 KB

Contents

module FlushingFlash
  module ActionControllerMethods
    def self.included(base)
      base.extend ClassMethods
      base.send :include, InstanceMethods
      base.send(:helper_method, :pull_flash)
    end
    
    module ClassMethods
    end
    
    module InstanceMethods
      def push_flash(message_type, *args)
        opts = args.extract_options!

        i18n_options = opts.delete(:i18n_options)
        target = opts.delete(:target) || :default

        flash_content = case args[0].class.name
        when String.name
          args[0]
        when Symbol.name
          I18n.t("flashes.#{args[0]}.#{message_type}", i18n_options)
        else
          I18n.t("flashes.#{self.class.name.gsub(/Controller$/, "").underscore.gsub(/\//, ".")}.#{action_name.underscore}.#{message_type}", i18n_options)
        end

        flash[target] ||= []
        flash[target] << { message_type: message_type, content: flash_content }
      end
    
      def pull_flash(target=:default)
        @pulled_flashes ||= {}
        return @pulled_flashes[target] if @pulled_flashes[target]

        @pulled_flashes[target] = flash[target] || []
        flash.delete(target)

        @pulled_flashes[target]
      end
    end
  end
end

ActionController::Base.send :include, FlushingFlash::ActionControllerMethods

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
flushing-flash-0.3.0 lib/flushing-flash/action_controller_methods.rb
flushing-flash-0.2.3 lib/flushing-flash/action_controller_methods.rb
flushing-flash-0.2.2 lib/flushing-flash/action_controller_methods.rb
flushing-flash-0.2.1 lib/flushing-flash/action_controller_methods.rb
flushing_flash-0.1.3 lib/flushing_flash/action_controller_methods.rb
flushing_flash-0.1.2 lib/flushing_flash/action_controller_methods.rb
flushing_flash-0.1.0 lib/flushing_flash/action_controller_methods.rb