diff --git a/Gemfile.lock b/Gemfile.lock index e1b93b6..6dfa575 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -116,7 +116,7 @@ GEM activesupport (= 4.2.2) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (10.4.2) + rake (11.3.0) rdoc (4.2.0) responders (2.1.0) railties (>= 4.2.0, < 5) @@ -195,4 +195,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.10.5 + 1.12.5 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_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..784099d --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,6 @@ +class ApiV1::MessagesController < ApplicationController + def index + messages = Message.all + render json: messages, only: [:id, :status, :category_name, :title, :content, :created_at] + end +end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..d379441 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,7 +16,9 @@ def destroy @comment = current_user.comments.find( params[:id] ) @comment.destroy - redirect_to :back + respond_to do |format| + format.js + end end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..7141615 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,19 +4,20 @@ 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" ) - elsif params[:status] == "completed" + if params[:status] == "pending" + @messages = @messages.pending + # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "completed" ) + elsif params[: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.within_days(params[:days].to_i) end end 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..d807d5b --- /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..79a9d28 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -1,11 +1,20 @@ class Message < ActiveRecord::Base - belongs_to :user - - has_many :comments, :dependent => :destroy - def last_comment_summary self.comments.last.try(:content).try(:truncate, 20) end + belongs_to :user + + has_many :comments, :dependent => :destroy + + scope :pending, -> { where(:status => 'pending')} + scope :completed, -> { where(:status => 'completed')} + scope :within_days, -> (date){ where( "created_at >= ?", Time.now - date.days ) } + + has_many :subscriptions, dependent: :destroy + has_many :subscribed_users, through: :subscriptions, source: :user, dependent: :destroy + + has_many :likes, dependent: :destroy + has_many :liked_users, through: :likes, source: :user, dependent: :destroy end diff --git a/app/models/subscription.rb b/app/models/subscription.rb new file mode 100644 index 0000000..d75cb6b --- /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..d0cbc40 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 :subscriptions, dependent: :destroy + has_many :subscription_messages, through: :subscriptions, source: :message, dependent: :destroy + + has_many :likes, dependent: :destroy + has_many :like_messages, 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 b/app/views/api_v1/messages/index.json new file mode 100644 index 0000000..664d8f7 --- /dev/null +++ b/app/views/api_v1/messages/index.json @@ -0,0 +1,9 @@ +messages.each |g| +{ + id: g.id + status: g.status + category_name: g.category_name + title: g.title + content: g.content + created_at: g.created_at +} diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..30ae41f --- /dev/null +++ b/app/views/comments/destroy.js.erb @@ -0,0 +1 @@ +$('#c-<%= @comment.id %>').remove(); \ No newline at end of file diff --git a/app/views/messages/index.html.erb b/app/views/messages/index.html.erb index edac212..7f022e9 100644 --- a/app/views/messages/index.html.erb +++ b/app/views/messages/index.html.erb @@ -34,7 +34,7 @@
Who likes? |
+
<%= i.display_name %> | +
Who subscribed? |
+
<%= s.display_name %> | +
<%= 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 %>
- +