Sha256: 774f89d2803b6773d9181712ee067cd8321d9a348d9e437418bcfd32b13f74d6

Contents?: true

Size: 1.88 KB

Versions: 23

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

# :markup: markdown

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 `alert` and `notice` 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) if respond_to?(:helper_method)

          self._flash_types += [type]
        end
      end

      def action_methods # :nodoc:
        @action_methods ||= super - _flash_types.map(&:to_s).to_set
      end
    end

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

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

        super(options, response_options_and_flash)
      end
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
actionpack-8.0.1 lib/action_controller/metal/flash.rb
actionpack-8.0.0.1 lib/action_controller/metal/flash.rb
actionpack-7.2.2.1 lib/action_controller/metal/flash.rb
actionpack-8.0.0 lib/action_controller/metal/flash.rb
actionpack-7.2.2 lib/action_controller/metal/flash.rb
actionpack-8.0.0.rc2 lib/action_controller/metal/flash.rb
actionpack-7.2.1.2 lib/action_controller/metal/flash.rb
actionpack-8.0.0.rc1 lib/action_controller/metal/flash.rb
actionpack-7.2.1.1 lib/action_controller/metal/flash.rb
actionpack-8.0.0.beta1 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha9 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha8 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha7 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha4 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha3 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha2 lib/action_controller/metal/flash.rb
omg-actionpack-8.0.0.alpha1 lib/action_controller/metal/flash.rb
actionpack-7.2.1 lib/action_controller/metal/flash.rb
actionpack-7.2.0 lib/action_controller/metal/flash.rb
actionpack-7.2.0.rc1 lib/action_controller/metal/flash.rb