summaryrefslogtreecommitdiff
path: root/app/controllers/controller_extension/json_responses.rb
blob: da1ae58b58bd802853844060cd1c477069f1062c (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
module ControllerExtension::JsonResponses
  extend ActiveSupport::Concern

  private

  def success(key)
    json_message :success, key
  end

  def error(key)
    json_message :error, key
  end

  def json_message(type, key)
    long_key = "#{controller_string}.#{action_string}.#{key}"
    { type => key.to_s,
      :message => I18n.t(long_key, cascade: true) }
  end

  def controller_string
    self.class.name.underscore.
      sub(/_controller$/, '').
      sub(/^v\d\//, '')
  end

  def action_string
    params[:action]
  end
end