diff --git a/Gemfile b/Gemfile index bde2f2e..d9ae7a3 100644 --- a/Gemfile +++ b/Gemfile @@ -43,7 +43,6 @@ group :development, :test do gem 'byebug' # Access an IRB console on exception pages or by using <%= console %> in views - gem 'web-console', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' diff --git a/Gemfile.lock b/Gemfile.lock index e1b93b6..ee6949f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,59 +36,58 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (6.0.0) - bcrypt (3.1.10) - binding_of_caller (0.7.2) - debug_inspector (>= 0.0.1) + arel (6.0.3) + bcrypt (3.1.11) builder (3.2.2) - byebug (5.0.0) - columnize (= 0.9.0) - coffee-rails (4.1.0) + byebug (9.0.5) + coffee-rails (4.1.1) coffee-script (>= 2.2.0) - railties (>= 4.0.0, < 5.0) + railties (>= 4.0.0, < 5.1.x) coffee-script (2.4.1) coffee-script-source execjs - coffee-script-source (1.9.1.1) - columnize (0.9.0) - debug_inspector (0.0.2) - devise (3.5.1) + coffee-script-source (1.10.0) + concurrent-ruby (1.0.2) + devise (4.2.0) bcrypt (~> 3.0) orm_adapter (~> 0.1) - railties (>= 3.2.6, < 5) + railties (>= 4.1.0, < 5.1) responders - thread_safe (~> 0.1) warden (~> 1.2.3) diff-lcs (1.2.5) erubis (2.7.0) - execjs (2.5.2) - faker (1.4.3) + execjs (2.7.0) + faker (1.6.5) i18n (~> 0.5) - globalid (0.3.5) + globalid (0.3.6) activesupport (>= 4.1.0) i18n (0.7.0) - jbuilder (2.3.0) - activesupport (>= 3.0.0, < 5) + jbuilder (2.6.0) + activesupport (>= 3.0.0, < 5.1) multi_json (~> 1.2) - jquery-rails (4.0.4) - rails-dom-testing (~> 1.0) + jquery-rails (4.1.1) + rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.3) - kaminari (0.16.3) + kaminari (0.17.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) - loofah (2.0.2) + loofah (2.0.3) nokogiri (>= 1.5.9) - mail (2.6.3) - mime-types (>= 1.16, < 3) - mime-types (2.6.1) - mini_portile (0.6.2) - minitest (5.7.0) - multi_json (1.11.1) - nokogiri (1.6.6.2) - mini_portile (~> 0.6.0) + mail (2.6.4) + mime-types (>= 1.16, < 4) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + mini_portile2 (2.1.0) + minitest (5.9.0) + multi_json (1.12.1) + nokogiri (1.6.8) + mini_portile2 (~> 2.1.0) + pkg-config (~> 1.1.7) orm_adapter (0.5.0) + pkg-config (1.1.7) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -105,73 +104,70 @@ GEM sprockets-rails rails-deprecated_sanitizer (1.0.3) activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.6) + rails-dom-testing (1.0.7) activesupport (>= 4.2.0.beta, < 5.0) nokogiri (~> 1.6.0) rails-deprecated_sanitizer (>= 1.0.1) - rails-html-sanitizer (1.0.2) + rails-html-sanitizer (1.0.3) loofah (~> 2.0) railties (4.2.2) actionpack (= 4.2.2) activesupport (= 4.2.2) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (10.4.2) - rdoc (4.2.0) - responders (2.1.0) - railties (>= 4.2.0, < 5) - rspec-core (3.3.2) - rspec-support (~> 3.3.0) - rspec-expectations (3.3.1) + rake (11.2.2) + rdoc (4.2.2) + json (~> 1.4) + responders (2.2.0) + railties (>= 4.2.0, < 5.1) + rspec-core (3.5.1) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-mocks (3.3.2) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.3.0) - rspec-rails (3.3.3) - actionpack (>= 3.0, < 4.3) - activesupport (>= 3.0, < 4.3) - railties (>= 3.0, < 4.3) - rspec-core (~> 3.3.0) - rspec-expectations (~> 3.3.0) - rspec-mocks (~> 3.3.0) - rspec-support (~> 3.3.0) - rspec-support (3.3.0) - sass (3.4.15) - sass-rails (5.0.3) - railties (>= 4.0.0, < 5.0) + rspec-support (~> 3.5.0) + rspec-rails (3.5.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + sass (3.4.22) + sass-rails (5.0.5) + railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) - tilt (~> 1.1) + tilt (>= 1.1, < 3) sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - spring (1.3.6) - sprockets (3.2.0) - rack (~> 1.0) - sprockets-rails (2.3.2) - actionpack (>= 3.0) - activesupport (>= 3.0) - sprockets (>= 2.8, < 4.0) - sqlite3 (1.3.10) + spring (1.7.2) + sprockets (3.7.0) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.1.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.3.11) thor (0.19.1) thread_safe (0.3.5) - tilt (1.4.1) - turbolinks (2.5.3) - coffee-rails + tilt (2.0.5) + turbolinks (5.0.0) + turbolinks-source (~> 5) + turbolinks-source (5.0.0) tzinfo (1.2.2) thread_safe (~> 0.1) - uglifier (2.7.1) - execjs (>= 0.3.0) - json (>= 1.8.0) - warden (1.2.3) + uglifier (3.0.0) + execjs (>= 0.3.0, < 3) + warden (1.2.6) rack (>= 1.0) - web-console (2.1.3) - activemodel (>= 4.0) - binding_of_caller (>= 0.7.2) - railties (>= 4.0) - sprockets-rails (>= 2.0, < 4.0) PLATFORMS ruby @@ -192,7 +188,6 @@ DEPENDENCIES sqlite3 turbolinks uglifier (>= 1.3.0) - web-console (~> 2.0) BUNDLED WITH - 1.10.5 + 1.12.4 diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb new file mode 100644 index 0000000..75bb517 --- /dev/null +++ b/app/controllers/api_controller.rb @@ -0,0 +1,2 @@ +class ApiController < ActionController::Base +end \ No newline at end of file diff --git a/app/controllers/api_v1/messages_controller.rb b/app/controllers/api_v1/messages_controller.rb new file mode 100644 index 0000000..3731df9 --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,5 @@ +class ApiV1::MessagesController < ApiController + def index + @message = Message.all + end +end \ No newline at end of file diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..5b024bb 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 # destroy.js.erb + end end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..0275b0c 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,19 +4,19 @@ 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.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/like.rb b/app/models/like.rb new file mode 100644 index 0000000..3af785e --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,4 @@ +class Like < ActiveRecord::Base + belongs_to :message + belongs_to :user +end diff --git a/app/models/message.rb b/app/models/message.rb index e24a8b2..04c1594 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.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..b312e30 --- /dev/null +++ b/app/models/subscription.rb @@ -0,0 +1,4 @@ +class Subscription < ActiveRecord::Base + belongs_to :message + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 6d01aa9..782cf20 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.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..d13131c --- /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/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..e02f77e --- /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..66cfb79 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -2,6 +2,15 @@ <%= simple_format @message.content %> +

Likes

+<% @message.likes_by.each do |user| %> +

<%= user.email.split("@").first%>

+<% end %> +

Subscribed

+<% @message.subscriptions_by.each do |user| %> +

<%= user.email.split("@").first%>

+<% end %> +

Comments list

<%= form_for [@message, @comment] do |f| %> @@ -18,14 +27,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?"} %> + <%= link_to "Delete", message_comment_path(@message, comment), :method => :delete, :remote => true %> <% end %>

+
<% end %> diff --git a/config/application.rb b/config/application.rb index 4d3aa47..9ebd74d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,7 +1,7 @@ require File.expand_path('../boot', __FILE__) require 'rails/all' - +require 'yaml' # Require the gems listed in Gemfile, including any gems # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..a2eac1d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,10 @@ resources :comments 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/20160722082411_create_subscriptions.rb b/db/migrate/20160722082411_create_subscriptions.rb new file mode 100644 index 0000000..6555f86 --- /dev/null +++ b/db/migrate/20160722082411_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/20160722082420_create_likes.rb b/db/migrate/20160722082420_create_likes.rb new file mode 100644 index 0000000..3ecdab6 --- /dev/null +++ b/db/migrate/20160722082420_create_likes.rb @@ -0,0 +1,11 @@ +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..2090cb8 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: 20160722082420) 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..b41b88c 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/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..880d358 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -25,10 +25,25 @@ 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(10), + :content => Faker::Lorem.words(20)) + Comment.create!(:user => @other_user, + :content => Faker::Lorem.words(20), + :message_id => m1) + Comment.create!(:user => @user, + :content => Faker::Lorem.words(15), + :message_id => m1) + # 共45字 for @user + # 共20字 for @other_user end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do # TODO: 測試 words_count 方法 + expect(@user.words_count).to eq(45) + expect(@other_user.words_count).to eq(20) end end