Module: MiniApi::Translation::Message

Included in:
ModelResponder
Defined in:
lib/mini_api/translation/message.rb

Overview

Module to handle the message key on json response. Will identify if model has errors or not and define the I18n path to notice, for model without errors and alert for model with errors. There is three possible path for messages:

The path based on model name mini_api:

messages:
  model_name:
    action_name:
      notitce:
      alert:

The path based on controller name mini_api:

messages:
  controller:
    controller_name:
      action_name:
        notitce:
        alert:

And the last, is per action path: mini_api:

messages:
  actions:
    create:
      notice: '%{resource_name} foi criado com sucesso.'
      alert: '%{resource_name} não pôde ser criado.'

Instance Method Summary collapse

Instance Method Details

#i18n_messageObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/mini_api/translation/message.rb', line 36

def i18n_message
  kind = @resource.errors.empty? ? 'notice' : 'alert'

  I18n.t(
    kind,
    scope: model_message_path || controller_message_path || default_message_path,
    resource_name: @resource.class.model_name.human,
    default: ''
  )
end