diff --git a/Gemfile b/Gemfile index bde2f2e..2962e37 100644 --- a/Gemfile +++ b/Gemfile @@ -38,6 +38,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test do gem "rspec-rails" + gem 'guard-rspec', require: false # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' diff --git a/Gemfile.lock b/Gemfile.lock index e1b93b6..b1ca21d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -43,6 +43,7 @@ GEM builder (3.2.2) byebug (5.0.0) columnize (= 0.9.0) + coderay (1.1.1) coffee-rails (4.1.0) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.0) @@ -64,8 +65,24 @@ GEM execjs (2.5.2) faker (1.4.3) i18n (~> 0.5) + ffi (1.9.14) + formatador (0.2.5) globalid (0.3.5) activesupport (>= 4.1.0) + guard (2.14.0) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + lumberjack (~> 1.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.9.12) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-compat (1.2.1) + guard-rspec (4.7.2) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) i18n (0.7.0) jbuilder (2.3.0) activesupport (>= 3.0.0, < 5) @@ -78,17 +95,31 @@ GEM kaminari (0.16.3) actionpack (>= 3.0.0) activesupport (>= 3.0.0) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) loofah (2.0.2) nokogiri (>= 1.5.9) + lumberjack (1.0.10) mail (2.6.3) mime-types (>= 1.16, < 3) + method_source (0.8.2) mime-types (2.6.1) mini_portile (0.6.2) minitest (5.7.0) multi_json (1.11.1) + nenv (0.3.0) nokogiri (1.6.6.2) mini_portile (~> 0.6.0) + notiffany (0.1.0) + nenv (~> 0.1) + shellany (~> 0.0) orm_adapter (0.5.0) + pry (0.10.4) + coderay (~> 1.1.0) + method_source (~> 0.8.1) + slop (~> 3.4) rack (1.6.4) rack-test (0.6.3) rack (>= 1.0) @@ -117,9 +148,16 @@ GEM rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) rake (10.4.2) + rb-fsevent (0.9.7) + rb-inotify (0.9.7) + ffi (>= 0.5.0) rdoc (4.2.0) responders (2.1.0) railties (>= 4.2.0, < 5) + rspec (3.3.0) + rspec-core (~> 3.3.0) + rspec-expectations (~> 3.3.0) + rspec-mocks (~> 3.3.0) rspec-core (3.3.2) rspec-support (~> 3.3.0) rspec-expectations (3.3.1) @@ -137,6 +175,7 @@ GEM rspec-mocks (~> 3.3.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) + ruby_dep (1.3.1) sass (3.4.15) sass-rails (5.0.3) railties (>= 4.0.0, < 5.0) @@ -147,6 +186,8 @@ GEM sdoc (0.4.1) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + shellany (0.0.1) + slop (3.6.0) spring (1.3.6) sprockets (3.2.0) rack (~> 1.0) @@ -181,6 +222,7 @@ DEPENDENCIES coffee-rails (~> 4.1.0) devise faker + guard-rspec jbuilder (~> 2.0) jquery-rails kaminari @@ -195,4 +237,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.10.5 + 1.12.3 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000..3215f01 --- /dev/null +++ b/Guardfile @@ -0,0 +1,70 @@ +# A sample Guardfile +# More info at https://github.com/guard/guard#readme + +## Uncomment and set this to only include directories you want to watch +# directories %w(app lib config test spec features) \ +# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")} + +## Note: if you are using the `directories` clause above and you are not +## watching the project directory ('.'), then you will want to move +## the Guardfile to a watched dir and symlink it back, e.g. +# +# $ mkdir config +# $ mv Guardfile config/ +# $ ln -s config/Guardfile . +# +# and, you'll have to watch "config/Guardfile" instead of "Guardfile" + +# Note: The cmd option is now required due to the increasing number of ways +# rspec may be run, below are examples of the most common uses. +# * bundler: 'bundle exec rspec' +# * bundler binstubs: 'bin/rspec' +# * spring: 'bin/rspec' (This will use spring if running and you have +# installed the spring binstubs per the docs) +# * zeus: 'zeus rspec' (requires the server to be started separately) +# * 'just' rspec: 'rspec' + +guard :rspec, cmd: "bundle exec rspec" do + require "guard/rspec/dsl" + dsl = Guard::RSpec::Dsl.new(self) + + # Feel free to open issues for suggestions and improvements + + # RSpec files + rspec = dsl.rspec + watch(rspec.spec_helper) { rspec.spec_dir } + watch(rspec.spec_support) { rspec.spec_dir } + watch(rspec.spec_files) + + # Ruby files + ruby = dsl.ruby + dsl.watch_spec_files_for(ruby.lib_files) + + # Rails files + rails = dsl.rails(view_extensions: %w(erb haml slim)) + dsl.watch_spec_files_for(rails.app_files) + dsl.watch_spec_files_for(rails.views) + + watch(rails.controllers) do |m| + [ + rspec.spec.call("routing/#{m[1]}_routing"), + rspec.spec.call("controllers/#{m[1]}_controller"), + rspec.spec.call("acceptance/#{m[1]}") + ] + end + + # Rails config changes + watch(rails.spec_helper) { rspec.spec_dir } + watch(rails.routes) { "#{rspec.spec_dir}/routing" } + watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" } + + # Capybara features specs + watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") } + watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") } + + # Turnip features and steps + watch(%r{^spec/acceptance/(.+)\.feature$}) + watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m| + Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance" + end +end 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/api_controller.rb b/app/controllers/api_v1/api_controller.rb new file mode 100644 index 0000000..75bb517 --- /dev/null +++ b/app/controllers/api_v1/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..796ee8a --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,7 @@ +class ApiV1::MessagesController < ApiController + + 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..5353ded 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -1,27 +1,26 @@ class MessagesController < ApplicationController - + before_action :set_message ,:only =>[:show,:subscribe,:unsubscribe,:like,:dislike] 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] ) + @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 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 def show - @message = Message.find( params[:id] ) @comment = Comment.new end @@ -59,7 +58,43 @@ def destroy redirect_to root_path end + def subscribe + @subscription = @message.find_my_subscription(current_user) + unless @subscription + @subscription = Subscription.create!( :message => @message, :user => current_user ) + end + + redirect_to :back + end + + def unsubscribe + @subscription = @message.find_my_subscription(current_user) + @subscription.destroy + + redirect_to :back + end + + def like + @like = @message.find_my_like(current_user) + unless @like + @like = Like.create!( :message => @message, :user => current_user ) + end + + redirect_to :back + end + + def dislike + @like = @message.find_my_like(current_user) + @like.destroy + + redirect_to :back + end + + protected + def set_message + @message = Message.find( params[:id] ) + end def message_params params.require(:message).permit(:title, :content, :category_name) diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..88c087f --- /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..65749a6 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,8 +4,27 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + has_many :subscriptions, :dependent => :destroy + has_many :subscribed_users, :through => :subscriptions, :source => :user + has_many :likes, :dependent => :destroy + has_many :liked_users, :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" )} + + def self.within_days(params) + where( ["created_at >= ?", Time.now - params.days ] ) + end + + def find_my_subscription(user) + self.subscriptions.where( :user => user ).first + end + def find_my_like(user) + self.likes.where( :user => user ).first + end end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 0000000..24876ed --- /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..0097b43 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,10 @@ class User < ActiveRecord::Base has_many :messages has_many :comments + has_many :subscriptions, :dependent => :destroy + has_many :subscribed_messages, :through => :subscriptions, :source => :message + has_many :likes, :dependent => :destroy + has_many :like_messages, :through => :likes, :source => :message def display_name self.email.split("@").first @@ -13,6 +17,7 @@ def display_name 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..de88686 --- /dev/null +++ b/app/views/api_v1/messages/index.json.jbuilder @@ -0,0 +1,13 @@ + +json.message @messages do |m| + json.id m.id + json.status m.status + json.category_name m.category_name + json.title m.title + json.content m.content + json.created_at m.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..1426a1e --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1 @@ +$("#comment-<%= @comment.id %>").remove(); diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index edac212..3888c99 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -37,6 +37,25 @@ <%= link_to "Delete", message_path(msg), :method => :delete, :data => { :confirm => "Are u sure?"} %> <% end %> + + <% subscription = msg.find_my_subscription(current_user) %> + + <% if subscription %> + <%= link_to "Unsubscribe", unsubscribe_message_path(msg), :class => "btn btn-primary", :method => :post %> + <% else %> + <%= link_to "Subscribe", subscribe_message_path(msg), :class => "btn btn-primary", :method => :post %> + <% end %> + + + <% like = msg.find_my_like(current_user) %> + + <% if like %> + <%= link_to "dislike", dislike_message_path(msg), :class => "btn btn-primary", :method => :post %> + <% else %> + <%= link_to "like", like_message_path(msg), :class => "btn btn-primary", :method => :post %> + <% end %> + + <% end %> diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 41401f6..4200de7 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,5 +1,15 @@

<%= @message.title %>

+like people : +<% @message.liked_users.each do |l| %> + "<%=l.display_name %>" +<%end%> +
+like subscription : +<% @message.subscribed_users.each do |l| %> + "<%=l.display_name %>" +<%end%> + <%= simple_format @message.content %>

Comments list

@@ -18,14 +28,16 @@ <% 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,:data => { :confirm => "Are u sure?"} %> <% end %> -

+

+
<% end %> + diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..c519bd0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,6 +3,12 @@ resources :messages do resources :comments + member do + post :subscribe + post :unsubscribe + post :like + post :dislike + end end # The priority is based upon order of creation: first created -> highest priority. @@ -11,6 +17,10 @@ # You can have the root of your site routed with "root" root 'messages#index' + scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => { :format => :json } do + resources :messages ,:only => [:index] + end + # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/migrate/20160722084638_create_subscriptions.rb b/db/migrate/20160722084638_create_subscriptions.rb new file mode 100644 index 0000000..b591b9a --- /dev/null +++ b/db/migrate/20160722084638_create_subscriptions.rb @@ -0,0 +1,9 @@ +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/20160722091300_create_likes.rb b/db/migrate/20160722091300_create_likes.rb new file mode 100644 index 0000000..4567c54 --- /dev/null +++ b/db/migrate/20160722091300_create_likes.rb @@ -0,0 +1,10 @@ +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..165891b 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: 20160722091300) 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..0f1d311 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -21,6 +21,27 @@ namespace :dev do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) end + + i = 0 + until i > 1 do + user = User.all.sample + subscription = m.find_my_subscription(user) + unless subscription + Subscription.create!( :message_id => m.id, :user_id => user.id ) + i += 1 + end + end + + i = 0 + until i > 1 do + user1 = User.all.sample + like = m.find_my_like(user1) + unless like + Like.create!( :message_id => m.id, :user_id => user1.id ) + i += 1 + end + end + end 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..4e672c5 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -24,12 +24,14 @@ describe "#words_count" do before do - # TODO: 加 Message 和 Comment 測試資料 + m2 = Message.create!( :user => @user ,:content => "test a 3432f258672__") + Comment.create!( :user => @user, :message => m2, :content =>"a great tea" ) + Comment.create!( :user => @user, :message => m2, :content =>" 12345 + 88654 " ) end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do # TODO: 測試 words_count 方法 - + expect( @user.words_count ).to eq(9) end end