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..5bdcdcd --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,11 @@ +class ApiV1::MessagesController < ApiController + def index + @messages = Message.all + render :json => @messages.to_json + end + + def show + @message = Message.find(params[:id]) + render :json => @message.to_json + end +end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..e03036d 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..2ef59c6 100644 --- a/app/controllers/messages_controller.rb +++ b/app/controllers/messages_controller.rb @@ -4,14 +4,16 @@ 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(:comments).order("id DESC").page( params[:page] ) if params[:status] == "pending" # TODO: @messages = @messages.pending - @messages = @messages.where( :status => "pending" ) + #@messages = @messages.where( :status => "pending" ) + @messages = @messages.pending elsif params[:status] == "completed" # TODO: @messages = @messages.completed - @messages = @messages.where( :status => "completed" ) + #@messages = @messages.where( :status => "completed" ) + @messages = @messages.completed end if params[:days] @@ -22,6 +24,11 @@ def index def show @message = Message.find( params[:id] ) + + #Message.last.likes.first.user.email + @who_like_message = @message.likes + @who_subscribe_message = @message.subscribes + @comment = Comment.new end @@ -59,6 +66,29 @@ def destroy redirect_to root_path end + def like + @message = Message.find(params[:id]) + if current_user.like_message?(@message) + current_user.like_messages.delete(@message) + else + current_user.like_messages << @message + end + + redirect_to message_path(@message) + end + + def subscribe + @message = Message.find(params[:id]) + if current_user.subscribe_message?(@message) + current_user.subscribe_messages.delete(@message) + else + current_user.subscribe_messages << @message + end + + redirect_to message_path(@message) + end + + protected def message_params 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..5519adf --- /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..bb8dba7 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,6 +4,18 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + has_many :likes + + has_many :subscribes + + scope :pending, -> {where(:status => :pending)} + + scope :completed, -> {where(:status => :completed)} + + scope :within_days, -> {where(':status => :created_at?', Time.now - params[:days].to_i.days)} + + #scope :who_like_it, -> {where(:status => :completed)} + def last_comment_summary self.comments.last.try(:content).try(:truncate, 20) end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb new file mode 100644 index 0000000..63706d8 --- /dev/null +++ b/app/models/subscribe.rb @@ -0,0 +1,4 @@ +class Subscribe < ActiveRecord::Base + belongs_to :user + belongs_to :message +end diff --git a/app/models/user.rb b/app/models/user.rb index 6d01aa9..73912e7 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,12 +7,27 @@ class User < ActiveRecord::Base has_many :messages has_many :comments + has_many :likes + has_many :like_messages, :through => :likes, :source => :message + + has_many :subscribes + has_many :subscribe_messages, :through => :subscribes, :source => :message + + def like_message?(message) + self.like_messages.include?(message) + end + + def subscribe_message?(message) + self.subscribe_messages.include?(message) + end + def display_name self.email.split("@").first end def posts_count # TODO: 請完成我 + self.messages.size+self.comments.size end def words_count diff --git a/app/views/comments/destroy.js.erb b/app/views/comments/destroy.js.erb new file mode 100644 index 0000000..840e620 --- /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/show.html.erb b/app/views/messages/show.html.erb index 41401f6..68ddc4f 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,9 +1,42 @@
@@ -18,14 +51,14 @@ <% 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 %>
- +