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 #186

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
8da5775
setup tweet routes for public /admin user
ZionYu Jul 11, 2018
59e1e96
using devise, add authenticate_user!, add user name to registration form
ZionYu Jul 11, 2018
0c7bc6c
setup routes for user ,add tweet index page and user edit page
ZionYu Jul 11, 2018
8ebd938
install bootstrap-sass and setup scss
ZionYu Jul 11, 2018
01657b4
install gem 'jquery-rails' for bootstrap js
ZionYu Jul 11, 2018
dc9c4be
setup all model's relation
ZionYu Jul 14, 2018
dc6d8e9
styled layouts/application page and fixed edit link
ZionYu Jul 18, 2018
87af560
add colume name when log in
ZionYu Jul 18, 2018
f70ee59
add index&create action and tweet index page
ZionYu Jul 18, 2018
e9a3172
add tweets, edit, update action and edit page for users
ZionYu Jul 18, 2018
b093dd2
setup admin routes using namespace and add admin index page
ZionYu Jul 18, 2018
04e8b72
setup relation between tweet and user and add users/tweets page
ZionYu Jul 18, 2018
b8c7326
create a rake "fake_tweet"
ZionYu Jul 18, 2018
ef7029f
steup relation with tweet, user and reply, add index ,create atcion a…
ZionYu Jul 18, 2018
45992ca
add gem 'kaminari'
ZionYu Jul 22, 2018
0cfea9a
according created time to sort tweets
ZionYu Jul 22, 2018
4fba264
setup follow and like routes
ZionYu Jul 22, 2018
6cf3b7d
add #create # destroy action on controller followships
ZionYu Jul 22, 2018
27f1d34
add validation of user name and following?(user)
ZionYu Jul 22, 2018
1206f6f
add user # followings action and using partial template for follow fu…
ZionYu Jul 22, 2018
31cfd08
add index, destroy action, and admin index page for admin
ZionYu Jul 23, 2018
05d6738
add private method authenticate_admin
ZionYu Jul 23, 2018
95d0b2c
add like/unlike action in controller tweets
ZionYu Jul 24, 2018
11b9e77
fixed routes for tweets
ZionYu Jul 24, 2018
a932a67
add column tweets_count to users
ZionYu Jul 24, 2018
c7dd2c4
fixed edit action
ZionYu Jul 24, 2018
3b9d359
add control flow for users can't follow themselves
ZionYu Jul 24, 2018
5931ce6
sorting followings by followships created time
ZionYu Jul 24, 2018
9216f7c
replace follow partial and show user's followings
ZionYu Jul 24, 2018
dd679c3
add followers association
ZionYu Jul 24, 2018
5b36a58
setup follower routes
ZionYu Jul 24, 2018
833d3a6
add follower action in users controller
ZionYu Jul 24, 2018
da2aeab
add counter_cache in model followship
ZionYu Jul 24, 2018
e2cf1d8
create followers page
ZionYu Jul 24, 2018
bba99dd
add link 'follower' to following page
ZionYu Jul 24, 2018
8bfcaed
add like association and counter_cache in model like
ZionYu Jul 24, 2018
6c9c7c6
add likes action in users controller
ZionYu Jul 25, 2018
37c0088
create like/unlike links
ZionYu Jul 25, 2018
aba1c78
create like page show user's liked_tweets
ZionYu Jul 25, 2018
7e7a142
add validates of comment in reply model
ZionYu Jul 25, 2018
6bfc47d
add is_liked? method
ZionYu Jul 25, 2018
ff352ab
add users instance in index action
ZionYu Jul 26, 2018
5254c1e
add Popular users on index page
ZionYu Jul 26, 2018
8972a59
create patial '_count.html.erb '
ZionYu Jul 26, 2018
7f502bb
add all page user_name link and using partial _count
ZionYu Jul 26, 2018
1e25e68
fixed avatar img size
ZionYu Jul 26, 2018
4e433cd
styled like/unlike link
ZionYu Jul 26, 2018
306008b
add user instance to replies controller
ZionYu Jul 26, 2018
340d9bc
add index action to controller tweets for admin
ZionYu Jul 26, 2018
d883382
add index page for admin
ZionYu Jul 26, 2018
1235b02
add index action to users controller for admin
ZionYu Jul 26, 2018
ab3e4fc
create users index page for admin
ZionYu Jul 26, 2018
d704f64
create seed amdin data
ZionYu Jul 26, 2018
3eb421a
setting production/test environment
ZionYu Jul 26, 2018
5b05230
using postgresql be database
ZionYu Jul 26, 2018
94ba273
change avatar_uploader in different environment
ZionYu Jul 26, 2018
5970457
install gem 'pg' and 'sqlite3'
ZionYu Jul 27, 2018
bd87f20
reset avatar_uploader
ZionYu Jul 27, 2018
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
13 changes: 11 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ gem 'ffaker'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
Expand All @@ -37,9 +37,16 @@ gem 'jbuilder', '~> 2.5'
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

gem 'bootstrap-sass', '~>3.3.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'jquery-rails'

gem 'kaminari'

group :production do
gem 'pg'
end

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
Expand All @@ -51,6 +58,8 @@ group :development, :test do
gem 'factory_bot_rails'
gem 'shoulda-matchers', '~> 3.1'
gem 'rails-controller-testing'
gem 'sqlite3'

end

group :development do
Expand Down
28 changes: 28 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,16 @@ GEM
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
arel (8.0.0)
autoprefixer-rails (8.6.5)
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-sass (3.3.7)
autoprefixer-rails (>= 5.2.1)
sass (>= 3.3.4)
builder (3.2.3)
byebug (10.0.0)
capybara (2.17.0)
Expand Down Expand Up @@ -97,6 +102,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 +145,9 @@ GEM
nokogiri (1.8.1-x86-mingw32)
mini_portile2 (~> 2.3.0)
orm_adapter (0.5.0)
pg (1.0.0)
pg (1.0.0-x64-mingw32)
pg (1.0.0-x86-mingw32)
public_suffix (3.0.1)
puma (3.11.2)
puma (3.11.2-java)
Expand Down Expand Up @@ -251,6 +275,7 @@ PLATFORMS
x86-mswin32

DEPENDENCIES
bootstrap-sass (~> 3.3.7)
byebug
capybara (~> 2.13)
carrierwave
Expand All @@ -259,7 +284,10 @@ DEPENDENCIES
factory_bot_rails
ffaker
jbuilder (~> 2.5)
jquery-rails
kaminari
listen (>= 3.0.5, < 3.2)
pg
puma (~> 3.7)
rails (~> 5.1.4)
rails-controller-testing
Expand Down
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@
//= require rails-ujs
//= require turbolinks
//= require_tree .
//= require jquery
//= require bootstrap-sprockets
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
* 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-sprockets';
@import 'bootstrap';
55 changes: 55 additions & 0 deletions app/assets/stylesheets/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
html, body {
width: 100%;
height: 100%;
}

body {
padding: 0px;
margin: 0px;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #fff;
}

* {
box-sizing: border-box;
}

h1, h2, h3, h4, h5, h6, p {
margin: 0px;
padding: 0px;
}

ul, ol {
list-style: none;
margin: 0px;
padding: 0px;
}

a, a:hover, a:focus {
margin: 0px;
padding: 0px;
text-decoration: none;
}

img {
width: 100%;
height: auto;
display: block;
}

.container {
width: 85%;
max-width: 1170px;
margin: 0 auto;
position: relative;
}

.col-left{
float: left
}

.col-right{
float: right
}
9 changes: 9 additions & 0 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
class Admin::BaseController < ApplicationController
before_action :authenticate_admin
private
def authenticate_admin
unless current_user.admin?
flash[:alert] = "Not allow"
redirect_to root_path
end

end

end
13 changes: 13 additions & 0 deletions app/controllers/admin/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
class Admin::TweetsController < Admin::BaseController
def index
@tweets = Tweet.all.order(created_at: :desc)
end

def destroy

@tweet = Tweet.find(params[:id])

@tweet.destroy
redirect_to admin_root_path
flash[:alert] = "tweet has destroyed"

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

end
4 changes: 4 additions & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Admin::UsersController < Admin::BaseController

def index
@users = User.all.order(tweets_count: :desc)
end


end
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!

# 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法
# https://github.com/plataformatec/devise#strong-parameters
# 注意有 sign_up 和 account_update 兩種參數要處理

protected

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end
end
18 changes: 18 additions & 0 deletions app/controllers/followships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
class FollowshipsController < ApplicationController

def create
@user = User.find(params[:following_id])
if @user == current_user
flash[:alert] = "can't follow yourself"
else
@followship = current_user.followships.build(following_id: params[:following_id])
if @followship.save
flash[:notice] = "followed sucessfully"
redirect_back(fallback_location: root_path)
else
flash[:alert] = @followship.errors.full_messages.to_sentence
redirect_back(fallback_location: root_path)
end
end
end

def destroy
@followship = current_user.followships.where(following_id: params[:id]).first
@followship.destroy
flash[:alert] = "Followship destroyed"
redirect_back(fallback_location: root_path)
end
end
23 changes: 23 additions & 0 deletions app/controllers/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
class RepliesController < ApplicationController

def index

@tweet = Tweet.find(params[:tweet_id])
@replies = @tweet.replies
@reply = Reply.new
@user = @tweet.user

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 successfully"
redirect_to tweet_replies_path
else
flash[:alert] = "fail to reply the tweet"
render :index
end
end

private
def reply_params
params.require(:reply).permit(:comment, :user_id, :tweet_id)
end

end
27 changes: 26 additions & 1 deletion app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
class TweetsController < ApplicationController

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

def create
@user = current_user
@tweet = @user.tweets.build(tweet_params)

if @tweet.save
flash[:notice] = 'create a tweet successfully'
redirect_to tweets_path
else
flash[:alert] = 'failed to create a tweet'
render :index
end

end

def like
@tweet = Tweet.find(params[:id])
@tweet.likes.create!(user: current_user)
redirect_to tweets_path
end

def unlike
@tweet = Tweet.find(params[:id])
likes = Like.where(tweet: @tweet, user: current_user)
likes.destroy_all
redirect_to tweets_path
end

private

def tweet_params
params.require(:tweet).permit(:description, :user_id)
end
end
36 changes: 33 additions & 3 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,54 @@
class UsersController < ApplicationController

def tweets
@user = User.find(params[:id])
@tweet = Tweet.new
@tweets = @user.tweets.all
end



def edit
@user = User.find(params[:id])

unless @user == current_user
redirect_to tweets_user_path(@user)
end

end

def update
@user = User.find(params[:id])
@user.update(user_params)

redirect_to tweets_user_path(@user)
end

def followings
@followings # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@followings = @user.followings.page(params[:page]).per(6).order('followships.created_at DESC') # 基於測試規格,必須講定變數名稱
end

def followers
@followers # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@followers = @user.followers.page(params[:page]).per(6).order('followships.created_at DESC') # 基於測試規格,必須講定變數名稱
end

def likes
@likes # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@likes = @user.liked_tweets.page(params[:page]).per(6).order('likes.created_at DESC') # 基於測試規格,必須講定變數名稱
@tweets = @user.tweets


end

def show

end

private

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