Skip to content
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: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc

group :development, :test do
gem "rspec-rails"
gem 'guard-rspec', require: false

# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
Expand Down
44 changes: 43 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ GEM
builder (3.2.2)
byebug (5.0.0)
columnize (= 0.9.0)
coderay (1.1.1)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
Expand All @@ -64,8 +65,24 @@ GEM
execjs (2.5.2)
faker (1.4.3)
i18n (~> 0.5)
ffi (1.9.14)
formatador (0.2.5)
globalid (0.3.5)
activesupport (>= 4.1.0)
guard (2.14.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.7.2)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
i18n (0.7.0)
jbuilder (2.3.0)
activesupport (>= 3.0.0, < 5)
Expand All @@ -78,17 +95,31 @@ GEM
kaminari (0.16.3)
actionpack (>= 3.0.0)
activesupport (>= 3.0.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
loofah (2.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.0.10)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1)
mini_portile (0.6.2)
minitest (5.7.0)
multi_json (1.11.1)
nenv (0.3.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
notiffany (0.1.0)
nenv (~> 0.1)
shellany (~> 0.0)
orm_adapter (0.5.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4)
rack-test (0.6.3)
rack (>= 1.0)
Expand Down Expand Up @@ -117,9 +148,16 @@ GEM
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rb-fsevent (0.9.7)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rdoc (4.2.0)
responders (2.1.0)
railties (>= 4.2.0, < 5)
rspec (3.3.0)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
Expand All @@ -137,6 +175,7 @@ GEM
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
ruby_dep (1.3.1)
sass (3.4.15)
sass-rails (5.0.3)
railties (>= 4.0.0, < 5.0)
Expand All @@ -147,6 +186,8 @@ GEM
sdoc (0.4.1)
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
shellany (0.0.1)
slop (3.6.0)
spring (1.3.6)
sprockets (3.2.0)
rack (~> 1.0)
Expand Down Expand Up @@ -181,6 +222,7 @@ DEPENDENCIES
coffee-rails (~> 4.1.0)
devise
faker
guard-rspec
jbuilder (~> 2.0)
jquery-rails
kaminari
Expand All @@ -195,4 +237,4 @@ DEPENDENCIES
web-console (~> 2.0)

BUNDLED WITH
1.10.5
1.12.3
70 changes: 70 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
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
2 changes: 2 additions & 0 deletions app/controllers/api_v1/api_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ApiController < ActionController::Base
end
7 changes: 7 additions & 0 deletions app/controllers/api_v1/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ApiV1::MessagesController < ApiController

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
47 changes: 41 additions & 6 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
class MessagesController < ApplicationController

before_action :set_message ,:only =>[:show,:subscribe,:unsubscribe,:like,:dislike]
before_action :authenticate_user!, :except => [:index, :show]

def index
# TODO: fix N+1 queries for user and comments
@messages = Message.order("id DESC").page( params[:page] )
@messages = Message.includes(:comments,:user).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

def show
@message = Message.find( params[:id] )
@comment = Comment.new
end

Expand Down Expand Up @@ -59,7 +58,43 @@ def destroy
redirect_to root_path
end

def subscribe
@subscription = @message.find_my_subscription(current_user)
unless @subscription
@subscription = Subscription.create!( :message => @message, :user => current_user )
end

redirect_to :back
end

def unsubscribe
@subscription = @message.find_my_subscription(current_user)
@subscription.destroy

redirect_to :back
end

def like
@like = @message.find_my_like(current_user)
unless @like
@like = Like.create!( :message => @message, :user => current_user )
end

redirect_to :back
end

def dislike
@like = @message.find_my_like(current_user)
@like.destroy

redirect_to :back
end


protected
def set_message
@message = Message.find( params[:id] )
end

def message_params
params.require(:message).permit(:title, :content, :category_name)
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 :user
belongs_to :message
end
19 changes: 19 additions & 0 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,27 @@ class Message < ActiveRecord::Base

has_many :comments, :dependent => :destroy

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


def last_comment_summary
self.comments.last.try(:content).try(:truncate, 20)
end

scope :pending, -> { where( :status => "pending" ) }
scope :completed, -> {where( :status => "completed" )}

def self.within_days(params)
where( ["created_at >= ?", Time.now - params.days ] )
end

def find_my_subscription(user)
self.subscriptions.where( :user => user ).first
end
def find_my_like(user)
self.likes.where( :user => user ).first
end
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
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ class User < ActiveRecord::Base

has_many :messages
has_many :comments
has_many :subscriptions, :dependent => :destroy
has_many :subscribed_messages, :through => :subscriptions, :source => :message
has_many :likes, :dependent => :destroy
has_many :like_messages, :through => :likes, :source => :message

def display_name
self.email.split("@").first
end

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

def words_count
Expand Down
13 changes: 13 additions & 0 deletions app/views/api_v1/messages/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

json.message @messages do |m|
json.id m.id
json.status m.status
json.category_name m.category_name
json.title m.title
json.content m.content
json.created_at m.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();
19 changes: 19 additions & 0 deletions app/views/messages/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,25 @@
<%= link_to "Delete", message_path(msg), :method => :delete, :data => { :confirm => "Are u sure?"} %>
<% end %>
</td>
<td>
<% subscription = msg.find_my_subscription(current_user) %>

<% if subscription %>
<%= link_to "Unsubscribe", unsubscribe_message_path(msg), :class => "btn btn-primary", :method => :post %>
<% else %>
<%= link_to "Subscribe", subscribe_message_path(msg), :class => "btn btn-primary", :method => :post %>
<% end %>
</td>
<td>
<% like = msg.find_my_like(current_user) %>

<% if like %>
<%= link_to "dislike", dislike_message_path(msg), :class => "btn btn-primary", :method => :post %>
<% else %>
<%= link_to "like", like_message_path(msg), :class => "btn btn-primary", :method => :post %>
<% end %>
</td>

</tr>
<% end %>
</table>
Expand Down
Loading