Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jerry#final_exam #46

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def display_name
end

def posts_count
# TODO: 請完成我
self.messages.count + self.comments.count
end

def words_count
Expand Down
1 change: 1 addition & 0 deletions app/views/comments/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#comment-<%= @comment.id %>").remove();
7 changes: 5 additions & 2 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@

<% @message.comments.each do |comment| %>

<div id="comment-<%= comment.id %>">

<p><%= 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 %>
</p>

</div>

<% end %>

8 changes: 6 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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