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

Final exam #47

Open
wants to merge 4 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
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.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
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
end
end

protected
Expand Down
11 changes: 4 additions & 7 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ class MessagesController < ApplicationController

def index
# TODO: fix N+1 queries for user and comments
@messages = Message.order("id DESC").page( params[:page] )
@messages = Message.order("id DESC").includes(:user ,:comments).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
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
5 changes: 5 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Like < ActiveRecord::Base

belongs_to :message
belongs_to :user
end
14 changes: 13 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ class Message < ActiveRecord::Base

has_many :comments, :dependent => :destroy

def last_comment_summary
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
5 changes: 5 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Subscription < ActiveRecord::Base

belongs_to :message
belongs_to :user
end
8 changes: 7 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ 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!(@messages) 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 @@
$("#delete-<%= @comment.id %>").remove();
18 changes: 15 additions & 3 deletions app/views/messages/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

<%= simple_format @message.content %>

<h3>Likes</h3>
<p><% @message.likes_by.each do |user| %></p>
<%= user.email%>
<% end %>

<h3>Subscribed</h3>
<% @message.subscriptions_by.each do |user| %>
<p><%= user.email%></p>
<% end %>


<h2>Comments list</h2>

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

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

<div id = "delete-<%= 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
Loading