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

Peter's Final Exam #57

Open
wants to merge 10 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
1 change: 1 addition & 0 deletions .rbenv-gemsets
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rails_pratice
7 changes: 7 additions & 0 deletions app/controllers/api_v1/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApiV1::MessagesController < ApplicationController
skip_before_action :verify_authenticity_token

def index
@messages = Message.all
end
end
5 changes: 3 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ def create
def destroy
@comment = current_user.comments.find( params[:id] )
@comment.destroy

redirect_to :back
respond_to do |format|
format.js
end
end

protected
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ class MessagesController < ApplicationController

def index
# TODO: fix N+1 queries for user and comments
@messages = Message.order("id DESC").page( params[:page] )
@messages = Message.includes(:comments, :user).order("id DESC").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(params[:days].to_i)
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
10 changes: 10 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ class Message < ActiveRecord::Base

has_many :comments, :dependent => :destroy

has_many :likes
has_many :like_users, :through => :likes, :source => :user

has_many :subscriptions
has_many :subscript_users, :through => :subscriptions, :source => :user

scope :pending, -> { where( :status => "pending" ) }
scope :completed, -> { where( :status => "completed" ) }
scope :within_days, ->(date) { where( ["created_at >= ?", Time.now - date.days ] ) }

def last_comment_summary
self.comments.last.try(:content).try(:truncate, 20)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ class User < ActiveRecord::Base
has_many :messages
has_many :comments

has_many :likes
has_many :like_messages, :through => :likes, :source => :messages

has_many :subscriptions
has_many :subscript_messages, :through => :subscription, :source => :messages

def display_name
self.email.split("@").first
end

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

def words_count
Expand Down
5 changes: 5 additions & 0 deletions app/views/api_v1/messages/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.data do
json.messages @messages do |message|
json.(message, :id, :status, :category_name, :title, :content, :created_at)
end
end
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();
27 changes: 20 additions & 7 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
like_users:
<% @message.like_users.each do |user| %>
<li><%= user.email %></li>
<% end %>

subscript_users:
<% @message.subscript_users.each do |user| %>
<li><%= user.email %></li>
<% end %>


<h2><%= @message.title %></h2>

<%= simple_format @message.content %>
Expand All @@ -19,13 +30,15 @@

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

<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?"} %>
<% end %>
</p>
<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), :remote => true, :method => :delete, :data => { :confirm => "Are u sure?"} %>
<% end %>
</p>
</div>

<% end %>

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
resources :comments
end

scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => { :format => :json } do
resources :messages, only: [ :index ]
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20161111165335_create_likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.integer :user_id
t.integer :message_id

t.timestamps null: false
end
add_index :likes, :user_id
add_index :likes, :message_id
end
end
12 changes: 12 additions & 0 deletions db/migrate/20161111165512_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateSubscriptions < ActiveRecord::Migration
def change
create_table :subscriptions do |t|
t.integer :user_id
t.integer :message_id

t.timestamps null: false
end
add_index :subscriptions, :user_id
add_index :subscriptions, :message_id
end
end
22 changes: 21 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150728165437) do
ActiveRecord::Schema.define(version: 20161111165512) do

create_table "comments", force: :cascade do |t|
t.text "content"
Expand All @@ -23,6 +23,16 @@

add_index "comments", ["message_id"], name: "index_comments_on_message_id"

create_table "likes", force: :cascade do |t|
t.integer "user_id"
t.integer "message_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "likes", ["message_id"], name: "index_likes_on_message_id"
add_index "likes", ["user_id"], name: "index_likes_on_user_id"

create_table "messages", force: :cascade do |t|
t.string "title"
t.text "content"
Expand All @@ -36,6 +46,16 @@
add_index "messages", ["status"], name: "index_messages_on_status"
add_index "messages", ["user_id"], name: "index_messages_on_user_id"

create_table "subscriptions", force: :cascade do |t|
t.integer "user_id"
t.integer "message_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "subscriptions", ["message_id"], name: "index_subscriptions_on_message_id"
add_index "subscriptions", ["user_id"], name: "index_subscriptions_on_user_id"

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down
10 changes: 10 additions & 0 deletions lib/tasks/dev.rake
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace :dev do

task :build => ["tmp:clear", "log:clear", "db:drop", "db:create", "db:migrate"]
task :rebuild => ["dev:build" , :fake]
task :fake => :environment do
User.delete_all
Message.delete_all
Comment.delete_all

users = []
users << User.create!( :email => "[email protected]",
:password => "12345678")
10.times do
users << User.create!( :email => Faker::Internet.email,
:password => "12345678")
Expand All @@ -14,9 +18,15 @@ namespace :dev do
20.times do
m = Message.create!( :title => Faker::Lorem.word,
:status => ["pending", "completed"].sample,
:category_name => ["快報", "求物資", "求救"].sample,
:content => Faker::Lorem.paragraph,
:user => users.sample,
:created_at => Time.now - rand(30).days )
2.times do
m.likes.create!( :user => users.sample )
m.subscriptions.create!( :user => users.sample )
end
puts m.title
5.times do
m.comments.create!( :content => Faker::Lorem.paragraph,
:user => users.sample )
Expand Down
4 changes: 3 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
describe "#words_count" do
before do
# TODO: 加 Message 和 Comment 測試資料
@message = @user.messages.create!(title: "12345", content: "67890")
@message.comments.create!(content: "12345", user: @user)
end

it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do
# TODO: 測試 words_count 方法

expect( @user.words_count ).to eq(3)
end
end

Expand Down