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

Web Bootcamp 期末考 #39

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ group :development, :test do
gem 'byebug'

# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'

# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
Expand Down
151 changes: 73 additions & 78 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -36,59 +36,58 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.0)
bcrypt (3.1.10)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
arel (6.0.3)
bcrypt (3.1.11)
builder (3.2.2)
byebug (5.0.0)
columnize (= 0.9.0)
coffee-rails (4.1.0)
byebug (9.0.5)
coffee-rails (4.1.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
railties (>= 4.0.0, < 5.1.x)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.9.1.1)
columnize (0.9.0)
debug_inspector (0.0.2)
devise (3.5.1)
coffee-script-source (1.10.0)
concurrent-ruby (1.0.2)
devise (4.2.0)
bcrypt (~> 3.0)
orm_adapter (~> 0.1)
railties (>= 3.2.6, < 5)
railties (>= 4.1.0, < 5.1)
responders
thread_safe (~> 0.1)
warden (~> 1.2.3)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.5.2)
faker (1.4.3)
execjs (2.7.0)
faker (1.6.5)
i18n (~> 0.5)
globalid (0.3.5)
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
jbuilder (2.3.0)
activesupport (>= 3.0.0, < 5)
jbuilder (2.6.0)
activesupport (>= 3.0.0, < 5.1)
multi_json (~> 1.2)
jquery-rails (4.0.4)
rails-dom-testing (~> 1.0)
jquery-rails (4.1.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.3)
kaminari (0.16.3)
kaminari (0.17.0)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
loofah (2.0.2)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
mime-types (2.6.1)
mini_portile (0.6.2)
minitest (5.7.0)
multi_json (1.11.1)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
mail (2.6.4)
mime-types (>= 1.16, < 4)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.9.0)
multi_json (1.12.1)
nokogiri (1.6.8)
mini_portile2 (~> 2.1.0)
pkg-config (~> 1.1.7)
orm_adapter (0.5.0)
pkg-config (1.1.7)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand All @@ -105,73 +104,70 @@ GEM
sprockets-rails
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.6)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (4.2.2)
actionpack (= 4.2.2)
activesupport (= 4.2.2)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rdoc (4.2.0)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
rake (11.2.2)
rdoc (4.2.2)
json (~> 1.4)
responders (2.2.0)
railties (>= 4.2.0, < 5.1)
rspec-core (3.5.1)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-rails (3.3.3)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
sass (3.4.15)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
sass (3.4.22)
sass-rails (5.0.5)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (~> 1.1)
tilt (>= 1.1, < 3)
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
sprockets-rails (2.3.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
sqlite3 (1.3.10)
spring (1.7.2)
sprockets (3.7.0)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.1.1)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.11)
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
turbolinks (2.5.3)
coffee-rails
tilt (2.0.5)
turbolinks (5.0.0)
turbolinks-source (~> 5)
turbolinks-source (5.0.0)
tzinfo (1.2.2)
thread_safe (~> 0.1)
uglifier (2.7.1)
execjs (>= 0.3.0)
json (>= 1.8.0)
warden (1.2.3)
uglifier (3.0.0)
execjs (>= 0.3.0, < 3)
warden (1.2.6)
rack (>= 1.0)
web-console (2.1.3)
activemodel (>= 4.0)
binding_of_caller (>= 0.7.2)
railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0)

PLATFORMS
ruby
Expand All @@ -192,7 +188,6 @@ DEPENDENCIES
sqlite3
turbolinks
uglifier (>= 1.3.0)
web-console (~> 2.0)

BUNDLED WITH
1.10.5
1.12.4
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
5 changes: 5 additions & 0 deletions app/controllers/api_v1/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ApiV1::MessagesController < ApiController
def index
@message = Message.all
end
end
5 changes: 4 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ 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 # destroy.js.erb
end
end

protected
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(: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

Expand Down
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 :message
belongs_to :user
end
10 changes: 10 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ class Message < ActiveRecord::Base

has_many :comments, :dependent => :destroy

has_many :likes, :dependent => :destroy
has_many :likes_by, :through => :likes, :source => :user, :dependent => :destroy

has_many :subscriptions, :dependent => :destroy
has_many :subscriptions_by, :through => :likes, :source => :user, :dependent => :destroy

scope :pending, -> { where status: "pending" }
scope :completed, -> {where status: "completed" }
scope :within_days, -> (time){ where "created_at >= ?", Time.now - time.days }

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 :message
belongs_to :user
end
9 changes: 8 additions & 1 deletion 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 :likes, :dependent => :destroy
has_many :like_messages, :through => :likes, :source => :message, :dependent => :destroy

has_many :subscriptions, :dependent => :destroy
has_many :subscriptions_by, :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
10 changes: 10 additions & 0 deletions app/views/api_v1/messages/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
json.data do
json.array!(@message) do |message|
json.id message.id
json.status message.status
json.category_name message.category_name
json.title message.title
json.content message.content
json.created_at message.created_at
end
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();
14 changes: 12 additions & 2 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

<%= simple_format @message.content %>

<h4>Likes</h4>
<% @message.likes_by.each do |user| %>
<p><%= user.email.split("@").first%></p>
<% end %>
<h4>Subscribed</h4>
<% @message.subscriptions_by.each do |user| %>
<p><%= user.email.split("@").first%></p>
<% end %>

<h2>Comments list</h2>

<%= form_for [@message, @comment] do |f| %>
Expand All @@ -18,14 +27,15 @@
<% end %>

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

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

<% end %>

2 changes: 1 addition & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require File.expand_path('../boot', __FILE__)

require 'rails/all'

require 'yaml'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
resources :comments
end

scope :path => '/api/v1/', :module => "api_v1", :as => 'v1', :defaults => {:format => :json} do
resources :messages
end

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20160722082411_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, index: :true
t.integer :message_id, index: :true

t.timestamps null: false
end
end
end
Loading