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/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/controllers/messages_controller.rb b/app/controllers/messages_controller.rb
index 96f65e5..457e080 100644
--- a/app/controllers/messages_controller.rb
+++ b/app/controllers/messages_controller.rb
@@ -3,26 +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] )
-
- 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 = Message.includes(:user, :comments).order("id DESC").page( params[:page] )
+ @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
@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/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 e24a8b2..0b815fd 100644
--- a/app/models/message.rb
+++ b/app/models/message.rb
@@ -4,8 +4,21 @@ 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
+ scope :pending, -> { where( status: "pending" ) }
+ scope :completed, -> { where( status: "completed" ) }
+ 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
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 6d01aa9..4c6da85 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -7,12 +7,18 @@ 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
def posts_count
- # TODO: 請完成我
+ self.messages.count + self.comments.count
end
def words_count
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/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..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
@@ -17,15 +26,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| %>
+
<% 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
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/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
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
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