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 Tweet by Cynthia #191

Open
wants to merge 100 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
3febaff
set up routes and index page for admin and public user
mycynthia Jul 20, 2018
95b010b
add login process
mycynthia Jul 23, 2018
cda185c
add user name to registration form
mycynthia Jul 23, 2018
267e6f3
add admin log in process and CRUD
mycynthia Jul 23, 2018
6edfdce
setup route and link for user profile page
mycynthia Jul 23, 2018
289530e
add tweet index action and view fo admin
mycynthia Jul 24, 2018
0d4ea94
setup tweet resources for public users
mycynthia Jul 24, 2018
a3677d7
link user and tweet model
mycynthia Jul 24, 2018
20e59a0
user can tweet on tweet's index page
mycynthia Jul 24, 2018
349bd05
install bootstrap-sass and setup scss and gem 'jquery-rails' for boot…
mycynthia Jul 24, 2018
0c4cde8
modify log in and sign up page css
mycynthia Jul 24, 2018
4f10c71
add css for tweet index page
mycynthia Jul 25, 2018
bd4b5f6
add notice and alert css for log in and sign in page
mycynthia Jul 25, 2018
246812b
add limit the number of words (140) in tweet and cannot be blank
mycynthia Jul 25, 2018
3ed66ab
modify tweets
mycynthia Jul 25, 2018
9bcd725
add tweet content on tweet index page
mycynthia Jul 25, 2018
292b597
add style css
mycynthia Jul 25, 2018
b68a539
modify tweets content css in index page
mycynthia Jul 25, 2018
e4c1c08
setup user edit user's info routes
mycynthia Jul 25, 2018
852abd5
setup user edit and update controller
mycynthia Jul 25, 2018
733463a
add user editprofile page
mycynthia Jul 25, 2018
f732ef6
add edit profile page btn css
mycynthia Jul 25, 2018
2540cfd
reset routes for admin
mycynthia Jul 25, 2018
e4f8951
modify tweet index page css
mycynthia Jul 25, 2018
355bfd3
add tweets replies route
mycynthia Jul 26, 2018
edcbe5c
link tweet and reply model
mycynthia Jul 26, 2018
d3f8c3f
modify avatar image css
mycynthia Jul 26, 2018
489a9ed
user can add replies on tweet index page
mycynthia Jul 26, 2018
d13adc6
add user avatar partial and modify user edit page
mycynthia Jul 26, 2018
45f0f80
add tweets user avatar css in index page
mycynthia Jul 26, 2018
572f186
add reply and user relationship
mycynthia Jul 26, 2018
eff1fda
add reply index page
mycynthia Jul 26, 2018
de134fb
setup self-referential relationships within users table through follo…
mycynthia Jul 26, 2018
c2ffc56
setup routes for user index page and follow/unfollow function
mycynthia Jul 26, 2018
a15716e
setup routes for user's profile and tweets pages
mycynthia Jul 26, 2018
8fe8574
modify user avatar partial and replies and tweets and user edit inde…
mycynthia Jul 27, 2018
3c808ac
modify avatar partial page
mycynthia Jul 27, 2018
0a71a4f
add tweeter partial page
mycynthia Jul 27, 2018
16da6cb
modify avatar partial on replies and user edit page and add tweeter p…
mycynthia Jul 27, 2018
ef9b15b
add tweets count on replies index page
mycynthia Jul 27, 2018
ff8bbc3
modify tweets controller
mycynthia Jul 27, 2018
19d9324
add follow button on tweet index page
mycynthia Jul 27, 2018
c2fb784
add create action for followship record
mycynthia Jul 27, 2018
f164576
add unfollow button on tweet index page and destroy action
mycynthia Jul 27, 2018
5d2ba6c
add following include? method and follow partial
mycynthia Jul 27, 2018
932762b
add follow partial on tweet index page
mycynthia Jul 27, 2018
aff8e78
add follow/unfollow button css
mycynthia Jul 27, 2018
a5e7cda
setup user followers and following page routes
mycynthia Jul 28, 2018
3d5ab53
setup user and follower relationship
mycynthia Jul 28, 2018
e8d2550
setup follower and following action
mycynthia Jul 28, 2018
05a1310
add followers and followings page
mycynthia Jul 28, 2018
879842e
add following and follower sort by follow created time
mycynthia Jul 29, 2018
dc5586b
setup routes for like and unlike and user like page
mycynthia Jul 29, 2018
c296fb0
setup relation among user, tweet and like
mycynthia Jul 29, 2018
eab22c4
setup user like or unlike tweet routes
mycynthia Jul 29, 2018
4d72980
add user like and unlike tweet action
mycynthia Jul 29, 2018
46afd70
add user like/unlike tweet button
mycynthia Jul 29, 2018
23d8b21
add is_liked? method to tweet model for checking like/unlike button
mycynthia Jul 29, 2018
b93f46d
add like/unlike button on tweet index page
mycynthia Jul 29, 2018
5770282
add like link partial
mycynthia Jul 30, 2018
e5f27db
modify like partia on tweeter and tweets index page
mycynthia Jul 30, 2018
d0eae51
add reply link on tweeter partial page
mycynthia Jul 30, 2018
00347f0
modify unfollow button css
mycynthia Jul 30, 2018
6fc5016
add user info partial page
mycynthia Jul 30, 2018
c5f4f82
add like page
mycynthia Jul 30, 2018
c9501b6
add like tweet order desc by like created time
mycynthia Jul 30, 2018
b0e9f2c
add tweet,following,follower and like link on user info partial page
mycynthia Jul 30, 2018
470a257
add find user tweets order created time desc and modify another conto…
mycynthia Jul 30, 2018
b97836b
modify user model for tweets
mycynthia Jul 30, 2018
b11c6f7
add user tweet page
mycynthia Jul 30, 2018
512946a
add user info partial and follow/unfollow button on following and fol…
mycynthia Jul 30, 2018
7edf2d9
add tweet's user name link to user tweet page on tweeter partial
mycynthia Jul 30, 2018
6e315fe
add user info partial on reply page
mycynthia Jul 30, 2018
468a30b
add follower count method on user model
mycynthia Jul 31, 2018
c91f63c
add follower count action on user and tweet controller
mycynthia Jul 31, 2018
c88efec
add popular user on tweet index page
mycynthia Jul 31, 2018
af7ffbb
add current user image link on navigation
mycynthia Jul 31, 2018
3584c3e
add admin delete tweet action
mycynthia Jul 31, 2018
7eaa5a4
add delete tweet button on admin index page
mycynthia Jul 31, 2018
bc2579f
transfer admin authenticate to admin base controller
mycynthia Jul 31, 2018
4ee707c
add tweets_count column on users table
mycynthia Jul 31, 2018
286e742
add tweets_count method on user model and add user order by tweets co…
mycynthia Jul 31, 2018
d2f7e00
add all user list on admin user index page
mycynthia Jul 31, 2018
9266cf9
add tweets and users list for admin link
mycynthia Jul 31, 2018
6030063
modify nav and like page css
mycynthia Jul 31, 2018
3c1dd4a
add admin user on seed
mycynthia Jul 31, 2018
9913e23
add pg, update Gemfile.lock, change production db to pg
mycynthia Jul 31, 2018
1142dcc
modify admin user list order tweets count desc
mycynthia Jul 31, 2018
19f1872
modify user and replies relationships
mycynthia Aug 1, 2018
790b2e4
modify user #edit go to edit page will render edit page
mycynthia Aug 1, 2018
f6d5a51
fix tweet #unlike will redirect index
mycynthia Aug 1, 2018
cde68c2
fix tweet #like like first tweet will redirect index
mycynthia Aug 1, 2018
10e9356
modify user validation should validate that :name cannot be empty/falsy
mycynthia Aug 2, 2018
c93d22e
modify admin::tweet go to admin root page if admin user log in can de…
mycynthia Aug 2, 2018
e3072d9
modify tweet #index user log in tweets behaviour can see all tweets…
mycynthia Aug 2, 2018
9aa2182
modify followship #create when user1 wants to follow user2 can not fo…
mycynthia Aug 3, 2018
e18ea5b
modify reply /tweets/:id/replies #index can see all replies instance
mycynthia Aug 3, 2018
416d1be
modify tweet #index user log in user behaviour can show all popular user
mycynthia Aug 3, 2018
a7c6c54
modify admin page
mycynthia Aug 3, 2018
27e6b00
modify admin page
mycynthia Aug 3, 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
11 changes: 9 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ 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
gem 'bootstrap-sass', '~> 3.3.7'
gem 'sass-rails', '~> 5.0'
# For bootstrap
gem 'jquery-rails'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
Expand Down Expand Up @@ -51,6 +53,7 @@ group :development, :test do
gem 'factory_bot_rails'
gem 'shoulda-matchers', '~> 3.1'
gem 'rails-controller-testing'
gem 'sqlite3'
end

group :development do
Expand All @@ -62,5 +65,9 @@ group :development do
gem 'spring-watcher-listen', '~> 2.0.0'
end

group :production do
gem 'pg', '~> 0.20'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
17 changes: 16 additions & 1 deletion 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 (9.0.0)
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,10 @@ 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)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand Down Expand Up @@ -124,6 +133,9 @@ GEM
nokogiri (1.8.1-x86-mingw32)
mini_portile2 (~> 2.3.0)
orm_adapter (0.5.0)
pg (0.21.0)
pg (0.21.0-x64-mingw32)
pg (0.21.0-x86-mingw32)
public_suffix (3.0.1)
puma (3.11.2)
puma (3.11.2-java)
Expand Down Expand Up @@ -251,6 +263,7 @@ PLATFORMS
x86-mswin32

DEPENDENCIES
bootstrap-sass (~> 3.3.7)
byebug
capybara (~> 2.13)
carrierwave
Expand All @@ -259,7 +272,9 @@ DEPENDENCIES
factory_bot_rails
ffaker
jbuilder (~> 2.5)
jquery-rails
listen (>= 3.0.5, < 3.2)
pg (~> 0.20)
puma (~> 3.7)
rails (~> 5.1.4)
rails-controller-testing
Expand All @@ -276,4 +291,4 @@ DEPENDENCIES
web-console (>= 3.3.0)

BUNDLED WITH
1.16.1
1.16.3
2 changes: 2 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require bootstrap-sprockets
//= require rails-ujs
//= require turbolinks
//= require_tree .
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* 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';
@import 'style';
18 changes: 18 additions & 0 deletions app/assets/stylesheets/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//攥寫自己的css.
.tweet-item {
border: 1px solid #ddd;
border-radius: 4px;
padding: 4px;
margin-bottom: 20px;

}

//edit page按鈕
.update-btn {
float:right;
}

.avatar-image {
height: 100px;
width: auto;
}
11 changes: 11 additions & 0 deletions app/controllers/admin/base_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
class Admin::BaseController < ApplicationController
# 後台登入先確認user登入再確認身份為admin,authenticate_admin方法要另外定義
before_action :authenticate_user!
before_action :authenticate_admin

private

def authenticate_admin
unless current_user.admin? #admin?要另外在user model定義
flash[:alert] = "Not allow!"
redirect_to root_path
end
end
end
8 changes: 8 additions & 0 deletions app/controllers/admin/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
class Admin::TweetsController < Admin::BaseController
# 後台登入先確認user登入再確認身份為admin,authenticate_admin方法要另外定義
before_action :authenticate_user!
before_action :authenticate_admin
def index
@tweets = Tweet.all.order(created_at: :desc) #管理者瀏覽所有tweet資料
end

def destroy
@tweets = Tweet.find(params[:id])
@tweets.destroy
redirect_to admin_root_path
flash[:alert] = "tweet was deleted"
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
# 後台登入先確認user登入再確認身份為admin,authenticate_admin方法要另外定義
before_action :authenticate_user!
before_action :authenticate_admin
def index
@users = User.order(tweets_count: :desc)
end
end
18 changes: 18 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?

# 請參考 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

private

def authenticate_admin
unless current_user.admin?
flash[:alert] = "Not allow"
redirect_to root_path
end
end

end
13 changes: 12 additions & 1 deletion app/controllers/followships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
class FollowshipsController < ApplicationController
def create
@followship = current_user.followships.build(following_id: params[:following_id])
if @followship.save
flash[:notice] = "Successfully followed"
redirect_back(fallback_location: root_path)
else
flash[:alert] = @followship.errors.full_messages.to_sentence
redirect_back(fallback_location: root_path)
end
end

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

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

def create
@tweet = Tweet.find(params[:tweet_id])
@reply = @tweet.replies.build(reply_params)
@reply.user_id = current_user.id
@reply.save!
redirect_to tweet_replies_path(@tweet)
end

private

def reply_params
params.require(:reply).permit(:comment)
end

end
32 changes: 30 additions & 2 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
class TweetsController < ApplicationController

# 登入認證
before_action :authenticate_user!
def index
@users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料
@tweets = Tweet.all.order(created_at: :desc)
@tweet = Tweet.new #在index建立tweet容器來建立短文
# 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料
# 必須搭配counter_cache才年通過測試
@users = User.order(followers_count: :desc).limit(10)
# 以下也可以通過自動測試
#@users = User.all.sort_by {|user| user.followers.size}.reverse.first(10)
end

def create
@tweet = current_user.tweets.build(tweet_params)
if @tweet.save
flash[:notice] = "Tweeted successfully"
redirect_to tweets_path
else
flash.now[:alert] = "Tweeted faield"
render :index
end
end

def like
@tweet = Tweet.find(params[:id])
@tweet.likes.create!(user: current_user)
redirect_back(fallback_location: 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)
end

end
30 changes: 25 additions & 5 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
class UsersController < ApplicationController

before_action :set_user, only: [:edit, :update, :followings, :followers, :likes, :tweets]
def tweets
@user = User.find(params[:id])
@tweets = @user.tweets.includes(:user).order("tweets.created_at desc")
end

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

def update
@user.update(user_params)
redirect_to edit_user_path(@user)
end

def followings
@followings # 基於測試規格,必須講定變數名稱
# For ordering on the attribute of an associated model you have to include it:
@followings = @user.followings.includes(:followships).order("followships.created_at desc") # 基於測試規格,必須講定變數名稱
end

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

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

private

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

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

end
10 changes: 10 additions & 0 deletions app/models/followship.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
class Followship < ApplicationRecord
# 限制使用者只能追蹤另一個使用者一次
validates :following_id, uniqueness: { scope: :user_id }
# 限制是否追蹤自己
validate :check_self_following
belongs_to :user
belongs_to :following, class_name:"User" ,counter_cache: :followers_count

def check_self_following
if user_id == following_id
errors[:following_id] << "Can's follow yourself"
end
end
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
belongs_to :tweet
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
belongs_to :tweet
end
14 changes: 14 additions & 0 deletions app/models/tweet.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
class Tweet < ApplicationRecord
validates_length_of :description, maximum: 140
# 建立user跟tweet的關聯,一個tweet只會有一個user
belongs_to :user, counter_cache: true
# tweet限制字數及空白
validates :user_id, presence: true
validates :description, presence: true, length: {maximum: 140}
# tweet有很多回覆,當tweet被刪除時,順便刪除回覆
has_many :replies, dependent: :destroy

# 使用者可以喜歡很多評論
has_many :likes, dependent: :destroy
has_many :liked_users, through: :likes, source: :user

def is_liked?(user)
self.liked_users.include?(user)
end
end
Loading