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 %> +
<%= 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 %> -
+ +