diff --git a/app/controllers/api/messages_controller.rb b/app/controllers/api/messages_controller.rb new file mode 100644 index 0000000..693687a --- /dev/null +++ b/app/controllers/api/messages_controller.rb @@ -0,0 +1,18 @@ +class Api::MessagesController < ApplicationController + skip_before_action :verify_authenticity_token + before_action :enable_cors + def index + @messages = Message.all + end + + + private + def enable_cors + response.headers['Access-Control-Allow-Origin'] = '*' + response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE' + response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, auth_token, *' + response.headers['Access-Control-Request-Method'] = 'GET, POST, PUT, DELETE' + response.headers['Access-Control-Request-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, *' + end +end + diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..fb61acb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -14,9 +14,11 @@ def create def destroy @comment = current_user.comments.find( params[:id] ) + respond_to do |format| + format.js + end @comment.destroy - - redirect_to :back + #redirect_to :back end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..30f5bb5 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,19 +4,22 @@ 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 - @messages = @messages.where( :status => "pending" ) + #@messages = @messages.where( :status => "pending" ) + @messages = @messages.messages_pending elsif params[:status] == "completed" # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "completed" ) + #@messages = @messages.where( :status => "completed" ) + @messages = @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.where( ["created_at >= ?", Time.now - params[:days].to_i.days ] ) + @messages = @messages.messages_within_days end end @@ -62,7 +65,7 @@ def destroy protected def message_params - params.require(:message).permit(:title, :content, :category_name) + params.require(:message).permit(:title, :content, :category_name, :status) end end diff --git a/app/helpers/api/v1/messages_helper.rb b/app/helpers/api/v1/messages_helper.rb new file mode 100644 index 0000000..c164d3c --- /dev/null +++ b/app/helpers/api/v1/messages_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::MessagesHelper +end 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..b88ebd9 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -3,7 +3,10 @@ class Message < ActiveRecord::Base belongs_to :user has_many :comments, :dependent => :destroy - + has_many :likes + has_many :liked_users, :through => :likes, :source => :user + has_many :subscriptions + has_many :subscribed_users, :through => :subscriptions, :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 6d01aa9..bac0582 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,13 +6,17 @@ class User < ActiveRecord::Base has_many :messages has_many :comments - + has_many :likes + has_many :liked_messages, :through => :likes, :source => :message + has_many :subscriptions + has_many :subscribed_messages, :through => :subscriptions, :source => :message def display_name self.email.split("@").first end def posts_count # TODO: 請完成我 + self.comments.count + self.messages.count end def words_count diff --git a/app/views/api/messages/index.json.jbuilder b/app/views/api/messages/index.json.jbuilder new file mode 100644 index 0000000..9558b84 --- /dev/null +++ b/app/views/api/messages/index.json.jbuilder @@ -0,0 +1,4 @@ +json.data(@messages) do |m| + json.( m, :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..019963a --- /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..8e69fe0 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,4 +1,11 @@

<%= @message.title %>

+subscribed_users:<% @message.subscribed_users.each do |u| %> + <%= u.email %> +<% end %> +
+liked_users:<% @message.liked_users.each do |u| %> + <%= u.email %> +<% end %> <%= simple_format @message.content %> @@ -19,13 +26,13 @@ <% @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), :method => :delete, :remote => true, :data => { :confirm => "Are u sure?"} %> + <% end %> +
<% end %> diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..0cb9521 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,10 @@ Rails.application.routes.draw do devise_for :users - + namespace :api, :defaults => { :format => :json } do + resource :v1 do + resources :messages + end + end resources :messages do resources :comments end diff --git a/db/migrate/20161112141754_create_subscriptions.rb b/db/migrate/20161112141754_create_subscriptions.rb new file mode 100644 index 0000000..63ce663 --- /dev/null +++ b/db/migrate/20161112141754_create_subscriptions.rb @@ -0,0 +1,9 @@ +class CreateSubscriptions < ActiveRecord::Migration + def change + create_table :subscriptions do |t| + t.integer :user_id + t.integer :message_id + t.timestamps null: false + end + end +end diff --git a/db/migrate/20161112141801_create_likes.rb b/db/migrate/20161112141801_create_likes.rb new file mode 100644 index 0000000..83f844d --- /dev/null +++ b/db/migrate/20161112141801_create_likes.rb @@ -0,0 +1,9 @@ +class CreateLikes < ActiveRecord::Migration + def change + create_table :likes do |t| + t.integer :user_id + t.integer :message_id + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ef7b5c0..d43e4b3 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: 20161112141801) do create_table "comments", force: :cascade do |t| t.text "content" @@ -23,6 +23,13 @@ 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 + create_table "messages", force: :cascade do |t| t.string "title" t.text "content" @@ -36,6 +43,13 @@ 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 + 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..05324b8 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -10,18 +10,34 @@ namespace :dev do users << User.create!( :email => Faker::Internet.email, :password => "12345678") end - + messages = [] 20.times do m = Message.create!( :title => Faker::Lorem.word, :status => ["pending", "completed"].sample, :content => Faker::Lorem.paragraph, :user => users.sample, :created_at => Time.now - rand(30).days ) + messages << m 5.times do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) end end + + rand_messages = messages.sample(2) + rand_users = users.sample(2) + Like.create!(:user_id => rand_users.first.id.to_i, + :message_id => rand_messages.first.id.to_i) + Like.create!(:user_id => rand_users.second.id.to_i, + :message_id => rand_messages.second.id.to_i) + + rand_messages2 = messages.sample(2) + rand_users2 = users.sample(2) + Subscription.create!(:user_id => rand_users2.first.id.to_i, + :message_id => rand_messages2.first.id.to_i) + Subscription.create!(:user_id => rand_users2.second.id.to_i, + :message_id => rand_messages2.second.id.to_i) + end end \ No newline at end of file diff --git a/spec/controllers/api/v1/messages_controller_spec.rb b/spec/controllers/api/v1/messages_controller_spec.rb new file mode 100644 index 0000000..2e829c7 --- /dev/null +++ b/spec/controllers/api/v1/messages_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Api::V1::MessagesController, type: :controller do + +end diff --git a/spec/helpers/api/v1/messages_helper_spec.rb b/spec/helpers/api/v1/messages_helper_spec.rb new file mode 100644 index 0000000..461b495 --- /dev/null +++ b/spec/helpers/api/v1/messages_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Api::V1::MessagesHelper. For example: +# +# describe Api::V1::MessagesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Api::V1::MessagesHelper, type: :helper do + pending "add some examples to (or delete) #{__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..b44d390 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 測試資料 + m = Message.create!( :user => @user, :title => "1", :content => "2 2" ) + c = Comment.create!( :user => @user, :content => "3 3 3") end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do # TODO: 測試 words_count 方法 - + expect( @user.words_count ).to eq(6) end end