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 @@ <% if msg.user == current_user %> <%= link_to "Edit", edit_message_path(msg) %> - <%= link_to "Delete", message_path(msg), :method => :delete, :data => { :confirm => "Are u sure?"} %> + <%= link_to "Delete", message_path(msg), :method => :delete, :data => { :confirm => "Are u sure?"}%> <% end %> diff --git a/app/views/messages/show.html.erb b/app/views/messages/show.html.erb index 41401f6..e2b6d9f 100644 --- a/app/views/messages/show.html.erb +++ b/app/views/messages/show.html.erb @@ -17,15 +17,42 @@ <% end %> + + + + +
+
+ + <% @message.liked_users.each do |i| %> + + + <% end %> +

Who likes?

<%= i.display_name %>
+ + + + +
+
+ + <% @message.subscribed_users.each do |s| %> + + + <% end %> + +

Who subscribed?

<%= s.display_name %>
+
<% @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 %>

- +
<% end %> diff --git a/config/routes.rb b/config/routes.rb index 74d4dbf..5c90fbe 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,6 +11,10 @@ # You can have the root of your site routed with "root" root 'messages#index' + scope :path => '/api/v1/', :module => "api_v1", :defaults => { :format => :json }, :as => 'v1' do + resources :messages, only: [:index] + end + # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/migrate/20161007210820_create_likes.rb b/db/migrate/20161007210820_create_likes.rb new file mode 100644 index 0000000..e038772 --- /dev/null +++ b/db/migrate/20161007210820_create_likes.rb @@ -0,0 +1,11 @@ +class CreateLikes < ActiveRecord::Migration + def change + create_table :likes do |t| + t.integer :user_id + t.integer :message_id + t.timestamps null: false + end + add_index :likes, :user_id + add_index :likes, :message_id + end +end diff --git a/db/migrate/20161007210831_create_subscriptions.rb b/db/migrate/20161007210831_create_subscriptions.rb new file mode 100644 index 0000000..cac46f1 --- /dev/null +++ b/db/migrate/20161007210831_create_subscriptions.rb @@ -0,0 +1,11 @@ +class CreateSubscriptions < ActiveRecord::Migration + def change + create_table :subscriptions do |t| + t.integer :user_id + t.integer :message_id + t.timestamps null: false + end + add_index :subscriptions, :user_id + add_index :subscriptions, :message_id + end +end diff --git a/db/schema.rb b/db/schema.rb index ef7b5c0..03dee8b 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: 20161007210831) do create_table "comments", force: :cascade do |t| t.text "content" @@ -23,6 +23,16 @@ 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 + + add_index "likes", ["message_id"], name: "index_likes_on_message_id" + add_index "likes", ["user_id"], name: "index_likes_on_user_id" + create_table "messages", force: :cascade do |t| t.string "title" t.text "content" @@ -36,6 +46,16 @@ add_index "messages", ["status"], name: "index_messages_on_status" add_index "messages", ["user_id"], name: "index_messages_on_user_id" + create_table "subscriptions", 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 + + add_index "subscriptions", ["message_id"], name: "index_subscriptions_on_message_id" + add_index "subscriptions", ["user_id"], name: "index_subscriptions_on_user_id" + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 4673e72..0a6d335 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -17,6 +17,14 @@ namespace :dev do :content => Faker::Lorem.paragraph, :user => users.sample, :created_at => Time.now - rand(30).days ) + a = (users[0].id..users[9].id).to_a.shuffle.take(2) + m.liked_users << User.find(a[0]) + m.liked_users << User.find(a[1]) + + b = (users[0].id..users[9].id).to_a.shuffle.take(2) + m.subscribed_users << User.find(b[0]) + m.subscribed_users << User.find(b[1]) + 5.times do m.comments.create!( :content => Faker::Lorem.paragraph, :user => users.sample ) 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_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 diff --git a/spec/models/subscription_spec.rb b/spec/models/subscription_spec.rb new file mode 100644 index 0000000..d40bf0b --- /dev/null +++ b/spec/models/subscription_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Subscription, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 528cc38..a6fe9fc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -25,11 +25,13 @@ describe "#words_count" do before do # TODO: 加 Message 和 Comment 測試資料 + m = Message.create!( user: @user, title: "final project", content: "coming soon") + Comment.create!(user: @user, content: "team work", message: m) end it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do # TODO: 測試 words_count 方法 - + expect( @user.words_count ).to eq(6) end end