module FlashSpeaker

Public Instance Methods

attach_message(success_flag, entity_errors = nil, t_params = {}) click to toggle source
# File lib/flash_speaker.rb, line 2
def attach_message(success_flag, entity_errors = nil, t_params = {})
  flash_type = success_flag ? :success : :failed
  if entity_errors.present?
    full_messages = t_params.delete(:full_messages)
    flash[:errors] = serialize_errors(entity_errors, full_messages)
  end
  flash[flash_type] = message(flash_type, t_params)
end

Private Instance Methods

default_message_path(flash_type) click to toggle source
# File lib/flash_speaker.rb, line 38
def default_message_path(flash_type)
  "default.actions.#{action_name}.#{flash_type}"
end
entity_name() click to toggle source
# File lib/flash_speaker.rb, line 42
def entity_name
  t "#{controller_name}.entity"
end
message(flash_type, t_params = {}) click to toggle source
# File lib/flash_speaker.rb, line 22
def message(flash_type, t_params = {})
  case
  when message_exists?(message_path(flash_type))
    t message_path(flash_type), t_params
  when message_exists?(default_message_path(flash_type))
    msg = t default_message_path(flash_type), t_params
    msg.sub('<entity>', entity_name)
  else
    raise "translation missing: #{message_path(flash_type)}"
  end
end
message_exists?(path) click to toggle source
# File lib/flash_speaker.rb, line 46
def message_exists?(path)
  I18n.t path, raise: true
rescue
  false
end
message_path(flash_type) click to toggle source
# File lib/flash_speaker.rb, line 34
def message_path(flash_type)
  "#{controller_name}.actions.#{action_name}.#{flash_type}"
end
serialize_errors(entity_errors, full_messages = false) click to toggle source
# File lib/flash_speaker.rb, line 13
def serialize_errors(entity_errors, full_messages = false)
  return entity_errors unless entity_errors.kind_of? ActiveModel::Errors
  if full_messages
    entity_errors.full_messages
  else
    entity_errors.values.flatten
  end
end