diff --git a/app/assets/javascripts/api.coffee b/app/assets/javascripts/api.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/api.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/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.scss b/app/assets/stylesheets/api.scss new file mode 100644 index 0000000..5836003 --- /dev/null +++ b/app/assets/stylesheets/api.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the api controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ 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..5f381d2 --- /dev/null +++ b/app/controllers/api_controller.rb @@ -0,0 +1,2 @@ +class ApiController < ActionController::Base +end diff --git a/app/controllers/api_v1/messages_controller.rb b/app/controllers/api_v1/messages_controller.rb new file mode 100644 index 0000000..1226359 --- /dev/null +++ b/app/controllers/api_v1/messages_controller.rb @@ -0,0 +1,6 @@ +class ApiV1::MessagesController < ApiController + def messages + + render :json => Message.all + end +end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e9d6aaa..c4b674a 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -15,8 +15,11 @@ def create def destroy @comment = current_user.comments.find( params[:id] ) @comment.destroy - - redirect_to :back + + respond_to do |format| + format.js + end + # redirect_to :back end protected diff --git a/app/controllers/messages_controller.rb b/app/controllers/messages_controller.rb index 96f65e5..4e82e11 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" ) + @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 @@ -55,7 +56,9 @@ def update def destroy @message = current_user.messages.find( params[:id] ) @message.destroy - + + + redirect_to root_path end diff --git a/app/helpers/api_helper.rb b/app/helpers/api_helper.rb new file mode 100644 index 0000000..1f82fcf --- /dev/null +++ b/app/helpers/api_helper.rb @@ -0,0 +1,2 @@ +module ApiHelper +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/comment.rb b/app/models/comment.rb index aed6653..76e5ac4 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -3,4 +3,5 @@ class Comment < ActiveRecord::Base belongs_to :user belongs_to :message + end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 0000000..29ae813 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,7 @@ +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..32d2805 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -4,8 +4,35 @@ class Message < ActiveRecord::Base has_many :comments, :dependent => :destroy + + has_many :like_users, :through => :likes, :source => :user + has_many :likes + + + + 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_days, -> (days){ where( "created_at >= ?", Time.now - 7.days ) } + # def pending + # self.where( :status => "pending" ) + # end + + # def completed + # self.where( :status => "completed" ) + # end + + # def within_days(aaa) + # self.where( ["created_at >= ?", Time.now - aaa.to_i.days ] ) + # end + + + + end diff --git a/app/models/user.rb b/app/models/user.rb index 6d01aa9..95c0c5b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -7,6 +7,10 @@ class User < ActiveRecord::Base has_many :messages has_many :comments + + has_many :like_messages, :through => :likes, :source => :message + has_many :likes + def display_name self.email.split("@").first 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/show.html.erb b/app/views/messages/show.html.erb index 41401f6..eb9b05e 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -18,14 +18,15 @@ <% 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 %> <% end %>

- +
+ <% end %> diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..f89fa21 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,9 +2,14 @@ devise_for :users resources :messages do + resources :likes resources :comments end + scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => { :format => :json } do + get "/messages" => "messages#messages" + end + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20160429090646_create_likes.rb b/db/migrate/20160429090646_create_likes.rb new file mode 100644 index 0000000..76ba56a --- /dev/null +++ b/db/migrate/20160429090646_create_likes.rb @@ -0,0 +1,10 @@ +class CreateLikes < ActiveRecord::Migration + def change + create_table :likes do |t| + t.integer :user_id + t.integer :message_id + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ef7b5c0..3731a39 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150728165437) do +ActiveRecord::Schema.define(version: 20160429090646) do create_table "comments", force: :cascade do |t| t.text "content" @@ -23,6 +23,13 @@ add_index "comments", ["message_id"], name: "index_comments_on_message_id" + create_table "likes", force: :cascade do |t| + t.integer "user_id" + t.integer "message_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "messages", force: :cascade do |t| t.string "title" t.text "content" diff --git a/spec/controllers/api_controller_spec.rb b/spec/controllers/api_controller_spec.rb new file mode 100644 index 0000000..7a0d335 --- /dev/null +++ b/spec/controllers/api_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ApiController, type: :controller do + +end diff --git a/spec/controllers/api_v1/messages_controller_spec.rb b/spec/controllers/api_v1/messages_controller_spec.rb new file mode 100644 index 0000000..e1f1525 --- /dev/null +++ b/spec/controllers/api_v1/messages_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ApiV1::MessagesController, type: :controller do + +end diff --git a/spec/helpers/api_helper_spec.rb b/spec/helpers/api_helper_spec.rb new file mode 100644 index 0000000..0be225c --- /dev/null +++ b/spec/helpers/api_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ApiHelper. For example: +# +# describe ApiHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ApiHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/api_v1/messages_helper_spec.rb b/spec/helpers/api_v1/messages_helper_spec.rb new file mode 100644 index 0000000..e078b0d --- /dev/null +++ b/spec/helpers/api_v1/messages_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ApiV1::MessagesHelper. For example: +# +# describe ApiV1::MessagesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ApiV1::MessagesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb new file mode 100644 index 0000000..76ea93d --- /dev/null +++ b/spec/models/like_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Like, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end