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

birdbrother final exam #60

Open
wants to merge 2 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
18 changes: 18 additions & 0 deletions app/controllers/api/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Api::MessagesController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :enable_cors
def index
@messages = Message.all
end


private
def enable_cors
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE'
response.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, *'
response.headers['Access-Control-Request-Method'] = 'GET, POST, PUT, DELETE'
response.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

6 changes: 4 additions & 2 deletions app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ def create

def destroy
@comment = current_user.comments.find( params[:id] )
respond_to do |format|
format.js
end
@comment.destroy

redirect_to :back
#redirect_to :back
end

protected
Expand Down
13 changes: 8 additions & 5 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ 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.where( :status => "pending" )
@messages = @messages.messages_pending
elsif params[:status] == "completed"
# TODO: @messages = @messages.completed
@messages = @messages.where( :status => "completed" )
#@messages = @messages.where( :status => "completed" )
@messages = @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.where( ["created_at >= ?", Time.now - params[:days].to_i.days ] )
@messages = @messages.messages_within_days
end
end

Expand Down Expand Up @@ -62,7 +65,7 @@ def destroy
protected

def message_params
params.require(:message).permit(:title, :content, :category_name)
params.require(:message).permit(:title, :content, :category_name, :status)
end

end
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 Api::V1::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
5 changes: 4 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ class Message < ActiveRecord::Base
belongs_to :user

has_many :comments, :dependent => :destroy

has_many :likes
has_many :liked_users, :through => :likes, :source => :user
has_many :subscriptions
has_many :subscribed_users, :through => :subscriptions, :source => :user
def last_comment_summary
self.comments.last.try(:content).try(:truncate, 20)
end
Expand Down
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
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ class User < ActiveRecord::Base

has_many :messages
has_many :comments

has_many :likes
has_many :liked_messages, :through => :likes, :source => :message
has_many :subscriptions
has_many :subscribed_messages, :through => :subscriptions, :source => :message
def display_name
self.email.split("@").first
end

def posts_count
# TODO: 請完成我
self.comments.count + self.messages.count
end

def words_count
Expand Down
4 changes: 4 additions & 0 deletions app/views/api/messages/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
json.data(@messages) do |m|
json.( m, :id, :status, :category_name, :title, :content, :created_at)
end

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 @@
$("#comment-<%= @comment.id%>").remove();
21 changes: 14 additions & 7 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<h2><%= @message.title %></h2>
subscribed_users:<% @message.subscribed_users.each do |u| %>
<%= u.email %>
<% end %>
<br>
liked_users:<% @message.liked_users.each do |u| %>
<%= u.email %>
<% end %>

<%= simple_format @message.content %>

Expand All @@ -19,13 +26,13 @@

<% @message.comments.each do |comment| %>

<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?"} %>
<% end %>
</p>
<div id="comment-<%= comment.id %>">
<%= 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, :remote => true, :data => { :confirm => "Are u sure?"} %>
<% end %>
</div>
<% end %>

6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Rails.application.routes.draw do
devise_for :users

namespace :api, :defaults => { :format => :json } do
resource :v1 do
resources :messages
end
end
resources :messages do
resources :comments
end
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20161112141754_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSubscriptions < ActiveRecord::Migration
def change
create_table :subscriptions do |t|
t.integer :user_id
t.integer :message_id
t.timestamps null: false
end
end
end
9 changes: 9 additions & 0 deletions db/migrate/20161112141801_create_likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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
16 changes: 15 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: 20161112141801) do

create_table "comments", force: :cascade do |t|
t.text "content"
Expand All @@ -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"
Expand All @@ -36,6 +43,13 @@
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

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand Down
18 changes: 17 additions & 1 deletion lib/tasks/dev.rake
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,34 @@ namespace :dev do
users << User.create!( :email => Faker::Internet.email,
:password => "12345678")
end

messages = []
20.times do
m = Message.create!( :title => Faker::Lorem.word,
:status => ["pending", "completed"].sample,
:content => Faker::Lorem.paragraph,
:user => users.sample,
:created_at => Time.now - rand(30).days )
messages << m
5.times do
m.comments.create!( :content => Faker::Lorem.paragraph,
:user => users.sample )
end
end

rand_messages = messages.sample(2)
rand_users = users.sample(2)
Like.create!(:user_id => rand_users.first.id.to_i,
:message_id => rand_messages.first.id.to_i)
Like.create!(:user_id => rand_users.second.id.to_i,
:message_id => rand_messages.second.id.to_i)

rand_messages2 = messages.sample(2)
rand_users2 = users.sample(2)
Subscription.create!(:user_id => rand_users2.first.id.to_i,
:message_id => rand_messages2.first.id.to_i)
Subscription.create!(:user_id => rand_users2.second.id.to_i,
:message_id => rand_messages2.second.id.to_i)

end

end
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 Api::V1::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 Api::V1::MessagesHelper. For example:
#
# describe Api::V1::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 Api::V1::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 => "1", :content => "2 2" )
c = Comment.create!( :user => @user, :content => "3 3 3")
end

it "加總該使用者的所有 Mesasge 和 Comment 的總字數" do
# TODO: 測試 words_count 方法

expect( @user.words_count ).to eq(6)
end
end

Expand Down