From 12b235ba6fcb930249b1a64ab7d2d34998d56e06 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:33:51 +0800 Subject: [PATCH 01/10] exam init --- .rbenv-gemsets | 1 + lib/tasks/dev.rake | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 .rbenv-gemsets diff --git a/.rbenv-gemsets b/.rbenv-gemsets new file mode 100644 index 0000000..2fd6c23 --- /dev/null +++ b/.rbenv-gemsets @@ -0,0 +1 @@ +rails_pratice diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 4673e72..203591f 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -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 => "test@gmail.com", + :password => "12345678") 10.times do users << User.create!( :email => Faker::Internet.email, :password => "12345678") @@ -17,6 +21,7 @@ namespace :dev do :content => Faker::Lorem.paragraph, :user => users.sample, :created_at => Time.now - rand(30).days ) + puts m.title 5.times do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) From 325f451709189c0e5e42c3e3916fdedd8b8ae79a Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:34:53 +0800 Subject: [PATCH 02/10] use ajax destroy comment --- app/controllers/comments_controller.rb | 5 +++-- app/views/comments/destroy.js.erb | 1 + app/views/messages/show.html.erb | 16 +++++++++------- 3 files changed, 13 insertions(+), 9 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..66ffda0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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 diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..be9884e --- /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..39f29d5 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -19,13 +19,15 @@ <% @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?"} %> - <% end %> -

+
+

<%= 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 %> +

+
<% end %> From 2309e976ff46580c9bd9a37cf3bf79b71f69e904 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:35:53 +0800 Subject: [PATCH 03/10] solve messages N+1 query --- 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..4d3d6ee 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.includes(:comments, :user).order("id DESC").page( params[:page] ) if params[:status] == "pending" # TODO: @messages = @messages.pending From ebd1e08e3e20f21f0701e056c04b5c87665c5ae9 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:36:34 +0800 Subject: [PATCH 04/10] message index refactor to scope --- app/controllers/messages_controller.rb | 6 +++--- app/models/message.rb | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 4d3d6ee..baa8969 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -8,15 +8,15 @@ def index 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 diff --git a/app/models/message.rb b/app/models/message.rb index e24a8b2..5fe4151 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,6 +4,10 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + 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 From bfc614edda67b8c51c0ffc27e8849c25d4d45ce2 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:37:03 +0800 Subject: [PATCH 05/10] user add posts_count method --- app/models/user.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/user.rb b/app/models/user.rb index 6d01aa9..8f74cad 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,7 @@ def display_name def posts_count # TODO: 請完成我 + self.messages.size + self.comments.size end def words_count From f6e9e51ecab2e79fa251270f5bef209674e50b26 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:37:33 +0800 Subject: [PATCH 06/10] unit test user.words_count --- spec/models/user_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 528cc38..32925a9 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 From c97d4f416cdd1696a2a353caa3f69734e51c6899 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Fri, 11 Nov 2016 23:38:04 +0800 Subject: [PATCH 07/10] api v1 messages list --- app/controllers/api_v1/messages_controller.rb | 7 +++++++ app/views/api_v1/messages/index.json.jbuilder | 5 +++++ config/routes.rb | 3 +++ lib/tasks/dev.rake | 1 + 4 files changed, 16 insertions(+) create mode 100644 app/controllers/api_v1/messages_controller.rb create mode 100644 app/views/api_v1/messages/index.json.jbuilder diff --git a/app/controllers/api_v1/messages_controller.rb b/app/controllers/api_v1/messages_controller.rb new file mode 100644 index 0000000..1776b4e --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,7 @@ +class ApiV1::MessagesController < ApplicationController + skip_before_action :verify_authenticity_token + + def index + @messages = Message.all + end +end diff --git a/app/views/api_v1/messages/index.json.jbuilder b/app/views/api_v1/messages/index.json.jbuilder new file mode 100644 index 0000000..989d46e --- /dev/null +++ b/app/views/api_v1/messages/index.json.jbuilder @@ -0,0 +1,5 @@ +json.data do + json.messages @messages do |message| + json.(message, :id, :status, :category_name, :title, :content, :created_at) + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..6a88efc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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". diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 203591f..658a1f0 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -18,6 +18,7 @@ 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 ) From faf960f891cdee9f52628f855878376f85fe7377 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Sat, 12 Nov 2016 01:23:32 +0800 Subject: [PATCH 08/10] (7-1) new like and subscription model, set user and message has_many to has_many --- app/models/like.rb | 4 ++++ app/models/message.rb | 6 +++++ app/models/subscription.rb | 4 ++++ app/models/user.rb | 6 +++++ db/migrate/20161111165335_create_likes.rb | 12 ++++++++++ .../20161111165512_create_subscriptions.rb | 12 ++++++++++ db/schema.rb | 22 ++++++++++++++++++- 7 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 app/models/like.rb create mode 100644 app/models/subscription.rb create mode 100644 db/migrate/20161111165335_create_likes.rb create mode 100644 db/migrate/20161111165512_create_subscriptions.rb diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..88c087f --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,4 @@ +class Like < ActiveRecord::Base + belongs_to :user + belongs_to :message +end diff --git a/app/models/message.rb b/app/models/message.rb index 5fe4151..d45b98b 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,6 +4,12 @@ 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 ] ) } diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 0000000..24876ed --- /dev/null +++ b/app/models/subscription.rb @@ -0,0 +1,4 @@ +class Subscription < ActiveRecord::Base + belongs_to :user + belongs_to :message +end diff --git a/app/models/user.rb b/app/models/user.rb index 8f74cad..dd5a568 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,12 @@ 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 diff --git a/db/migrate/20161111165335_create_likes.rb b/db/migrate/20161111165335_create_likes.rb new file mode 100644 index 0000000..386916e --- /dev/null +++ b/db/migrate/20161111165335_create_likes.rb @@ -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 diff --git a/db/migrate/20161111165512_create_subscriptions.rb b/db/migrate/20161111165512_create_subscriptions.rb new file mode 100644 index 0000000..b02313a --- /dev/null +++ b/db/migrate/20161111165512_create_subscriptions.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index ef7b5c0..682e4fe 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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" @@ -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 From 482255974e05a6ae33d2e5c85160f194df70116a Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Sat, 12 Nov 2016 01:25:55 +0800 Subject: [PATCH 09/10] (7-2) dev:fake can random two user like message and subscription message --- lib/tasks/dev.rake | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 658a1f0..d0e96d0 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -22,6 +22,10 @@ namespace :dev do :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, From 118fddb591b6748618995a7be53502dfdff91314 Mon Sep 17 00:00:00 2001 From: HEROGWP Date: Sat, 12 Nov 2016 01:31:23 +0800 Subject: [PATCH 10/10] (7-3) message can show who like and subscription --- app/views/messages/show.html.erb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 39f29d5..4efafc9 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,3 +1,14 @@ +like_users: +<% @message.like_users.each do |user| %> +
  • <%= user.email %>
  • +<% end %> + +subscript_users: +<% @message.subscript_users.each do |user| %> +
  • <%= user.email %>
  • +<% end %> + +

    <%= @message.title %>

    <%= simple_format @message.content %>