Sha256: fd9845f0351f35c30319db3db35da736842bc97c36082e5b07dcb84ee8fd71c6

Contents?: true

Size: 1.71 KB

Versions: 90

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module ActionController #:nodoc:
  module Flash
    extend ActiveSupport::Concern

    included do
      class_attribute :_flash_types, instance_accessor: false, default: []

      delegate :flash, to: :request
      add_flash_types(:alert, :notice)
    end

    module ClassMethods
      # Creates new flash types. You can pass as many types as you want to create
      # flash types other than the default <tt>alert</tt> and <tt>notice</tt> in
      # your controllers and views. For instance:
      #
      #   # in application_controller.rb
      #   class ApplicationController < ActionController::Base
      #     add_flash_types :warning
      #   end
      #
      #   # in your controller
      #   redirect_to user_path(@user), warning: "Incomplete profile"
      #
      #   # in your view
      #   <%= warning %>
      #
      # This method will automatically define a new method for each of the given
      # names, and it will be available in your views.
      def add_flash_types(*types)
        types.each do |type|
          next if _flash_types.include?(type)

          define_method(type) do
            request.flash[type]
          end
          helper_method type

          self._flash_types += [type]
        end
      end
    end

    private
      def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
        self.class._flash_types.each do |flash_type|
          if type = response_status_and_flash.delete(flash_type)
            flash[flash_type] = type
          end
        end

        if other_flashes = response_status_and_flash.delete(:flash)
          flash.update(other_flashes)
        end

        super(options, response_status_and_flash)
      end
  end
end

Version data entries

90 entries across 90 versions & 5 rubygems

Version Path
actionpack-5.2.8.1 lib/action_controller/metal/flash.rb
actionpack-5.2.8 lib/action_controller/metal/flash.rb
actionpack-5.2.7.1 lib/action_controller/metal/flash.rb
actionpack-5.2.7 lib/action_controller/metal/flash.rb
actionpack-5.2.6.3 lib/action_controller/metal/flash.rb
actionpack-5.2.6.2 lib/action_controller/metal/flash.rb
actionpack-5.2.6.1 lib/action_controller/metal/flash.rb
actionpack-5.2.6 lib/action_controller/metal/flash.rb
actionpack-5.2.4.6 lib/action_controller/metal/flash.rb
actionpack-5.2.5 lib/action_controller/metal/flash.rb
actionpack-5.2.4.5 lib/action_controller/metal/flash.rb
actionpack-5.2.4.4 lib/action_controller/metal/flash.rb
actionpack-5.2.4.3 lib/action_controller/metal/flash.rb
actionpack-5.2.4.2 lib/action_controller/metal/flash.rb
actionpack-5.2.4.1 lib/action_controller/metal/flash.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/flash.rb
actionpack-5.2.4 lib/action_controller/metal/flash.rb
actionpack-5.2.4.rc1 lib/action_controller/metal/flash.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/flash.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-5.2.3/lib/action_controller/metal/flash.rb