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

Simple Twitter期中考作業 #198

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'bootstrap', '~> 4.1.1'
gem 'sprockets-rails', '~> 3.1', '>= 3.1.1'
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
gem 'pry', '~> 0.11.3'
gem 'kaminari', '~> 1.1', '>= 1.1.1'



group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
Expand Down
40 changes: 39 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ GEM
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
autoprefixer-rails (8.6.1)
execjs
bcrypt (3.1.11)
bcrypt (3.1.11-java)
bcrypt (3.1.11-x64-mingw32)
bcrypt (3.1.11-x86-mingw32)
bindex (0.5.0)
bootstrap (4.1.1)
autoprefixer-rails (>= 6.0.3)
popper_js (>= 1.12.9, < 2)
sass (>= 3.5.2)
builder (3.2.3)
byebug (10.0.0)
capybara (2.17.0)
Expand All @@ -61,6 +67,7 @@ GEM
mime-types (>= 1.16)
childprocess (0.8.0)
ffi (~> 1.0, >= 1.0.11)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -97,6 +104,22 @@ GEM
jbuilder (2.7.0)
activesupport (>= 4.2.0)
multi_json (>= 1.2)
jquery-rails (4.3.3)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -124,6 +147,14 @@ GEM
nokogiri (1.8.1-x86-mingw32)
mini_portile2 (~> 2.3.0)
orm_adapter (0.5.0)
popper_js (1.12.9)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry (0.11.3-java)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
spoon (~> 0.0)
public_suffix (3.0.1)
puma (3.11.2)
puma (3.11.2-java)
Expand Down Expand Up @@ -199,6 +230,8 @@ GEM
rubyzip (~> 1.0)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
spoon (0.0.6)
ffi
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand Down Expand Up @@ -251,6 +284,7 @@ PLATFORMS
x86-mswin32

DEPENDENCIES
bootstrap (~> 4.1.1)
byebug
capybara (~> 2.13)
carrierwave
Expand All @@ -259,7 +293,10 @@ DEPENDENCIES
factory_bot_rails
ffaker
jbuilder (~> 2.5)
jquery-rails (~> 4.3, >= 4.3.3)
kaminari (~> 1.1, >= 1.1.1)
listen (>= 3.0.5, < 3.2)
pry (~> 0.11.3)
puma (~> 3.7)
rails (~> 5.1.4)
rails-controller-testing
Expand All @@ -269,11 +306,12 @@ DEPENDENCIES
shoulda-matchers (~> 3.1)
spring
spring-watcher-listen (~> 2.0.0)
sprockets-rails (~> 3.1, >= 3.1.1)
sqlite3
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)

BUNDLED WITH
1.16.1
1.16.2
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
//
//= require rails-ujs
//= require turbolinks
//= require jquery3
//= require popper
//= require bootstrap-sprockets
//= require_tree .
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope.
*
*= require_tree .
*= require_self
*/

@import 'bootstrap';


16 changes: 16 additions & 0 deletions app/controllers/admin/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
class Admin::TweetsController < Admin::BaseController
before_action :authenticate_user!
before_action :authenticate_admin

def index
@tweets = Tweet.order('created_at DESC').page(params[:page]).per(15)
render :index
end

def destroy
@tweet = Tweet.find(params[:id])
@tweet.destroy
redirect_to admin_root_path
end


private
def authenticate_admin
unless current_user.role == 'admin'
redirect_back(fallback_location: root_path, alert: "You are Not admin !")
end
end
end
11 changes: 11 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
class Admin::UsersController < Admin::BaseController
before_action :authenticate_user!
before_action :authenticate_admin

def index
@users = User.all.order(tweets_count: :desc).page(params[:page]).per(15)
end

private
def authenticate_admin
unless current_user.role == 'admin'
redirect_back(fallback_location: root_path, alert: "You are Not admin !")
end
end
end
10 changes: 10 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,14 @@ class ApplicationController < ActionController::Base
# 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法
# https://github.com/plataformatec/devise#strong-parameters
# 注意有 sign_up 和 account_update 兩種參數要處理

before_action :authenticate_user!
before_action :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
14 changes: 14 additions & 0 deletions app/controllers/followships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
class FollowshipsController < ApplicationController
def create
@followship = Followship.new(user_id: current_user.id, following_id: params[:following_id])
if @followship.user_id == @followship.following_id
flash[:alert] = "Something went wrong here...you can not follow yourself..."
else
@followship.save
end
redirect_back(fallback_location: root_path)
end

def destroy
@followship = Followship.find_by(user_id: current_user.id, following_id: params[:id])
if @followship
@followship.destroy
else
flash[:alert] = "Something went wrong here...please try again"
end
redirect_back(fallback_location: root_path)
end
end
17 changes: 17 additions & 0 deletions app/controllers/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
class RepliesController < ApplicationController

def index
@tweet = Tweet.find(params[:tweet_id])
@replies = @tweet.replies.order('created_at DESC')
@user = current_user
@reply = Reply.new
end

def create
@tweet = Tweet.find(params[:tweet_id])
@reply = @tweet.replies.build(reply_params)
@reply.user = current_user
if @reply.save
flash[:notice] = "Reply Created !"
else
flash[:alert] = @reply.errors.full_messages
end
redirect_to tweet_replies_path
end

private
def reply_params
params.require(:reply).permit(:comment, :tweet_id, :user_id)
end
end
32 changes: 31 additions & 1 deletion app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
class TweetsController < ApplicationController
before_action :find_tweet, only: [:like, :unlike]

def index
@users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料
@users = User.order(followers_count: :desc).first(10) # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料
@tweet = Tweet.new
@tweets = Tweet.order('created_at DESC').page(params[:page]).per(15)
end

def create
@tweet = current_user.tweets.build(tweet_params)
if @tweet.save!
flash[:notice] = 'Tweet is created successfully !'
else
flash[:alert] = @tweet.errors.messages
end
redirect_to tweets_path
end

def like
@like = Like.new(user_id: current_user.id, tweet_id: @tweet.id)
if @like.save
redirect_to tweets_path
else
flash[:alert] = like.errors.messages
redirect_back(fallback_location: root_path)
end
end

def unlike
@like = Like.find_by(user_id: current_user.id, tweet_id: @tweet.id)
if @like
@like.destroy
redirect_to tweets_path
end
end

private
def tweet_params
params.require(:tweet).permit(:description, :user_id)
end

def find_tweet
@tweet = Tweet.find(params[:id])
end
end
32 changes: 28 additions & 4 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
class UsersController < ApplicationController

before_action :find_user, only: [:edit, :update, :tweets, :followings, :followers, :likes]
def tweets
@tweets = @user.tweets.order("created_at DESC")
end

def edit
if @user == current_user
render :edit
else
redirect_to tweets_user_path(@user)
end
end

def update
if @user.update(user_profile_params)
flash[:notice] = "User Profile Udpate Successfully !"
else
flash[:alert] = @user.error.full_messsages
end
redirect_to tweets_user_path(@user)
end

def followings
@followings # 基於測試規格,必須講定變數名稱
@followings = @user.followings.order(followers_count: :desc)
# 基於測試規格,必須講定變數名稱
end

def followers
@followers # 基於測試規格,必須講定變數名稱
@followers = @user.followers.order(tweets_count: :desc)
# 基於測試規格,必須講定變數名稱
end

def likes
@likes # 基於測試規格,必須講定變數名稱
@likes = @user.liked_tweets.order(likes_count: :desc)
# 基於測試規格,必須講定變數名稱
end

private
def find_user
@user = User.find(params[:id])
end

def user_profile_params
params.require(:user).permit(:introduction, :avatar, :name)
end

end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module ApplicationHelper
def in_admin_panel?
request.fullpath.include?('admin')
end
end
6 changes: 4 additions & 2 deletions app/models/followship.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Followship < ApplicationRecord
validates :following_id, uniqueness: { scope: :user_id }


belongs_to :following, class_name: "User",counter_cache: :followers_count
belongs_to :user, counter_cache: :followings_count
validates_uniqueness_of :following_id, scope: :user_id
end
2 changes: 2 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Like < ApplicationRecord
belongs_to :user, counter_cache: :likes_count
belongs_to :tweet, counter_cache: :likes_count
end
2 changes: 2 additions & 0 deletions app/models/reply.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
class Reply < ApplicationRecord
belongs_to :user, counter_cache: :replies_count
belongs_to :tweet, counter_cache: :replies_count
end
5 changes: 4 additions & 1 deletion app/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Tweet < ApplicationRecord
validates_presence_of :description
validates_length_of :description, maximum: 140

belongs_to :user, counter_cache: :tweets_count
has_many :replies, dependent: :destroy
has_many :likes, dependent: :destroy
end
Loading