module Responders::FlashResponder
def controller_interpolation_options
def controller_interpolation_options controller.send(:flash_interpolation_options) if controller.respond_to?(:flash_interpolation_options, true) end
def flash_defaults_by_namespace(status) # :nodoc:
def flash_defaults_by_namespace(status) # :nodoc: defaults = [] slices = controller.controller_path.split("/") lookup = Responders::FlashResponder.namespace_lookup begin controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join(".")}.#{controller.action_name}.#{status}" actions_scope = lookup ? slices.fill("actions", -1).join(".") : :actions actions_scope = :"flash.#{actions_scope}.#{controller.action_name}.#{status}" defaults << :"#{controller_scope}_html" defaults << controller_scope defaults << :"#{actions_scope}_html" defaults << actions_scope slices.shift end while slices.size > 0 && lookup defaults << "" end
def initialize(controller, resources, options = {})
def initialize(controller, resources, options = {}) super @flash = options.delete(:flash) @notice = options.delete(:notice) @alert = options.delete(:alert) @flash_now = options.delete(:flash_now) { :on_failure } end
def mount_i18n_options(status) # :nodoc:
def mount_i18n_options(status) # :nodoc: options = { default: flash_defaults_by_namespace(status), resource_name: resource_name, downcase_resource_name: resource_name.downcase } controller_options = controller_interpolation_options if controller_options options.merge!(controller_options) end options end
def resource_name
def resource_name if resource.class.respond_to?(:model_name) resource.class.model_name.human else resource.class.name.underscore.humanize end end
def set_flash(key, value)
def set_flash(key, value) return if value.blank? flash = controller.flash flash = flash.now if set_flash_now? flash[key] ||= value end
def set_flash_message!
def set_flash_message! if has_errors? status = Responders::FlashResponder.flash_keys.last set_flash(status, @alert) else status = Responders::FlashResponder.flash_keys.first set_flash(status, @notice) end return if controller.flash[status].present? options = mount_i18n_options(status) message = controller.helpers.t options[:default].shift, **options set_flash(status, message) end
def set_flash_message? # :nodoc:
def set_flash_message? # :nodoc: !get? && @flash != false end
def set_flash_now?
def set_flash_now? @flash_now == true || format == :js || (default_action && (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success)) end
def to_html
def to_html set_flash_message! if set_flash_message? super end
def to_js
def to_js set_flash_message! if set_flash_message? defined?(super) ? super : to_format end