diff options
author | azul <azul@leap.se> | 2014-07-18 12:21:49 +0200 |
---|---|---|
committer | azul <azul@leap.se> | 2014-07-18 12:21:49 +0200 |
commit | bbd41c9bfd2cb88a88d7436dd58a8b46a5d10cf1 (patch) | |
tree | 00b39da219151473437c02ce35344117bcd5b2fa /app | |
parent | ade74d8a9091ae607586d7b287a0579a2ee7af8e (diff) | |
parent | 20352249fa5dafe3abb2d4b751b1e5c8c0a59abc (diff) |
Merge pull request #180 from azul/feature/messages-api
Feature/messages api
Diffstat (limited to 'app')
-rw-r--r-- | app/controllers/controller_extension/json_responses.rb | 29 | ||||
-rw-r--r-- | app/controllers/v1/messages_controller.rb | 4 | ||||
-rw-r--r-- | app/models/message.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 7 |
4 files changed, 37 insertions, 7 deletions
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 diff --git a/app/models/message.rb b/app/models/message.rb index 424f094..2478f3f 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -26,4 +26,8 @@ class Message < CouchRest::Model::Base def unread_by?(user) user_ids_to_show.include?(user.id) end + + def as_json(*args, &block) + {"id" => id, "text" => text}.as_json(*args, &block) + end end diff --git a/app/models/user.rb b/app/models/user.rb index 6bc5841..9ac7d3d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -92,12 +92,9 @@ class User < CouchRest::Model::Base Ticket.for_user(self).limit(count).all #defaults to having most recent updated first end - def messages(unseen = true) + def messages #TODO for now this only shows unseen messages. Will we ever want seen ones? Is it necessary to store? - - # we don't want to emit all the userids associated with a message, so only emit id and text. - Message.by_user_ids_to_show.key(self.id).map { |message| [message.id, message.text] } - + Message.by_user_ids_to_show.key(self.id) end # DEPRECATED |