Skip to content

Commit

Permalink
add /messages.json and /messages/sent.json interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jcs committed Oct 10, 2017
1 parent 6fa2cfe commit f1ad892
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
42 changes: 29 additions & 13 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,45 @@ class MessagesController < ApplicationController
before_action :find_message, :only => [ :show, :destroy, :keep_as_new ]

def index
@cur_url = "/messages"
@title = "Messages"
@messages = @user.undeleted_received_messages

@new_message = Message.new
respond_to do |format|
format.html {
@cur_url = "/messages"
@title = "Messages"

@direction = :in
@messages = @user.undeleted_received_messages
@new_message = Message.new

@direction = :in

if params[:to]
@new_message.recipient_username = params[:to]
if params[:to]
@new_message.recipient_username = params[:to]
end
}
format.json {
render :json => @messages
}
end
end

def sent
@cur_url = "/messages"
@title = "Messages Sent"

@direction = :out
@messages = @user.undeleted_sent_messages

@new_message = Message.new
respond_to do |format|
format.html {
@cur_url = "/messages"
@title = "Messages Sent"

render :action => "index"
@direction = :out

@new_message = Message.new

render :action => "index"
}
format.json {
render :json => @messages
}
end
end

def create
Expand Down
19 changes: 19 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@ class Message < ActiveRecord::Base
after_save :update_unread_counts
after_save :check_for_both_deleted

def as_json(options = {})
attrs = [
:short_id,
:created_at,
:has_been_read,
:subject,
:body,
:deleted_by_author,
:deleted_by_recipient,
]

h = super(:only => attrs)

h[:author_username] = self.author.try(:username)
h[:recipient_username] = self.recipient.try(:username)

h
end

def assign_short_id
self.short_id = ShortId.new(self.class).generate
end
Expand Down
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@
get "/comments/page/:page" => "comments#index"
get "/comments" => "comments#index", :format => /html|rss/

get "/messages/sent" => "messages#sent"
get "/messages/sent" => "messages#sent", :format => /html|json/
get "/messages" => "messages#index", :format => /html|json/
post "/messages/batch_delete" => "messages#batch_delete",
:as => "batch_delete_messages"
resources :messages do
Expand Down

0 comments on commit f1ad892

Please sign in to comment.