Sha256: 06554208725913858b0ae5bdffddc2b3a7702165b391d6c3c50e35558af6f86f

Contents?: true

Size: 1.62 KB

Versions: 1

Compression:

Stored size: 1.62 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] << to_flushing_flash_message(message_type, flash_content)
      end
    
      def pull_flash(target=:default)
        @pulled_flashes ||= {}
        return @pulled_flashes[target] if @pulled_flashes[target]
        
        pulled_flashes = flash[target] || []
        flash.delete(target)
        
        if pulled_flashes.is_a?(Array)
          @pulled_flashes[target] = pulled_flashes
        else
          @pulled_flashes[target] = [to_flushing_flash_message(target, pulled_flashes)]
        end

        @pulled_flashes[target]
      end
      
      def to_flushing_flash_message(message_type, flash_content)
        { message_type: message_type, content: flash_content }
      end
    end
  end
end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flushing-flash-0.4.1 lib/flushing-flash/action_controller_methods.rb