From b17387a17669bfc9afce7435653cd8c29c686999 Mon Sep 17 00:00:00 2001 From: Azul Date: Sat, 12 Jul 2014 09:12:48 +0200 Subject: some cleanup of the messages api and cuke feature --- .../controller_extension/json_responses.rb | 29 ++++++++++++++++++++++ app/controllers/v1/messages_controller.rb | 4 +-- 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 app/controllers/controller_extension/json_responses.rb (limited to 'app/controllers') diff --git a/app/controllers/controller_extension/json_responses.rb b/app/controllers/controller_extension/json_responses.rb new file mode 100644 index 0000000..da1ae58 --- /dev/null +++ b/app/controllers/controller_extension/json_responses.rb @@ -0,0 +1,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 diff --git a/app/controllers/v1/messages_controller.rb b/app/controllers/v1/messages_controller.rb index a9b93a9..a496378 100644 --- a/app/controllers/v1/messages_controller.rb +++ b/app/controllers/v1/messages_controller.rb @@ -11,9 +11,9 @@ module V1 if message = Message.find(params[:id]) message.mark_as_read_by(current_user) message.save - render json: true + render json: success(:marked_as_read) else - render json: false + render json: error(:not_found), status: :not_found end end -- cgit v1.2.3