From a50b4953e3f94d4b570a6816694bf5d79738f2d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E7=92=BD=E5=87=B1?= Date: Fri, 22 Jul 2016 17:45:59 +0800 Subject: [PATCH 1/5] =?UTF-8?q?1.=20(=E6=94=B9=20Ajax)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/comments_controller.rb | 8 +++++++- app/views/comments/destroy.js.erb | 1 + app/views/messages/show.html.erb | 7 +++++-- 3 files changed, 13 insertions(+), 3 deletions(-) create mode 100644 app/views/comments/destroy.js.erb 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/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 %>

+
+ <% end %> From 19ed8530e301b350ca5a4d306c8ae67d93850611 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E7=92=BD=E5=87=B1?= Date: Fri, 22 Jul 2016 18:23:01 +0800 Subject: [PATCH 2/5] =?UTF-8?q?2.=20(=E4=BF=AE=20N+1=20queries)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/messages_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..cef5e6f 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,7 +4,7 @@ class MessagesController < ApplicationController 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 From ca5ba9ba30183d42c2dfe87406aa5659a39d325f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E7=92=BD=E5=87=B1?= Date: Fri, 22 Jul 2016 18:43:31 +0800 Subject: [PATCH 3/5] =?UTF-8?q?3.=20(=E6=94=B9=20ActiveRecord=20Scope)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/messages_controller.rb | 13 +++++-------- app/models/message.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index cef5e6f..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").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 From d78a4f513c7f22e3fa3060ed6aa240845a9d5380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E7=92=BD=E5=87=B1?= Date: Fri, 22 Jul 2016 21:36:27 +0800 Subject: [PATCH 4/5] =?UTF-8?q?4.=20(=E5=96=AE=E5=85=83=E6=B8=AC=E8=A9=A6?= =?UTF-8?q?=E5=B7=B2=E7=B6=93=E5=AF=AB=E5=A5=BD=EF=BC=8C=E5=BE=85=E5=AF=A6?= =?UTF-8?q?=E4=BD=9C)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/user.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From bb9c4f44371b5579bb87e6275cbf511857668c4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=AA=E7=92=BD=E5=87=B1?= Date: Tue, 26 Jul 2016 14:11:42 +0800 Subject: [PATCH 5/5] =?UTF-8?q?5.=20(=E5=AF=A6=E4=BD=9C=E5=B7=B2=E7=B6=93?= =?UTF-8?q?=E5=AF=AB=E5=A5=BD=EF=BC=8C=E5=8A=A0=E5=96=AE=E5=85=83=E6=B8=AC?= =?UTF-8?q?=E8=A9=A6)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- spec/models/user_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 528cc38..d107790 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -24,13 +24,17 @@ describe "#words_count" do before do - # TODO: 加 Message 和 Comment 測試資料 + m2 = Message.create!( :user => @user , :content => "test abcdef ghijklmn" ) + Comment.create!( :user => @user, :message => m2, :content => "test opqr st" ) + Comment.create!( :user => @user, :message => m2, :content => "test uv wxyza " ) end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do - # TODO: 測試 words_count 方法 + expect( @user.words_count ).to eq(9) end end end + +