Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull request #52

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -195,4 +195,4 @@ DEPENDENCIES
web-console (~> 2.0)

BUNDLED WITH
1.10.5
1.12.5
3 changes: 3 additions & 0 deletions app/assets/javascripts/api_v1/messages.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/api_v1/messages.scss
Original file line number Diff line number Diff line change
@@ -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/
2 changes: 2 additions & 0 deletions app/controllers/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApiController < ActionController::Base
end
6 changes: 6 additions & 0 deletions app/controllers/api_v1/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions app/helpers/api_v1/messages_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApiV1::MessagesHelper
end
4 changes: 4 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
17 changes: 13 additions & 4 deletions app/models/message.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Subscription < ActiveRecord::Base
belongs_to :user
belongs_to :message
end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions app/views/api_v1/messages/index.json
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 1 addition & 0 deletions app/views/comments/destroy.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$('#c-<%= @comment.id %>').remove();
2 changes: 1 addition & 1 deletion app/views/messages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<td>
<% 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 %>
</td>
</tr>
Expand Down
31 changes: 29 additions & 2 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,42 @@

<% end %>

<table class="table table-striped">
<tr>
<td><h2>Who likes?</h2></td>
</tr>
<br>
<hr>
<tr>
<% @message.liked_users.each do |i| %>
<td><%= i.display_name %></td>
</tr>
<% end %>
</table>
<table class="table table-striped">
<tr>
<td><h2>Who subscribed?</h2></td>
</tr>
<br>
<hr>
<tr>
<% @message.subscribed_users.each do |s| %>
<td><%= s.display_name %></td>
</tr>
<% end %>

</table>
<hr>
<% @message.comments.each do |comment| %>

<div id="c-<%= comment.id %>">
<p><%= 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 %>
</p>

</div>
<% end %>

4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20161007210820_create_likes.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions db/migrate/20161007210831_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -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
22 changes: 21 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions lib/tasks/dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/api_v1/messages_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe ApiV1::MessagesController, type: :controller do

end
15 changes: 15 additions & 0 deletions spec/helpers/api_v1/messages_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions spec/models/like_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Like, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Subscription, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
4 changes: 3 additions & 1 deletion spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down