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

cj-simple-twitter #193

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
7df0422
add tweet index action and view for public users
Cing-Jhih Jul 20, 2018
f1cd638
add tweet view for admin
Cing-Jhih Jul 20, 2018
c7b0c54
setup all routes for simple-twitter
Cing-Jhih Jul 20, 2018
2753396
add login process
Cing-Jhih Jul 20, 2018
a49110b
set up admin authentication
Cing-Jhih Jul 20, 2018
8dd2efd
add column to registration form
Cing-Jhih Jul 20, 2018
ec316c4
add restaurant index view for admin
Cing-Jhih Jul 20, 2018
6e73e07
generate fake tweets using FFaker gem
Cing-Jhih Jul 20, 2018
a8dfe23
add tweet destroy action for admin
Cing-Jhih Jul 20, 2018
033eead
install bootstrap-sass, setup scss,and install gem jquery-rails for b…
Cing-Jhih Jul 20, 2018
7aa290e
Merge branch 'feature/add_column_to_registration_form'
Cing-Jhih Jul 21, 2018
0d374ca
setup admin user (seed)
Cing-Jhih Jul 21, 2018
d073752
add_registration_column(fix)
Cing-Jhih Jul 21, 2018
cbf4154
add user edit and update action and views for admin
Cing-Jhih Jul 21, 2018
0a023c7
generate link between user, tweet, and reply model
Cing-Jhih Jul 21, 2018
04a3cd6
fix authenticate_admin
Cing-Jhih Jul 25, 2018
a922736
fix link between user, reply, and tweet
Cing-Jhih Jul 25, 2018
60e4023
user can create tweets on index page
Cing-Jhih Jul 25, 2018
aa7cbe4
user can edit profile and see other's profile page
Cing-Jhih Jul 25, 2018
e2968a2
fake data(reply) & user can reply others tweet_1
Cing-Jhih Jul 28, 2018
d7fea53
styled navbar
Cing-Jhih Jul 28, 2018
5574487
styled user edit page-1
Cing-Jhih Jul 28, 2018
c9bfa73
styled tweets index page_2
Cing-Jhih Jul 29, 2018
9c4d79e
show user profile page
Cing-Jhih Jul 29, 2018
3ef5b7a
setup relation among user, tweet and like
Cing-Jhih Jul 29, 2018
d9be7cc
failure
Cing-Jhih Jul 29, 2018
13b576d
like/unlike button and controller action
Cing-Jhih Jul 29, 2018
8ec6409
show like count
Cing-Jhih Jul 29, 2018
fb3fcd6
setup self-referential relationships within users table through follo…
Cing-Jhih Jul 30, 2018
d07895d
fix the path and add users/tweets.html.erb
Cing-Jhih Jul 30, 2018
abfd13f
fix the path
Cing-Jhih Jul 30, 2018
cc3fdcf
add user tweet page with follow button and add create action for foll…
Cing-Jhih Jul 30, 2018
7b99375
add unfollow button on user index page and destroy action
Cing-Jhih Jul 30, 2018
422b4bf
show popular tweeter on index page & styled index page
Cing-Jhih Jul 30, 2018
97d60ad
admin can see all tweets and replies
Cing-Jhih Jul 30, 2018
f11453a
admin::tweets#index, users#index
Cing-Jhih Jul 30, 2018
89d802e
add pg, update Gemfile.lock, change production db to pg
Cing-Jhih Jul 30, 2018
90fc3e0
fix like/unlike button
Cing-Jhih Jul 30, 2018
80b7f08
fix follow/unfollow button
Cing-Jhih Jul 30, 2018
61e7be1
styled replies#index
Cing-Jhih Jul 30, 2018
aab6ce3
user can add replies on replies index page
Cing-Jhih Jul 30, 2018
25d336b
fix the error
Cing-Jhih Jul 30, 2018
4b3e802
admin can see tweets_count on admin panel
Cing-Jhih Jul 31, 2018
5b4089b
user can see tweets_count on replies#index and users#tweets
Cing-Jhih Jul 31, 2018
359dc11
admin::can see tweets replies
Cing-Jhih Jul 31, 2018
7c483a9
add likes_count
Cing-Jhih Jul 31, 2018
9a2d820
add current_user avatar on nav
Cing-Jhih Jul 31, 2018
f558479
add likes_count to unlike button on users#tweets
Cing-Jhih Jul 31, 2018
a9fcab9
fix rspec failure
Cing-Jhih Aug 1, 2018
47e550f
fix rspec failure
Cing-Jhih Aug 1, 2018
789c89d
fix like_count
Cing-Jhih Aug 1, 2018
fac432e
fix
Cing-Jhih Aug 1, 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: 10 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ gem 'carrierwave'

gem 'ffaker'

gem 'bootstrap-sass', '~> 3.3.7'

# 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 @@ -41,6 +43,12 @@ gem 'jbuilder', '~> 2.5'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

gem 'jquery-rails'

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

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 All @@ -51,6 +59,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 Down
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.2
3 changes: 3 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@
//= 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,32 @@
* 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";

.container-fluid{
background-color: #B0C4DE;
}

.navbar-header{
font-size: 50px;
color: #696969;
}

.navbar-body{
font-size: 20px;
color: #696969;
}

textarea.text-area {
font-size: 24px;
height: 128px;
}

.tweet-item {
border: 2px solid lightgray;
padding: 10px;
margin-bottom: 15px;
}
11 changes: 11 additions & 0 deletions app/assets/stylesheets/users.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
// Place all the styles related to the users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

.well{
width: 100%;
height: 150px;
padding: 12px 20px;
box-sizing: border-box;
border: 2px solid #ccc;
border-radius: 4px;
background-color: #f8f8f8;
resize: none;
}
6 changes: 6 additions & 0 deletions app/controllers/admin/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
class Admin::TweetsController < Admin::BaseController
def index
@tweets = Tweet.all.order(created_at: :desc)
@replies = Reply.all
end

def destroy
@tweet = Tweet.find(params[:id])
@tweet.destroy
redirect_to admin_tweets_path
flash[:alert] = "tweet was deleted"
end
end
1 change: 1 addition & 0 deletions app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Admin::UsersController < Admin::BaseController
def index
@users = User.all.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 兩種參數要處理

private

def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
devise_parameter_sanitizer.permit(:account_update, keys: [:name])
end

def authenticate_admin
puts "authenticate_admin"
unless current_user.admin?
puts "==================================================="
puts "???????????????????????????????????????????/"
flash[:alert] = "Not allow!"
redirect_to root_path
end
end
end
12 changes: 12 additions & 0 deletions app/controllers/followships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
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]).first
@followship.destroy
flash[:alert] = "Followship destroyed"
redirect_back(fallback_location: tweets_path)
end
end
14 changes: 14 additions & 0 deletions app/controllers/replies_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
class RepliesController < ApplicationController

def index
@tweet = Tweet.find(params[:tweet_id])
@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
@reply.save!
redirect_to tweet_replies_path(@tweet)
end

private

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

end
33 changes: 33 additions & 0 deletions app/controllers/tweets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,49 @@
class TweetsController < ApplicationController
before_action :authenticate_user!
# before_action :authenticate_admin

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

def create
@tweet = current_user.tweets.build(tweet_params)
@tweet.save!
redirect_to tweets_path
end

def show
@tweet = Tweet.find(params[:id])
@reply = Reply.new
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_back(fallback_location: tweets_path)
end

def unfavorite
@restaurant = Restaurant.find(params[:id])
favorites = Favorite.where(restaurant: @restaurant, user: current_user)
favorites.destroy_all
redirect_back(fallback_location: root_path)
end

private

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

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

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

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

def edit
@user = User.find(params[:id])
unless @user == current_user
redirect_to user_path(@user)
end
end

def update
@user = User.find(params[:id])
if @user.update(user_params)
flash[:notice] ="user was successfully updated"
redirect_to tweets_user_path
else
flash[:alert] ="user was failed to update"
render :edit
end
end

def followings
@followings # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@users = @user.followings.all
end

def followers
@followers # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@users = @user.followers.all
end

def likes
@likes # 基於測試規格,必須講定變數名稱
@user = User.find(params[:id])
@user.count_likes
@likes = @user.liked_tweets.all.order(created_at: :desc)
end

private

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

end
3 changes: 3 additions & 0 deletions app/models/followship.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Followship < ApplicationRecord
validates :following_id, uniqueness: { scope: :user_id }

belongs_to :user
belongs_to :following, class_name:"User"

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: true
belongs_to :tweet, counter_cache: true
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

belongs_to :user, counter_cache: true

has_many :replies, dependent: :destroy
has_many :replied_users, through: :replies, source: :user

has_many :likes, dependent: :destroy
has_many :liked_tweets, through: :likes, source: :user


def is_liked?(tweet)
self.liked_tweets.include?(tweet)
end


end
Loading