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