diff --git a/app/controllers/api_controller.rb b/app/controllers/api_controller.rb new file mode 100644 index 0000000..2afbd5c --- /dev/null +++ b/app/controllers/api_controller.rb @@ -0,0 +1,17 @@ +class ApiController < ActionController::Base + + # skip_before_action :verify_authenticity_token + before_action :enabled_cors + + + private + + def enabled_cors + headers['Access-Control-Allow-Origin'] = '*' + headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE' + headers['Access-Control-Allow-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, auth_token, *' + headers['Access-Control-Request-Method'] = 'GET, POST, PUT, DELETE' + headers['Access-Control-Request-Headers'] = 'Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, X-Remote, api_key, *' + end + +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..f7e6e7d --- /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 \ No newline at end of file diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..0f08afc 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -16,7 +16,11 @@ 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/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 0000000..2b845a9 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,25 @@ +class LikesController < ApplicationController + before_action :authenticate_user! + before_action :set_message + + def create + @subscribe = current_user.likes.build(:message_id => params[:message_id]) + @subscribe.save + + redirect_to message_path(@message) + end + + def destroy + @subscribe = current_user.likes.find_by_message_id(params[:message_id]) + @subscribe.destroy + + redirect_to message_path(@message) + end + + private + + def set_message + @message = Message.find(params[:message_id]) + end + +end \ No newline at end of file diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..fd9f63a 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(: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_week end end diff --git a/app/controllers/subscribes_controller.rb b/app/controllers/subscribes_controller.rb new file mode 100644 index 0000000..68ed8a5 --- /dev/null +++ b/app/controllers/subscribes_controller.rb @@ -0,0 +1,25 @@ +class SubscribesController < ApplicationController + before_action :authenticate_user! + before_action :set_message + + def create + @subscribe = current_user.subscribes.build(:message_id => params[:message_id]) + @subscribe.save + + redirect_to message_path(@message) + end + + def destroy + @subscribe = current_user.subscribes.find_by_message_id(params[:message_id]) + @subscribe.destroy + + redirect_to message_path(@message) + end + + private + + def set_message + @message = Message.find(params[:message_id]) + end + +end \ No newline at end of file diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..7f26420 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,6 @@ +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..e28d2c5 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -3,9 +3,17 @@ class Message < ActiveRecord::Base belongs_to :user has_many :comments, :dependent => :destroy + has_many :subscribes, :dependent => :destroy + has_many :subscribe_users, :through => :subscribes, :source => :user + has_many :likes, :dependent => :destroy + has_many :like_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") } + scope :within_week, -> { where( ["created_at >= ?", Time.now - 7.days] ) } + end diff --git a/app/models/subscribe.rb b/app/models/subscribe.rb new file mode 100644 index 0000000..23c4669 --- /dev/null +++ b/app/models/subscribe.rb @@ -0,0 +1,6 @@ +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..56f2716 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,8 +4,12 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - has_many :messages - has_many :comments + has_many :messages, :dependent => :destroy + has_many :comments, :dependent => :destroy + has_many :subscribes, :dependent => :destroy + has_many :subscribe_message, :through => :subscribes, :source => :message + has_many :likes, :dependent => :destroy + has_many :like_message, :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 @@ -29,4 +34,8 @@ def words_count return count end + def short_name + self.email.split("@").first + end + end diff --git a/app/views/api_v1/messages/_messages.json.jbuilder b/app/views/api_v1/messages/_messages.json.jbuilder new file mode 100644 index 0000000..7906cf8 --- /dev/null +++ b/app/views/api_v1/messages/_messages.json.jbuilder @@ -0,0 +1 @@ +json.( m, :id, :status, :category_name, :title, :content, :created_at) \ No newline at end of file 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..adff53e --- /dev/null +++ b/app/views/api_v1/messages/index.json.jbuilder @@ -0,0 +1 @@ +json.messages @messages , partial: "messages" , as: :m \ 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..899e945 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -1,6 +1,22 @@
<%= simple_format comment.content %> at <%= comment.created_at.to_s(:short) %> by <%= comment.user.display_name %> +