diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..cb5de26 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,7 +16,13 @@ def destroy @comment = current_user.comments.find( params[:id] ) @comment.destroy - redirect_to :back + respond_to do |format| + format.html { + redirect_to :back + } + + format.js + end end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..8e5819f 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -3,20 +3,17 @@ class MessagesController < ApplicationController before_action :authenticate_user!, :except => [:index, :show] def index - # TODO: fix N+1 queries for user and comments - @messages = Message.order("id DESC").page( params[:page] ) + + @messages = Message.order("id DESC").includes(:comments).includes(:user).page( params[:page] ) if params[:status] == "pending" - # TODO: @messages = @messages.pending - @messages = @messages.where( :status => "pending" ) + @messages = @messages.pending elsif params[:status] == "completed" - # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "completed" ) + @messages = @messages.completed end - + if params[:days] - # TODO: @messages = @messages.within_days(params[:days].to_i) - @messages = @messages.where( ["created_at >= ?", Time.now - params[:days].to_i.days ] ) + @messages = @messages.within_days( Time.now - 7.days ) end end diff --git a/app/models/message.rb b/app/models/message.rb index e24a8b2..402dfec 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,6 +4,15 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + scope :pending, -> { where( :status => "pending" ) } + scope :completed, -> { where( :status => "completed" ) } + +def self.within_days(t=Time.now) + + where(["created_at > ? ", t ]) + +end + def last_comment_summary self.comments.last.try(:content).try(:truncate, 20) end diff --git a/app/models/user.rb b/app/models/user.rb index 6d01aa9..3af87ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,7 +12,7 @@ def display_name end def posts_count - # TODO: 請完成我 + self.messages.count + self.comments.count end def words_count diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..e02f77e --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1 @@ +$("#comment-<%= @comment.id %>").remove(); \ No newline at end of file diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 41401f6..320079c 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -19,13 +19,16 @@ <% @message.comments.each do |comment| %> +
<%= simple_format comment.content %> at <%= comment.created_at.to_s(:short) %> by <%= comment.user.display_name %> <% if comment.user == current_user %> - <%# TODO: 修改成 AJAX 版本的刪除 %> - <%= link_to "Delete", message_comment_path(@message, comment), :method => :delete, :data => { :confirm => "Are u sure?"} %> + <%= link_to "Delete", message_comment_path(@message, comment), :method => :delete, remote: true %> <% end %>
+