diff --git a/app/assets/javascripts/api_v1/messages.coffee b/app/assets/javascripts/api_v1/messages.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api_v1/messages.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/api_v1/messages.scss b/app/assets/stylesheets/api_v1/messages.scss new file mode 100644 index 0000000..db6f927 --- /dev/null +++ b/app/assets/stylesheets/api_v1/messages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api_v1::messages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/api_v1/messages_controller.rb b/app/controllers/api_v1/messages_controller.rb new file mode 100644 index 0000000..af08b11 --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,7 @@ +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..7a70d95 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,7 +16,10 @@ def destroy @comment = current_user.comments.find( params[:id] ) @comment.destroy - redirect_to :back + respond_to do |format| + format.html { redirect_to :back } + format.js + end end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..66aedee 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,18 +4,18 @@ 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 + # @messages = @messages.where( :status => "pending" ) elsif params[:status] == "completed" - # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "completed" ) + @messages = @messages.completed + # @messages = @messages.where( :status => "completed" ) end if params[:days] - # TODO: @messages = @messages.within_days(params[:days].to_i) + # @messages = @messages.within_days(params[:days].to_i) @messages = @messages.where( ["created_at >= ?", Time.now - params[:days].to_i.days ] ) end end @@ -57,6 +57,7 @@ def destroy @message.destroy redirect_to root_path + end protected diff --git a/app/helpers/api_v1/messages_helper.rb b/app/helpers/api_v1/messages_helper.rb new file mode 100644 index 0000000..21af019 --- /dev/null +++ b/app/helpers/api_v1/messages_helper.rb @@ -0,0 +1,2 @@ +module ApiV1::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..ba54112 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,6 +4,16 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + has_many :likes, :dependent => :destroy + has_many :likes_by, :through => :likes, :source => :user, :dependent => :destroy + + has_many :subscriptions, :dependent => :destroy + has_many :subscriptions_by, :through => :likes, :source => :user, :dependent => :destroy + + scope :pending, -> {where( :status => "pending" )} + scope :completed, -> {where( :status => "completed" )} + scope :within_days , ->(time) { where "created_at >= ?", Time.now - time.to_i.days } + 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..96a48b0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,12 +7,19 @@ class User < ActiveRecord::Base has_many :messages has_many :comments + has_many :likes, :dependent => :destroy + has_many :like_messages, :through => :likes, :source => :message, :dependent => :destroy + + has_many :subscriptions, :dependent => :destroy + has_many :subscriptions_by, :through => :likes, :source => :message, :dependent => :destroy + def display_name self.email.split("@").first end def posts_count # TODO: 請完成我 + self.messages.size + self.comments.size 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..3ceeffa --- /dev/null +++ b/app/views/api_v1/messages/index.json.jbuilder @@ -0,0 +1,10 @@ +json.data do + json.array!(@message) do |message| + json.id message.id + json.status message.status + json.category_name message.category_name + json.title message.title + json.content message.content + json.created_at message.created_at + end + end \ No newline at end of file diff --git a/app/views/messages/destroy.js.erb b/app/views/messages/destroy.js.erb new file mode 100644 index 0000000..0982f0e --- /dev/null +++ b/app/views/messages/destroy.js.erb @@ -0,0 +1 @@ +$("#delete-block %>").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..a9ea23f 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -2,6 +2,15 @@ <%= simple_format @message.content %> +
<%= user.email.split("@").first%>
+ <% end %> +<%= user.email.split("@").first%>
+ <% end %> +<%= simple_format comment.content %> at <%= comment.created_at.to_s(:short) %> by <%= comment.user.display_name %> +
<%= simple_format comment.content %> at <%= comment.created_at.to_s(:short) %> by <%= comment.user.display_name %> - <% if comment.user == current_user %> + <% if comment.user == current_user %> <%# TODO: 修改成 AJAX 版本的刪除 %> - <%= link_to "Delete", message_comment_path(@message, comment), :method => :delete, :data => { :confirm => "Are u sure?"} %> - <% end %> -
- + + <%= link_to "Delete", message_comment_path(@message, comment), :method => :delete, :remote => true %> + + <% end %> + <% end %> diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..e008dfc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,10 +1,26 @@ Rails.application.routes.draw do devise_for :users + + resources :messages do resources :comments + + member do + post :like + post :dislike + post :subscribe + post :unsubscribe + end + end + + scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => {:format => :json} do + resources :messages 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/db/migrate/20160722090200_create_subscriptions.rb b/db/migrate/20160722090200_create_subscriptions.rb new file mode 100644 index 0000000..8a1ba1c --- /dev/null +++ b/db/migrate/20160722090200_create_subscriptions.rb @@ -0,0 +1,11 @@ +class CreateSubscriptions < ActiveRecord::Migration + def change + create_table :subscriptions do |t| + + t.integer :user_id, :index=> true + t.integer :message_id, :index => true + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160722090224_create_likes.rb b/db/migrate/20160722090224_create_likes.rb new file mode 100644 index 0000000..40ae4af --- /dev/null +++ b/db/migrate/20160722090224_create_likes.rb @@ -0,0 +1,9 @@ +class CreateLikes < ActiveRecord::Migration + def change + create_table :likes do |t| + t.integer :user_id, :index=> true + t.integer :message_id, :index => true + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ef7b5c0..60304ef 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: 20160722090224) 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..1742f3b 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -17,6 +17,12 @@ namespace :dev do :content => Faker::Lorem.paragraph, :user => users.sample, :created_at => Time.now - rand(30).days ) + users.sample(2).each do |u| + Subscription.create!(:user => u, :message => m) + end + users.sample(2).each do |u| + Like.create!(:user => u, :message => m) + end 5.times do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) 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..e1f1525 --- /dev/null +++ b/spec/controllers/api_v1/messages_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ApiV1::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..e078b0d --- /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 ApiV1::MessagesHelper. For example: +# +# describe ApiV1::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 ApiV1::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..f3fc259 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -25,11 +25,23 @@ describe "#words_count" do before do # TODO: 加 Message 和 Comment 測試資料 + @other_user = User.create!(:email => "ooxx@example.org", :password => "12345678") + + m1 =Message.create!(:user => @user, + :title => Faker::Lorem.words(5), + :content => Faker::Lorem.words(5)) + Comment.create!(:user => @other_user, + :content => Faker::Lorem.words(20), + :message_id => m1) + Comment.create!(:user => @user, + :content => Faker::Lorem.words(5), + :message_id => m1) end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do # TODO: 測試 words_count 方法 - + expect(@user.words_count).to eq(15) + expect(@other_user.words_count).to eq(20) end end