summaryrefslogtreecommitdiff
path: root/app/controllers/controller_extension/flash.rb
blob: 6a62351d978a197dcf374385f910c44313099a36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
module ControllerExtension::Flash
  extend ActiveSupport::Concern

  protected

  def flash_for(resource, options = {})
    return unless resource.changed?
    message = flash_message_for(resource)
    type = flash_type(resource)
    if message.present?
      flash[type] = [message, flash[type]].join(' ')
    end
  end

  def flash_message_for(resource)
    I18n.t flash_i18n_key(resource),
      scope: :flash,
      cascade: true,
      resource: resource.class.model_name.human
  end

  def flash_i18n_key(resource)
    namespace = [action_name]
    namespace += controller_path.split('/')
    namespace << flash_type(resource)
    namespace.join(".")
  end

  def flash_type(resource)
    resource.valid? ? :success : :error
  end

end