From 033a045810ff2ab71e33677c7d93897960909c03 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 20:18:50 +0800 Subject: [PATCH 01/10] ajax deleting comment --- app/controllers/comments_controller.rb | 4 +++- app/views/comments/destroy.js.erb | 1 + app/views/messages/show.html.erb | 20 ++++++++++---------- 3 files changed, 14 insertions(+), 11 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..b1577e0 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,7 +16,9 @@ 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..c51a6f3 --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1 @@ +$("#<%= @comment.id %>_comment").remove(); diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 41401f6..2825c09 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -17,15 +17,15 @@ <% end %> -<% @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 %> -

+<% @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, :remote => true %> + <% end %> +

+
<% end %> - From 70cdb923cff50321b08a74f81446a58fd8381b28 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 20:32:46 +0800 Subject: [PATCH 02/10] fix N+1 queries for user and comments --- 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..63a7909 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(:user, :comments).order("id DESC").page( params[:page] ) if params[:status] == "pending" # TODO: @messages = @messages.pending From 7e4f3edfba4990b99a4b91aad7fd5550b561f022 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 21:20:11 +0800 Subject: [PATCH 03/10] refactor messages controller with scope and class method --- app/controllers/messages_controller.rb | 17 +++-------------- app/models/message.rb | 7 +++++++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 63a7909..2ee8598 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -3,21 +3,10 @@ class MessagesController < ApplicationController before_action :authenticate_user!, :except => [:index, :show] def index - # TODO: fix N+1 queries for user and comments @messages = Message.includes(:user, :comments).order("id DESC").page( params[:page] ) - - if params[:status] == "pending" - # TODO: @messages = @messages.pending - @messages = @messages.where( :status => "pending" ) - elsif params[:status] == "completed" - # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "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 ] ) - end + @messages = @messages.pending if params[:status] == "pending" + @messages = @messages.completed if params[:status] == "completed" + @messages = @messages.within_days(params[:days].to_i) if params[:days] end def show diff --git a/app/models/message.rb b/app/models/message.rb index e24a8b2..7a62d00 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -8,4 +8,11 @@ def last_comment_summary self.comments.last.try(:content).try(:truncate, 20) end + scope :pending, -> { where( status: "pending" ) } + scope :completed, -> { where( status: "completed" ) } + + def self.within_days(time) + where( ["created_at >= ?", Time.now - time.days ] ) + end + end From 492985a8bd3d31edfbbb8ad8b3b096d45dafcf4e Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 21:37:05 +0800 Subject: [PATCH 04/10] change message class method into scope as required --- app/models/message.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/message.rb b/app/models/message.rb index 7a62d00..5b6ab02 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -10,9 +10,9 @@ def last_comment_summary scope :pending, -> { where( status: "pending" ) } scope :completed, -> { where( status: "completed" ) } - - def self.within_days(time) - where( ["created_at >= ?", Time.now - time.days ] ) - end - + scope :within_days, ->(time) { where("created_at >= ?", Time.now - time.days) } + # According to rails api,here I quote,"Using a class method is the preferred way to accept arguments for scopes." + # def self.within_days(time) + # where( ["created_at >= ?", Time.now - time.days ] ) + # end end From eb0870188fcd83d616818abb37a91c385220d91e Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 21:40:41 +0800 Subject: [PATCH 05/10] finish the User Class method posts_count --- 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 89ad00046a9761d225a4205b283a6f85502e04f5 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 21:56:05 +0800 Subject: [PATCH 06/10] finished words_count unit test --- spec/models/user_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 528cc38..66a30ad 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -24,12 +24,12 @@ describe "#words_count" do before do - # TODO: 加 Message 和 Comment 測試資料 + msg = Message.create!( :user => @user, :title => "Jacob is doing final exam", :content => "feels good") + comment = Comment.create!( :user => @user, :message => msg, :content=> "get it done" ) end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do - # TODO: 測試 words_count 方法 - + expect( @user.words_count ).to eq(10) end end From 65ac6fd86d38e94d7a844813429ea4f84c712fd9 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Fri, 11 Nov 2016 22:34:58 +0800 Subject: [PATCH 07/10] JSON API with info about all messages --- app/controllers/api_v1/messages_controller.rb | 5 ++ app/views/api_v1/messages/index.json.jbuilder | 3 + config/routes.rb | 56 +------------------ 3 files changed, 11 insertions(+), 53 deletions(-) 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..d4a3d72 --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,5 @@ +class ApiV1::MessagesController < ApplicationController + 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..c175dc3 --- /dev/null +++ b/app/views/api_v1/messages/index.json.jbuilder @@ -0,0 +1,3 @@ +json.data @messages do |msg| + json.(msg, :id, :status, :category_name, :title, :content, :created_at) +end diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..6378cf8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,59 +4,9 @@ resources :messages do resources :comments end - - # The priority is based upon order of creation: first created -> highest priority. - # See how all your routes lay out with "rake routes". - - # You can have the root of your site routed with "root" root 'messages#index' - # Example of regular route: - # get 'products/:id' => 'catalog#view' - - # Example of named route that can be invoked with purchase_url(id: product.id) - # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase - - # Example resource route (maps HTTP verbs to controller actions automatically): - # resources :products - - # Example resource route with options: - # resources :products do - # member do - # get 'short' - # post 'toggle' - # end - # - # collection do - # get 'sold' - # end - # end - - # Example resource route with sub-resources: - # resources :products do - # resources :comments, :sales - # resource :seller - # end - - # Example resource route with more complex sub-resources: - # resources :products do - # resources :comments - # resources :sales do - # get 'recent', on: :collection - # end - # end - - # Example resource route with concerns: - # concern :toggleable do - # post 'toggle' - # end - # resources :posts, concerns: :toggleable - # resources :photos, concerns: :toggleable - - # Example resource route within a namespace: - # namespace :admin do - # # Directs /admin/products/* to Admin::ProductsController - # # (app/controllers/admin/products_controller.rb) - # resources :products - # end + scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => { :format => :json } do + resources :messages, only: :index + end end From 7087528deab740a5c44f7187e33b7c430adfdfe1 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Sat, 12 Nov 2016 01:07:48 +0800 Subject: [PATCH 08/10] generated Subscription and Like model, made many to many association between User and Message --- app/models/like.rb | 4 ++++ app/models/message.rb | 6 +++++ app/models/subscription.rb | 4 ++++ app/models/user.rb | 6 +++++ .../20161111164452_create_subscriptions.rb | 12 ++++++++++ db/migrate/20161111164905_create_likes.rb | 12 ++++++++++ db/schema.rb | 22 ++++++++++++++++++- spec/models/like_spec.rb | 5 +++++ spec/models/subscription_spec.rb | 5 +++++ 9 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 app/models/like.rb create mode 100644 app/models/subscription.rb create mode 100644 db/migrate/20161111164452_create_subscriptions.rb create mode 100644 db/migrate/20161111164905_create_likes.rb create mode 100644 spec/models/like_spec.rb create mode 100644 spec/models/subscription_spec.rb diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..a8a243f --- /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 5b6ab02..0b815fd 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 :subscriptions, :dependent => :destroy + has_many :subed_by_users, -> { distinct }, :through => :subscriptions, :source => :user + + has_many :likes, :dependent => :destroy + has_many :liked_by_users, -> { distinct }, :through => :likes, :source => :user + def last_comment_summary self.comments.last.try(:content).try(:truncate, 20) end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 0000000..b431527 --- /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 3af87ba..4c6da85 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 :subscriptions, :dependent => :destroy + has_many :sub_messages, -> { distinct }, :through => :subscriptions, :source => :message + + has_many :likes, :dependent => :destroy + has_many :like_messages, -> { distinct }, :through => :likes, :source => :message + def display_name self.email.split("@").first end diff --git a/db/migrate/20161111164452_create_subscriptions.rb b/db/migrate/20161111164452_create_subscriptions.rb new file mode 100644 index 0000000..b02313a --- /dev/null +++ b/db/migrate/20161111164452_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/migrate/20161111164905_create_likes.rb b/db/migrate/20161111164905_create_likes.rb new file mode 100644 index 0000000..386916e --- /dev/null +++ b/db/migrate/20161111164905_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/schema.rb b/db/schema.rb index ef7b5c0..d63fac8 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: 20161111164905) 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 diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb new file mode 100644 index 0000000..76ea93d --- /dev/null +++ b/spec/models/like_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Like, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb new file mode 100644 index 0000000..d40bf0b --- /dev/null +++ b/spec/models/subscription_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Subscription, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end From c77ecd02d864f784f8f62cb52bd8a400c3e209fa Mon Sep 17 00:00:00 2001 From: jacobshan Date: Sat, 12 Nov 2016 01:29:19 +0800 Subject: [PATCH 09/10] simultaneously liked and subed by 2 random users when fake messages being generated --- lib/tasks/dev.rake | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 4673e72..0015be2 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -16,7 +16,9 @@ namespace :dev do :status => ["pending", "completed"].sample, :content => Faker::Lorem.paragraph, :user => users.sample, - :created_at => Time.now - rand(30).days ) + :created_at => Time.now - rand(30).days, + :subed_by_users => users.sample(2), + :liked_by_users => users.sample(2) ) 5.times do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) @@ -24,4 +26,4 @@ namespace :dev do end end -end \ No newline at end of file +end From e9c8a3d0cc09fe63c6ddd712d8971f10c852b7f1 Mon Sep 17 00:00:00 2001 From: jacobshan Date: Sat, 12 Nov 2016 02:12:48 +0800 Subject: [PATCH 10/10] show users who subed and liked on the msg show page --- app/controllers/messages_controller.rb | 2 ++ app/views/messages/show.html.erb | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 2ee8598..457e080 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -12,6 +12,8 @@ def index def show @message = Message.find( params[:id] ) @comment = Comment.new + @users_sub = @message.subed_by_users + @users_liked = @message.liked_by_users end def new diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 2825c09..1e46360 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,6 +1,15 @@

<%= @message.title %>

<%= simple_format @message.content %> +Subscribed by +<% @users_sub.each do |user| %> +

<%= user.display_name %>

+<% end %> + +Liked by +<% @users_liked.each do |user| %> +

<%= user.display_name %>

+<% end %>

Comments list