From 7df042297e0fea82dcd20693e106e5cdf97c48bb Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 21:27:59 +0800 Subject: [PATCH 01/51] add tweet index action and view for public users --- app/views/tweets/index.html.erb | 1 + config/routes.rb | 2 ++ 2 files changed, 3 insertions(+) create mode 100644 app/views/tweets/index.html.erb diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb new file mode 100644 index 000000000..0a2483be5 --- /dev/null +++ b/app/views/tweets/index.html.erb @@ -0,0 +1 @@ +

frontend

diff --git a/config/routes.rb b/config/routes.rb index 90856d4fe..43a2ab7a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,8 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users + root "tweets#index" + # 請依照專案指定規格來設定路由 end From f1cd6380891398b0ef6a4eae03108234ac68a058 Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 21:34:43 +0800 Subject: [PATCH 02/51] add tweet view for admin --- app/views/admin/tweets/index.html.erb | 1 + config/routes.rb | 4 ++++ 2 files changed, 5 insertions(+) create mode 100644 app/views/admin/tweets/index.html.erb diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb new file mode 100644 index 000000000..aa74937be --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1 @@ +

backend

diff --git a/config/routes.rb b/config/routes.rb index 43a2ab7a9..7fe5efd2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,10 @@ root "tweets#index" + namespace :admin do + root "tweets#index" + end + # 請依照專案指定規格來設定路由 end From c7b0c5422e9e7a4f7995477d612e09de89275a5c Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 22:09:19 +0800 Subject: [PATCH 03/51] setup all routes for simple-twitter --- config/routes.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 7fe5efd2f..3e25fbad0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,9 +2,30 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users + resources :tweets, only:[:index, :create] do + resources :replies, only:[:index, :create] + member do + post :like + post :unlike + end + end + + resources :users, only:[:edit] do + member do + get :tweets + get :followings + get :followers + get :likes + end + end + + resources :followships, only:[:create, :destroy] + root "tweets#index" namespace :admin do + resources :tweets, only:[:index, :destroy] + resources :users, only:[:index] root "tweets#index" end From 275339665c086dcfc1b626e534973008eba39deb Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 22:21:45 +0800 Subject: [PATCH 04/51] add login process --- app/controllers/tweets_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..a2bba8912 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,4 +1,5 @@ class TweetsController < ApplicationController + before_action :authenticate_user! def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 From a49110bf7c807aff0bd6c5e67b153a4d25b02d1f Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 22:27:23 +0800 Subject: [PATCH 05/51] set up admin authentication --- app/controllers/application_controller.rb | 9 +++++++++ app/controllers/tweets_controller.rb | 1 + app/models/user.rb | 4 ++++ 3 files changed, 14 insertions(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..7924505a0 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,4 +4,13 @@ class ApplicationController < ActionController::Base # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # https://github.com/plataformatec/devise#strong-parameters # 注意有 sign_up 和 account_update 兩種參數要處理 + + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "Not allow!" + redirect_to root_path + end + end end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index a2bba8912..c896737d4 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,5 +1,6 @@ class TweetsController < ApplicationController before_action :authenticate_user! + before_action :authenticate_admin def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..4809788d4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,10 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + def admin? + self.role == "admin" + end + mount_uploader :avatar, AvatarUploader # 需要 app/views/devise 裡找到樣板,加上 name 屬性 From 8dd2efd92ca202a89b91069f6a5499219c4c023d Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 23:42:16 +0800 Subject: [PATCH 06/51] add column to registration form --- app/controllers/application_controller.rb | 5 +++++ app/models/user.rb | 1 + app/views/devise/registrations/edit.html.erb | 5 +++++ app/views/devise/registrations/new.html.erb | 5 +++++ app/views/layouts/application.html.erb | 2 +- 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7924505a0..015f2ca35 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -7,6 +7,11 @@ class ApplicationController < ActionController::Base 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 unless current_user.admin? flash[:alert] = "Not allow!" diff --git a/app/models/user.rb b/app/models/user.rb index 4809788d4..9ec5677ef 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,7 @@ def admin? # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 validates_presence_of :name + validates_uniqueness_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 10ed32a9e..a93ee9044 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -31,6 +31,11 @@ <%= f.password_field :current_password, autocomplete: "off" %> +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
<%= f.submit "Update" %>
diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 602803cff..ee18305dd 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -21,6 +21,11 @@ <%= f.password_field :password_confirmation, autocomplete: "off" %> +
+ <%= f.label :name %>
+ <%= f.text_field :name %> +
+
<%= f.submit "Sign up" %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 952cb7a1b..d6e61778d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@ <% if current_user %> <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_restaurants_path %>
  • +
  • <%= link_to 'Admin Panel', admin_users_path %>
  • <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • From ec316c4194888f546d8f94257462b6f5c8c38f3e Mon Sep 17 00:00:00 2001 From: Allie Date: Fri, 20 Jul 2018 23:57:12 +0800 Subject: [PATCH 07/51] add restaurant index view for admin --- app/controllers/admin/tweets_controller.rb | 1 + app/views/admin/tweets/index.html.erb | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..db20aa105 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,5 +1,6 @@ class Admin::TweetsController < Admin::BaseController def index + @tweets = Tweet.all end def destroy diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index aa74937be..44060ac91 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1 +1,10 @@ -

    backend

    +

    Tweet 後台

    +
      + <% @tweets.each do |tweet| %> +
    • + <%= tweet.name %> + <%= link_to 'Show', admin_tweets_path(tweet) %> + <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %> +
    • + <% end %> +
    From 6e73e0714a402e5c076146b3fe8972fb35d1408b Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 21 Jul 2018 00:15:35 +0800 Subject: [PATCH 08/51] generate fake tweets using FFaker gem --- app/views/admin/tweets/index.html.erb | 2 +- lib/tasks/dev.rake | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 44060ac91..d344f9776 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -2,7 +2,7 @@
      <% @tweets.each do |tweet| %>
    • - <%= tweet.name %> + <%= tweet.description %> <%= link_to 'Show', admin_tweets_path(tweet) %> <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %>
    • diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 9b1e87ae4..54430ceff 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -20,4 +20,15 @@ namespace :dev do end end + task fake_tweet: :environment do + 100.times do |i| + Tweet.create!( + description: FFaker::Lorem::characters(100) + ) + end + + puts "have created fake tweets" + puts "now you have #{Tweet.count} tweets data" + end + end From a8dfe23f79f146652a4b8a4ef5e7255eb2555d14 Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 21 Jul 2018 00:28:01 +0800 Subject: [PATCH 09/51] add tweet destroy action for admin --- app/controllers/admin/tweets_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index db20aa105..6ae44acd7 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -4,5 +4,9 @@ def index end def destroy + @tweet = Tweet.find(params[:id]) + @tweet.destroy + redirect_to admin_tweets_path + flash[:alert] = "tweet was deleted" end end From 033eead40007b103b726e6d283ad0b368fffc671 Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 21 Jul 2018 00:41:25 +0800 Subject: [PATCH 10/51] install bootstrap-sass, setup scss,and install gem jquery-rails for bootstrap js --- Gemfile | 4 ++++ Gemfile.lock | 13 ++++++++++++- app/assets/javascripts/application.js | 3 +++ .../{application.css => application.scss} | 5 +++-- 4 files changed, 22 insertions(+), 3 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (92%) diff --git a/Gemfile b/Gemfile index c1f1ee3e2..fd2766807 100644 --- a/Gemfile +++ b/Gemfile @@ -14,6 +14,8 @@ 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 @@ -41,6 +43,8 @@ gem 'jbuilder', '~> 2.5' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +gem 'jquery-rails' + 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] diff --git a/Gemfile.lock b/Gemfile.lock index e55e9522f..e533faa5a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -251,6 +260,7 @@ PLATFORMS x86-mswin32 DEPENDENCIES + bootstrap-sass (~> 3.3.7) byebug capybara (~> 2.13) carrierwave @@ -259,6 +269,7 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + jquery-rails listen (>= 3.0.5, < 3.2) puma (~> 3.7) rails (~> 5.1.4) @@ -276,4 +287,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..8e7956cfb 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,3 +13,6 @@ //= require rails-ujs //= require turbolinks //= require_tree . + +//= require jquery +//= require bootstrap-sprockets diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 92% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index d05ea0f51..46a81f2be 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -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"; From 0d374cad9a3dc1d76b9c27b98ecb0e2ba385a37e Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 21 Jul 2018 22:42:56 +0800 Subject: [PATCH 11/51] setup admin user (seed) --- db/seeds.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2acc..6f00efbda 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,15 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +file = File.open("#{Rails.root}/public/avatar/user#{rand(1..20)}.jpg") +User.create( + email: "admin@gmail.com", + password: "123456", + name: "Admin", + introduction: "hello~", + avatar: file, + role: "admin" +) + +puts "admin has created" From d073752d8a297a9e2c322f2bfd63f1151512454e Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 21 Jul 2018 23:13:16 +0800 Subject: [PATCH 12/51] add_registration_column(fix) --- app/controllers/application_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 015f2ca35..526478668 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ 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 兩種參數要處理 From cbf4154301400936baca1b731e23275bd3348065 Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 22 Jul 2018 00:57:12 +0800 Subject: [PATCH 13/51] add user edit and update action and views for admin --- app/controllers/users_controller.rb | 15 +++++++++++++++ app/views/users/edit.html.erb | 28 ++++++++++++++++++++++++++++ config/routes.rb | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 app/views/users/edit.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 750e3c6b5..7e5689df3 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -4,9 +4,18 @@ def tweets end def edit + @user = User.find(params[:id]) end def update + @user = User.find(params[:id]) + if @user.update(user_params) + flash[:notice] ="user was successfully updated" + redirect_to tweets_path + else + flash[:alert] ="user was failed to update" + render :edit + end end def followings @@ -21,4 +30,10 @@ def likes @likes # 基於測試規格,必須講定變數名稱 end +private + + def user_params + params.require(:user).permit(:name, :introduction, :avatar) + end + end diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..305470775 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,28 @@ +<% if @user.errors.any? %> +

      We have some errors here !

      +
        + <% @user.errors.full_messages.each do |msg| %> +
      • <%= msg %>
      • + <% end %> +
      +<% end %> + +

      Profile

      +<%= form_for @user do |f| %> +
      + <%= f.label :name, "Name" %> + <%= f.text_field :name %> +
      + +
      + <%= f.label :introduction, "Introduction" %> + <%= f.text_field :introduction %> +
      + +
      + <%= f.label :avatar, "Avatar" %> + <%= f.text_field :avatar %> +
      + +<%= f.submit %> +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 3e25fbad0..a6943ff46 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ end end - resources :users, only:[:edit] do + resources :users, only:[:edit, :update] do member do get :tweets get :followings From 0a023c73190213fada17bc8dcfa6a4202849d5da Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 22 Jul 2018 01:32:57 +0800 Subject: [PATCH 14/51] generate link between user, tweet, and reply model --- app/models/reply.rb | 2 ++ app/models/tweet.rb | 3 +++ app/models/user.rb | 3 +++ 3 files changed, 8 insertions(+) diff --git a/app/models/reply.rb b/app/models/reply.rb index bae6f9463..223589f44 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,2 +1,4 @@ class Reply < ApplicationRecord + belongs_to :user + belongs_to :tweet end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6715fada2..f1b906f9c 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,4 +1,7 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 + belongs_to :user + has_many :replies, dependent: :destroy + end diff --git a/app/models/user.rb b/app/models/user.rb index 9ec5677ef..57066ea6f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,4 +16,7 @@ def admin? validates_uniqueness_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + has_many :tweets, dependent: :destroy + has_many :replies, dependent: :destroy + end From 04a3cd6b0ead42999dacc31afea7920155709b15 Mon Sep 17 00:00:00 2001 From: Allie Date: Wed, 25 Jul 2018 08:17:13 +0800 Subject: [PATCH 15/51] fix authenticate_admin --- app/controllers/admin/tweets_controller.rb | 2 +- app/controllers/application_controller.rb | 3 +++ app/controllers/tweets_controller.rb | 3 ++- app/views/tweets/index.html.erb | 15 +++++++++++++++ lib/tasks/dev.rake | 4 +++- 5 files changed, 24 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 6ae44acd7..834e94f13 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,6 +1,6 @@ class Admin::TweetsController < Admin::BaseController def index - @tweets = Tweet.all + @tweets = Tweet.all.order(created_at: :desc) end def destroy diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 526478668..0c554362f 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,7 +14,10 @@ def configure_permitted_parameters end def authenticate_admin + puts "authenticate_admin" unless current_user.admin? + puts "===================================================" + puts "???????????????????????????????????????????/" flash[:alert] = "Not allow!" redirect_to root_path end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c896737d4..8514b24ae 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,9 +1,10 @@ class TweetsController < ApplicationController before_action :authenticate_user! - before_action :authenticate_admin + # before_action :authenticate_admin def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @tweets = Tweet.all.order(created_at: :desc) end def create diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 0a2483be5..882880610 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1 +1,16 @@

      frontend

      + + + +
      + <% @tweets.each do |tweet| %> +
      +
      +
      + <%= tweet.user.name %> + <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> + <%= tweet.description %> + <%= link_to 'Reply' %> +
      + <% end %> +
      diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 54430ceff..35a35056e 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -21,9 +21,11 @@ namespace :dev do end task fake_tweet: :environment do + Tweet.destroy_all 100.times do |i| Tweet.create!( - description: FFaker::Lorem::characters(100) + description: FFaker::Lorem::characters(100), + user: User.all.sample ) end From a922736df7a157856c4cca211884c84ca4367478 Mon Sep 17 00:00:00 2001 From: Allie Date: Wed, 25 Jul 2018 08:24:31 +0800 Subject: [PATCH 16/51] fix link between user, reply, and tweet --- app/models/tweet.rb | 1 + app/models/user.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/app/models/tweet.rb b/app/models/tweet.rb index f1b906f9c..302a9e659 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -3,5 +3,6 @@ class Tweet < ApplicationRecord belongs_to :user has_many :replies, dependent: :destroy + has_many :replied_users, through: :replies, source: :user end diff --git a/app/models/user.rb b/app/models/user.rb index 57066ea6f..8123f65e1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,5 +18,6 @@ def admin? has_many :tweets, dependent: :destroy has_many :replies, dependent: :destroy + has_many :replied_tweets, through: :replies, source: :tweet end From 60e402345b350bf56b89fffe48d7447b13affb54 Mon Sep 17 00:00:00 2001 From: Allie Date: Thu, 26 Jul 2018 00:12:43 +0800 Subject: [PATCH 17/51] user can create tweets on index page --- app/controllers/tweets_controller.rb | 10 ++++++++++ app/views/tweets/index.html.erb | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 8514b24ae..b9d5e3f4d 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -5,9 +5,13 @@ class TweetsController < ApplicationController def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweets = Tweet.all.order(created_at: :desc) + @tweet = Tweet.new end def create + @tweet = current_user.tweets.build(tweet_params) + @tweet.save! + redirect_to tweets_path end def like @@ -16,4 +20,10 @@ def like def unlike end +private + + def tweet_params + params.require(:tweet).permit(:description) + end + end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 882880610..9f97d13d9 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,6 +1,19 @@

      frontend

      +
      +
      + <%= form_for @tweet do |f| %> +
      + <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control" %> +
      +
      + <%= f.submit "Tweet", class: "btn btn-primary" %> +
      + <% end %> +
      +
      +
      <% @tweets.each do |tweet| %> From aa7cbe4083852f18c8f024596f770afe02afc334 Mon Sep 17 00:00:00 2001 From: Allie Date: Thu, 26 Jul 2018 00:49:26 +0800 Subject: [PATCH 18/51] user can edit profile and see other's profile page --- app/controllers/users_controller.rb | 7 +++++++ app/views/tweets/index.html.erb | 2 +- app/views/users/show.html.erb | 26 ++++++++++++++++++++++++++ config/routes.rb | 2 +- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 7e5689df3..28374568a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,8 +3,15 @@ class UsersController < ApplicationController def 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 diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 9f97d13d9..c2db3bfae 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -20,7 +20,7 @@
      - <%= tweet.user.name %> + <%= link_to tweet.user.name, user_path(tweet.user) %> <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> <%= tweet.description %> <%= link_to 'Reply' %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 000000000..0a4ef7b74 --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,26 @@ +

      Profile

      + +
      +
      +
      + + + + +
      +

      <%= @user.name %>

      +

      <%= @user.email %>

      +

      <%= simple_format @user.introduction %>

      + <% if @user.email == current_user.email %> + <%= link_to 'Edit', edit_user_path, class:"btn btn-primary" %> + <% end %> +
      +
      +
      + +
      + + +
      +
      +
      diff --git a/config/routes.rb b/config/routes.rb index a6943ff46..5b2860f12 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ end end - resources :users, only:[:edit, :update] do + resources :users, only:[:show, :edit, :update] do member do get :tweets get :followings From e2968a25b9df44b3427530182954d65157cb0ea6 Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 28 Jul 2018 12:07:43 +0800 Subject: [PATCH 19/51] fake data(reply) & user can reply others tweet_1 --- app/controllers/replies_controller.rb | 11 +++++++++++ app/controllers/tweets_controller.rb | 5 +++++ app/views/replies/index.html.erb | 1 + app/views/tweets/index.html.erb | 2 +- config/routes.rb | 2 +- lib/tasks/dev.rake | 14 ++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 app/views/replies/index.html.erb diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index a9b6a315b..24c4b91d3 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -4,6 +4,17 @@ def index end def create + @tweet = Tweet.find(params[:tweet_id]) + @reply = @tweet.replies.build(reply_params) + @reply.user = current_user + @reply.save! + redirect_to tweet_path(@tweet) + end + +private + + def reply_params + params.require(:reply).permit(:comment) end end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index b9d5e3f4d..4e00303da 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -14,6 +14,11 @@ def create redirect_to tweets_path end + def show + @tweet = Tweet.find(params[:tweet_id]) + @reply = Reply.new + end + def like end diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb new file mode 100644 index 000000000..12fe7da1d --- /dev/null +++ b/app/views/replies/index.html.erb @@ -0,0 +1 @@ +

      Tweet show page

      diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index c2db3bfae..6e3e48178 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -23,7 +23,7 @@ <%= link_to tweet.user.name, user_path(tweet.user) %> <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> <%= tweet.description %> - <%= link_to 'Reply' %> + <%= link_to 'Reply', tweet_replies_path([:tweet_id]) %>
      <% end %> diff --git a/config/routes.rb b/config/routes.rb index 5b2860f12..da7a0a1b4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,7 +2,7 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users - resources :tweets, only:[:index, :create] do + resources :tweets, only:[:index, :create, :show] do resources :replies, only:[:index, :create] member do post :like diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 35a35056e..2364e2d73 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -33,4 +33,18 @@ namespace :dev do puts "now you have #{Tweet.count} tweets data" end + task fake_reply: :environment do + Reply.destroy_all + Tweet.all.each do |tweet| + 3.times do |i| + tweet.replies.create!( + comment:FFaker::Lorem.paragraph, + user: User.all.sample + ) + end + end + puts "have created fake replies!" + puts "now you have #{Reply.count} reply data!" + end + end From d7fea539978ae3864402442b719a87405743cdc7 Mon Sep 17 00:00:00 2001 From: Allie Date: Sat, 28 Jul 2018 14:49:20 +0800 Subject: [PATCH 20/51] styled navbar --- app/assets/stylesheets/application.scss | 14 +++++++ app/views/layouts/application.html.erb | 53 +++++++++++++++++-------- 2 files changed, 51 insertions(+), 16 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 46a81f2be..f80390f17 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -14,3 +14,17 @@ @import "bootstrap-sprockets"; @import "bootstrap"; + +.container-fluid{ + background-color: #B0C4DE; +} + +.navbar-header{ + font-size: 50px; + color: #696969; +} + +.navbar-body{ + font-size: 20px; + color: #696969; +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d6e61778d..8b7114292 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,20 +9,41 @@ - <% if current_user %> - <% if current_user&.admin? %> -
    • <%= link_to 'Admin Panel', admin_users_path %>
    • - <% end %> -
    • -
    • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
    • -
    • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
    • -
    • <%= link_to('修改密碼', edit_user_registration_path) %>
    • - <% else %> -
    • <%= link_to('註冊', new_user_registration_path) %>
    • -
    • <%= link_to('登入', new_user_session_path) %>
    • - <% end %> -

      <%= notice %>

      -

      <%= alert %>

      - <%= yield %> - +
    <% end %> -

    Profile

    -<%= form_for @user do |f| %> -
    - <%= f.label :name, "Name" %> - <%= f.text_field :name %> -
    +
    +
    + <%= form_for @user do |f| %> +
    +
    + <%= image_tag @user.avatar, width: '300px' if @user.avatar? %> + <%= f.file_field :avatar %> +
    +
    -
    - <%= f.label :introduction, "Introduction" %> - <%= f.text_field :introduction %> -
    +
    +
    +
    + <%= f.label :name, "Name" %> + <%= f.text_field :name %> +
    -
    - <%= f.label :avatar, "Avatar" %> - <%= f.text_field :avatar %> -
    +
    + <%= f.label :introduction, "Introduction" %> + <%= f.text_area :introduction %> +
    +
    +
    <%= f.submit %> <% end %> +
    diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 0a4ef7b74..724d143dc 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,13 +1,10 @@

    Profile

    +
    +
    + <%= image_tag @user.avatar, class: "img-responsive center-block" %> +
    -
    -
    -
    - - - - -
    +

    <%= @user.name %>

    <%= @user.email %>

    <%= simple_format @user.introduction %>

    From c9bfa73ea9cb5f976501a59c9ae76c57f6f2ffa7 Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 12:07:06 +0800 Subject: [PATCH 22/51] styled tweets index page_2 --- app/assets/stylesheets/application.scss | 5 +++ app/controllers/replies_controller.rb | 2 + app/views/replies/index.html.erb | 12 +++++ app/views/tweets/index.html.erb | 59 ++++++++++++++++--------- 4 files changed, 56 insertions(+), 22 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index f80390f17..8cd418a78 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -28,3 +28,8 @@ font-size: 20px; color: #696969; } + +textarea.text-area { + font-size: 24px; + height: 128px; +} diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 24c4b91d3..0d96c41f5 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,6 +1,8 @@ class RepliesController < ApplicationController def index + @tweet = Tweet.find(tweet_params) + @reply = Reply.new end def create diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 12fe7da1d..c3eefe09b 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1 +1,13 @@

    Tweet show page

    + + +<% @tweet.replies.each do |reply| %> +
    +

    <%= reply.user.name %>

    +

    <%= simple_format reply.comment %>

    +

    + <%= reply.created_at %> +

    +
    +
    +<% end %> diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 6e3e48178..52d18c8fd 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,29 +1,44 @@ -

    frontend

    -
    -
    - <%= form_for @tweet do |f| %> -
    - <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control" %> +
    +
    +
    + <%= form_for @tweet do |f| %> +
    + <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %> +
    +
    + <%= f.submit "Tweet", class: "btn btn-primary" %> +
    + <% end %> + +
    + <% @tweets.each do |tweet| %> +
    + <%= image_tag tweet.user.avatar, size: "100" if tweet.user.avatar? %> +
    +
    +

    + <%= link_to tweet.user.name, user_path(tweet.user) %> + <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> +

    +

    + <%= tweet.description %> +

    +
    + <%= link_to 'Reply', tweet_replies_path(tweet) %> +
    +
    + <% end %> +
    +
    + +
    -
    - <%= f.submit "Tweet", class: "btn btn-primary" %>
    - <% end %>
    -
    -
    -
    - <% @tweets.each do |tweet| %> -
    -
    -
    - <%= link_to tweet.user.name, user_path(tweet.user) %> - <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> - <%= tweet.description %> - <%= link_to 'Reply', tweet_replies_path([:tweet_id]) %> -
    - <% end %> + +
    +

    top 10 replies

    From 9c4d79e44afed0525cf7d2ce86d34fe1511838f1 Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 13:05:08 +0800 Subject: [PATCH 23/51] show user profile page --- app/controllers/replies_controller.rb | 2 +- app/views/users/show.html.erb | 38 ++++++++++++++++++--------- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 0d96c41f5..5be8df1f9 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,7 +1,7 @@ class RepliesController < ApplicationController def index - @tweet = Tweet.find(tweet_params) + @tweet = Tweet.find(params[:tweet_id]) @reply = Reply.new end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 724d143dc..e8f7c724e 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,23 +1,35 @@ -

    Profile

    -
    +
    +
    +
    - <%= image_tag @user.avatar, class: "img-responsive center-block" %> + <%= image_tag @user.avatar %>
    -
    -

    <%= @user.name %>

    -

    <%= @user.email %>

    -

    <%= simple_format @user.introduction %>

    - <% if @user.email == current_user.email %> - <%= link_to 'Edit', edit_user_path, class:"btn btn-primary" %> - <% end %> -
    -
    +
    +

    <%= @user.name %>

    +

    <%= @user.email %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    Tweets

    +

    Following

    +

    Follower

    +

    Likes

    -
    +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    +
    + +
    +

    my tweet

    +
    From 3ef5b7a194151393e88854d96928d8a978a2bd3a Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 13:25:04 +0800 Subject: [PATCH 24/51] setup relation among user, tweet and like --- app/models/like.rb | 2 ++ app/models/tweet.rb | 3 +++ app/models/user.rb | 3 +++ 3 files changed, 8 insertions(+) diff --git a/app/models/like.rb b/app/models/like.rb index d99b93a32..5771fe616 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,2 +1,4 @@ class Like < ApplicationRecord + belongs_to :user + belongs_to :tweet end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 302a9e659..cc3e5607e 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -5,4 +5,7 @@ class Tweet < ApplicationRecord 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 + end diff --git a/app/models/user.rb b/app/models/user.rb index 8123f65e1..ef9f77df0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -20,4 +20,7 @@ def admin? has_many :replies, dependent: :destroy has_many :replied_tweets, through: :replies, source: :tweet + has_many :likes, dependent: :destroy + has_many :liked_tweets, through: :likes, source: :tweet + end From d9be7ccea9c6e7a4ae43ed269e91874540b8f3d2 Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 15:41:17 +0800 Subject: [PATCH 25/51] failure --- app/controllers/tweets_controller.rb | 7 +++++++ app/views/tweets/index.html.erb | 2 ++ 2 files changed, 9 insertions(+) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 4e00303da..31bac5244 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -20,9 +20,16 @@ def show end def like + @tweet = Tweet.find(params[:id]) + @tweet.likes.create!(user: current_user) + redirect_back(fallback_location: root_path) end def unlike + @tweet = Tweet.find(params[:id]) + likes = Like.where(tweet: @tweet, user: current_user) + likes.destroy_all + redirect_back(fallback_location: root_path) end private diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 52d18c8fd..784241764 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -26,6 +26,8 @@

    <%= link_to 'Reply', tweet_replies_path(tweet) %> + <%= link_to 'Like', like_tweet_path(:tweet_id), method: :post, class: "btn btn-primary" %> + <%= link_to 'Unlike', unlike_tweet_path(:tweet_id), method: :post, class: "btn btn-info" %>
    <% end %> From 13b576de3907db73a8797c0d1f574e920c5c31ea Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 22:40:36 +0800 Subject: [PATCH 26/51] like/unlike button and controller action --- app/controllers/tweets_controller.rb | 4 ++-- app/models/tweet.rb | 4 ++++ app/views/tweets/index.html.erb | 9 ++++++--- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 31bac5244..4a1b1014b 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -27,8 +27,8 @@ def like def unlike @tweet = Tweet.find(params[:id]) - likes = Like.where(tweet: @tweet, user: current_user) - likes.destroy_all + @like = Like.where(tweet: @tweet, user: current_user).first + @like.destroy redirect_back(fallback_location: root_path) end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index cc3e5607e..9d25b18b5 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -8,4 +8,8 @@ class Tweet < ApplicationRecord has_many :likes, dependent: :destroy has_many :liked_tweets, through: :likes, source: :user + def is_liked?(tweet) + self.liked_tweets.include?(tweet) + end + end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 784241764..081645822 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -25,9 +25,12 @@ <%= tweet.description %>

    - <%= link_to 'Reply', tweet_replies_path(tweet) %> - <%= link_to 'Like', like_tweet_path(:tweet_id), method: :post, class: "btn btn-primary" %> - <%= link_to 'Unlike', unlike_tweet_path(:tweet_id), method: :post, class: "btn btn-info" %> + <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> + <% if @tweet.is_liked?(current_user) %> + <%= link_to 'Unlike', unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% else %> + <%= link_to 'Like', like_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% end %>
    <% end %> From 8ec64096547ca0a5d1e4547cb8326cde10d58b39 Mon Sep 17 00:00:00 2001 From: Allie Date: Sun, 29 Jul 2018 22:53:58 +0800 Subject: [PATCH 27/51] show like count --- app/models/tweet.rb | 6 ++++++ app/views/tweets/index.html.erb | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 9d25b18b5..4ae04003a 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -12,4 +12,10 @@ def is_liked?(tweet) self.liked_tweets.include?(tweet) end + def count_likes + self.likes_count = self.like.size + self.save + end + + end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 081645822..d8ccb0f9c 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -27,9 +27,9 @@
    <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> <% if @tweet.is_liked?(current_user) %> - <%= link_to 'Unlike', unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <%= link_to "Unlike", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> <% else %> - <%= link_to 'Like', like_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet.id), method: :post, class: "links links-red" %> <% end %>
    From fb3fcd67165644ed48809f5b365cafd2a63b99a1 Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 10:45:35 +0800 Subject: [PATCH 28/51] setup self-referential relationships within users table through followships --- app/models/followship.rb | 3 +++ app/models/user.rb | 3 +++ 2 files changed, 6 insertions(+) diff --git a/app/models/followship.rb b/app/models/followship.rb index 1aed01396..4d8325d4a 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,4 +1,7 @@ class Followship < ApplicationRecord validates :following_id, uniqueness: { scope: :user_id } + belongs_to :user + belongs_to :following, class_name:"User" + end diff --git a/app/models/user.rb b/app/models/user.rb index ef9f77df0..dc6e35554 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,4 +23,7 @@ def admin? has_many :likes, dependent: :destroy has_many :liked_tweets, through: :likes, source: :tweet + has_many :followships, dependent: :destroy + has_many :followings, through: :followships + end From d07895d3bf74266d77de0c2c488e4d47bcd04ee4 Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 10:58:21 +0800 Subject: [PATCH 29/51] fix the path and add users/tweets.html.erb --- app/controllers/users_controller.rb | 1 + app/views/tweets/index.html.erb | 2 +- app/views/users/tweets.html.erb | 35 +++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 app/views/users/tweets.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 28374568a..94ddd9a62 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,7 @@ class UsersController < ApplicationController def tweets + @user = User.find(params[:id]) end def show diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d8ccb0f9c..d03337f0d 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -18,7 +18,7 @@

    - <%= link_to tweet.user.name, user_path(tweet.user) %> + <%= link_to tweet.user.name, tweets_user_path(tweet.user) %> <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %>

    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb new file mode 100644 index 000000000..e8f7c724e --- /dev/null +++ b/app/views/users/tweets.html.erb @@ -0,0 +1,35 @@ +

    +
    +
    +
    + <%= image_tag @user.avatar %> +
    + +
    +

    <%= @user.name %>

    +

    <%= @user.email %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    Tweets

    +

    Following

    +

    Follower

    +

    Likes

    +
    + +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    + +
    + +
    +
    +
    + +
    +

    my tweet

    +
    From abfd13fbda432ee3675041b1349ceb0d36cdc426 Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 11:06:23 +0800 Subject: [PATCH 30/51] fix the path --- app/views/layouts/application.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 8b7114292..5f8f34f35 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,7 +22,7 @@ <% end %> From cc3fdcf61ab1541aa1a6fed38e6b3c92f28833ac Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 11:25:23 +0800 Subject: [PATCH 31/51] add user tweet page with follow button and add create action for followship record --- app/controllers/followships_controller.rb | 8 ++++++++ app/views/users/tweets.html.erb | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 05f01b552..6542d1726 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,5 +1,13 @@ 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 diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index e8f7c724e..751692a6e 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -18,6 +18,10 @@

    Likes

    +
    + <%= link_to 'Follow',followships_path(following_id: @user), method: :post %> +
    +
    <% if @user.email == current_user.email %> <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> From 7b9937529d59612b4c51bb333f1af9989c507d51 Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 11:29:56 +0800 Subject: [PATCH 32/51] add unfollow button on user index page and destroy action --- app/controllers/followships_controller.rb | 4 ++++ app/views/users/tweets.html.erb | 1 + 2 files changed, 5 insertions(+) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 6542d1726..4efb31340 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -11,5 +11,9 @@ def create 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 diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 751692a6e..8f3aaf8b2 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -20,6 +20,7 @@
    <%= link_to 'Follow',followships_path(following_id: @user), method: :post %> + <%= link_to 'Unfollow', followship_path(@user), method: :delete %>
    From 422b4bf4d76a0d1e5249adb3a78decac85ed53fa Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 13:33:09 +0800 Subject: [PATCH 33/51] show popular tweeter on index page & styled index page --- app/assets/stylesheets/application.scss | 6 ++ app/controllers/tweets_controller.rb | 1 + app/controllers/users_controller.rb | 1 + app/models/user.rb | 5 ++ app/views/tweets/index.html.erb | 73 ++++++++++++++++--------- 5 files changed, 59 insertions(+), 27 deletions(-) diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 8cd418a78..1fc2d2451 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -33,3 +33,9 @@ textarea.text-area { font-size: 24px; height: 128px; } + +.tweet-item { + border: 2px solid lightgray; + padding: 10px; + margin-bottom: 15px; + } diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 4a1b1014b..6a4f415b8 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -6,6 +6,7 @@ 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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 94ddd9a62..ae463e6a6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -32,6 +32,7 @@ def followings def followers @followers # 基於測試規格,必須講定變數名稱 + @user.count_followers end def likes diff --git a/app/models/user.rb b/app/models/user.rb index dc6e35554..4236aee97 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,11 @@ def admin? self.role == "admin" end + def count_followers + self.followers_count = self.followers.size + self.save + end + mount_uploader :avatar, AvatarUploader # 需要 app/views/devise 裡找到樣板,加上 name 屬性 diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d03337f0d..6186c09de 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,7 +1,7 @@
    -
    +
    <%= form_for @tweet do |f| %>
    <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %> @@ -11,39 +11,58 @@
    <% end %> -
    +
    <% @tweets.each do |tweet| %> -
    - <%= image_tag tweet.user.avatar, size: "100" if tweet.user.avatar? %> -
    -
    -

    - <%= link_to tweet.user.name, tweets_user_path(tweet.user) %> - <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> -

    -

    - <%= tweet.description %> -

    -
    - <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> - <% if @tweet.is_liked?(current_user) %> - <%= link_to "Unlike", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> - <% else %> - <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet.id), method: :post, class: "links links-red" %> - <% end %> +
    +
    +
    + <%= image_tag tweet.user.avatar, size: "100" if tweet.user.avatar? %> +
    +
    +

    + <%= link_to tweet.user.name, tweets_user_path(tweet.user) %> + <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> +

    +

    + <%= tweet.description %> +

    +
    + <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> + <% if @tweet.is_liked?(current_user) %> + <%= link_to "Unlike", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% else %> + <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% end %> +
    +
    <% end %>
    +
    +
    +

    Popular

    + + <% @users.each do |user| %> +
    +
    +
    + <%= image_tag user.avatar, size: "100" %> +
    +
    +

    <%= link_to user.name, tweets_user_path(user) %>

    +

    <%= truncate(user.introduction, length: 140) %>

    +
    + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default pull-right" %> + <%= link_to 'Unfollow', followship_path(user.id), method: :delete, class: "btn btn-default pull-right" %> +
    +
    +
    +
    + <% end %> +
    -
    - - - -
    -

    top 10 replies

    -
    From 97d60add9143ab45409469256e48d34cb506b348 Mon Sep 17 00:00:00 2001 From: Allie Date: Mon, 30 Jul 2018 14:28:47 +0800 Subject: [PATCH 34/51] admin can see all tweets and replies --- app/controllers/admin/tweets_controller.rb | 1 + app/views/admin/tweets/index.html.erb | 42 +++++++++++++++++----- app/views/layouts/application.html.erb | 2 +- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 834e94f13..a4571dbaa 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,6 +1,7 @@ class Admin::TweetsController < Admin::BaseController def index @tweets = Tweet.all.order(created_at: :desc) + @replies = Reply.all end def destroy diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index d344f9776..f2aba19e1 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,10 +1,34 @@

    Tweet 後台

    -
      - <% @tweets.each do |tweet| %> -
    • - <%= tweet.description %> - <%= link_to 'Show', admin_tweets_path(tweet) %> - <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %> -
    • - <% end %> -
    +
    +
    +
    +
    + <%= link_to "Tweet", class: "btn btn-primary" %> +
    +
    + + + + + + + + + + + + + <% @tweets.each do |tweet| %> + + + + + + + + <% end %> + +
    IDAuthurDescriptionReplies#
    <%= tweet.id %><%= tweet.user.name %><%= tweet.description %><%= tweet.replies %><%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %>
    +
    +
    +
    diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5f8f34f35..625c34f22 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -18,7 +18,7 @@
    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index ca328dba4..84a8d6ded 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -18,8 +18,13 @@
    - <%= link_to 'Follow',followships_path(following_id: @user), method: :post %> - <%= link_to 'Unfollow', followship_path(@user), method: :delete %> + <% if @user != current_user %> + <% if current_user.following?(@user)%> + <%= link_to 'Unfollow', followship_path(@user), method: :delete, class: "btn btn-default" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default" %> + <% end %> + <% end %>
    From 61e7be1ad859aac553cd253e8d80468a60d4c91c Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 02:59:39 +0800 Subject: [PATCH 39/51] styled replies#index --- app/controllers/replies_controller.rb | 1 + app/views/replies/index.html.erb | 92 +++++++++++++++++++++++---- 2 files changed, 80 insertions(+), 13 deletions(-) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 5be8df1f9..4e40b659b 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -3,6 +3,7 @@ class RepliesController < ApplicationController def index @tweet = Tweet.find(params[:tweet_id]) @reply = Reply.new + @user = @tweet.user end def create diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index c3eefe09b..8d9179e15 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1,13 +1,79 @@ -

    Tweet show page

    - - -<% @tweet.replies.each do |reply| %> -
    -

    <%= reply.user.name %>

    -

    <%= simple_format reply.comment %>

    -

    - <%= reply.created_at %> -

    -
    -
    -<% end %> + +
    +
    +
    +
    + <%= image_tag @user.avatar, size: "250" %> +
    + +
    +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    Tweets

    +

    Following

    +

    Follower

    +

    Likes

    +
    + +
    + <% if @user != current_user %> + <% if current_user.following?(@user)%> + <%= link_to 'Unfollow', followship_path(@user), method: :delete, class: "btn btn-default" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default" %> + <% end %> + <% end %> +
    + +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    + +
    +
    + + +
    +

    Tweets

    +
    +
    +
    + <%= image_tag @tweet.user.avatar, size: "100" if @tweet.user.avatar? %> +
    +
    +

    + <%= link_to @tweet.user.name, tweets_user_path(@tweet.user) %> + <%= @tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> +

    +

    + <%= @tweet.description %> +

    +
    + <%= link_to 'Reply', tweet_replies_path(@tweet), class:"links links-blue" %> + <% if @tweet.is_liked?(current_user) %> + <%= link_to "Unlike", unlike_tweet_path(@tweet.id), method: :post, class: "links links-red" %> + <% else %> + <%= link_to "Like(#{@tweet.likes_count})", like_tweet_path(@tweet.id), method: :post, class: "links links-red" %> + <% end %> +
    +
    +
    +
    + +

    Replies

    + <% @tweet.replies.each do |reply| %> +
    +

    <%= reply.user.name %>

    +

    <%= simple_format reply.comment %>

    +

    + <%= reply.created_at %> +

    +
    +
    + <% end %> +
    From aab6ce383530509e14160d06c17d33ad46d77cbd Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 03:28:33 +0800 Subject: [PATCH 40/51] user can add replies on replies index page --- app/controllers/replies_controller.rb | 2 +- app/controllers/tweets_controller.rb | 2 +- app/views/replies/index.html.erb | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 4e40b659b..6542c1f18 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -11,7 +11,7 @@ def create @reply = @tweet.replies.build(reply_params) @reply.user = current_user @reply.save! - redirect_to tweet_path(@tweet) + redirect_to tweet_replies_path(@tweet) end private diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index f3e6a648e..362dcbdad 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -16,7 +16,7 @@ def create end def show - @tweet = Tweet.find(params[:tweet_id]) + @tweet = Tweet.find(params[:id]) @reply = Reply.new end diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 8d9179e15..14c4e1da5 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -76,4 +76,19 @@

    <% end %> + + +
    + <%= form_for [@tweet, @reply] do |f| %> +
    + <%= f.text_area :comment, class: "form-control text-area" %> +
    +
    + <%= f.submit class: "btn btn-primary" %> +
    + <% end %> +
    +
    +
    +
    From 25d336b2c7f9d17838b066d7c98c2d2396636399 Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 03:51:54 +0800 Subject: [PATCH 41/51] fix the error --- app/controllers/users_controller.rb | 2 +- app/views/replies/index.html.erb | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index dbc519d84..bb4cec1c2 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -20,7 +20,7 @@ def update @user = User.find(params[:id]) if @user.update(user_params) flash[:notice] ="user was successfully updated" - redirect_to tweets_path + redirect_to tweets_user_path else flash[:alert] ="user was failed to update" render :edit diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 14c4e1da5..4196fd6ea 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -67,15 +67,22 @@

    Replies

    <% @tweet.replies.each do |reply| %> -
    -

    <%= reply.user.name %>

    -

    <%= simple_format reply.comment %>

    -

    - <%= reply.created_at %> -

    -
    -
    +
    +
    +
    + <%= image_tag reply.user.avatar, size: "100"%> +
    +
    +

    <%= reply.user.name %>

    +

    <%= simple_format reply.comment %>

    +

    + <%= reply.created_at %> +

    +
    +
    +
    <% end %> +
    From 4b3e8025f16c14df033cdb0c4349d6870673ee8d Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 19:12:28 +0800 Subject: [PATCH 42/51] admin can see tweets_count on admin panel --- app/controllers/admin/users_controller.rb | 2 +- app/models/tweet.rb | 3 ++- db/migrate/20180731110507_add_tweets_count_to_user.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20180731110507_add_tweets_count_to_user.rb diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index ade3c6835..1cb09b046 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,5 +1,5 @@ class Admin::UsersController < Admin::BaseController def index - @users = User.all.order(created_at: :desc) + @users = User.all.order(tweets_count: :desc) end end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 1d262b740..46d922edf 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,7 +1,8 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 - belongs_to :user + belongs_to :user, counter_cache: true + has_many :replies, dependent: :destroy has_many :replied_users, through: :replies, source: :user diff --git a/db/migrate/20180731110507_add_tweets_count_to_user.rb b/db/migrate/20180731110507_add_tweets_count_to_user.rb new file mode 100644 index 000000000..49ec05c2e --- /dev/null +++ b/db/migrate/20180731110507_add_tweets_count_to_user.rb @@ -0,0 +1,5 @@ +class AddTweetsCountToUser < ActiveRecord::Migration[5.1] + def change + add_column :users, :tweets_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a6842c86..41fce7ce3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180201102838) do +ActiveRecord::Schema.define(version: 20180731110507) do create_table "followships", force: :cascade do |t| t.integer "user_id" @@ -66,6 +66,7 @@ t.integer "likes_count", default: 0 t.string "role", default: "normal" t.integer "followers_count", default: 0 + t.integer "tweets_count" t.index ["email"], name: "index_users_on_email", unique: true t.index ["name"], name: "index_users_on_name", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true From 5b4089b6a97cad87ef3944d36a193fb2fe36520f Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 19:19:15 +0800 Subject: [PATCH 43/51] user can see tweets_count on replies#index and users#tweets --- app/views/replies/index.html.erb | 8 ++++---- app/views/users/tweets.html.erb | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 4196fd6ea..c351894cd 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -12,10 +12,10 @@
    -

    Tweets

    -

    Following

    -

    Follower

    -

    Likes

    +

    Tweets <%= @user.tweets.count %>

    +

    Following

    +

    Follower

    +

    Likes

    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 84a8d6ded..72356df07 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -11,10 +11,10 @@
    -

    Tweets

    -

    Following

    -

    Follower

    -

    Likes

    +

    Tweets <%= @user.tweets.count %>

    +

    Following

    +

    Follower

    +

    Likes

    From 359dc1174bc9e25eb85ba7f507042cc7f6553256 Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 20:06:16 +0800 Subject: [PATCH 44/51] admin::can see tweets replies --- app/views/admin/tweets/index.html.erb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 1962e2b32..60a39e726 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -23,7 +23,11 @@ <%= tweet.id %> <%= tweet.user.name %> <%= tweet.description %> - <%= tweet.replies %> + + <% tweet.replies.each do |reply| %> +

    <%= reply.comment %>

    + <% end %> + <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %> <% end %> From 7c483a963f8ec7f3c195b6f0a01c5171edafe714 Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 22:42:53 +0800 Subject: [PATCH 45/51] add likes_count --- app/controllers/admin/users_controller.rb | 1 + app/models/like.rb | 2 +- app/views/tweets/index.html.erb | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 1cb09b046..61097d8b1 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,5 +1,6 @@ class Admin::UsersController < Admin::BaseController def index @users = User.all.order(tweets_count: :desc) + @tweets = Tweet.all.order(likes_count: :desc) end end diff --git a/app/models/like.rb b/app/models/like.rb index 5771fe616..42d487577 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,4 +1,4 @@ class Like < ApplicationRecord belongs_to :user - belongs_to :tweet + belongs_to :tweet, counter_cache: true end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index a7958501e..38140a12f 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -29,7 +29,7 @@
    <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> <% if tweet.is_liked?(current_user) %> - <%= link_to "Unlike", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <%= link_to "Unlike(#{tweet.likes_count})", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> <% else %> <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet.id), method: :post, class: "links links-red" %> <% end %> From 9a2d820b3985b9b19929e9ad6a344734ed36de99 Mon Sep 17 00:00:00 2001 From: Allie Date: Tue, 31 Jul 2018 22:46:49 +0800 Subject: [PATCH 46/51] add current_user avatar on nav --- app/views/layouts/application.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 625c34f22..a7cd93fc1 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -22,6 +22,7 @@ <% end %>
    -

    Tweets <%= @user.tweets.count %>

    -

    Following

    -

    Follower

    -

    Likes

    +

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    +

    <%= link_to "Following", followings_user_path(@user) %>

    +

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Likes", likes_user_path(@user) %>

    diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb new file mode 100644 index 000000000..3e2594b98 --- /dev/null +++ b/app/views/users/followers.html.erb @@ -0,0 +1,66 @@ +
    +
    +
    +
    + <%= image_tag @user.avatar, size: "250" %> +
    + +
    +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    +

    <%= link_to "Following", followings_user_path(@user) %>

    +

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Likes", likes_user_path(@user) %>

    +
    + +
    + <% if @user != current_user %> + <% if current_user.following?(@user)%> + <%= link_to 'Unfollow', followship_path(@user), method: :delete, class: "btn btn-default" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default" %> + <% end %> + <% end %> +
    + +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    + +
    +
    + +
    + <% @users.each do |user| %> +
    +
    +
    + <%= image_tag user.avatar, size: "100" %> +
    +
    +

    <%= link_to user.name, tweets_user_path(user) %>

    +

    <%= truncate(user.introduction, length: 140) %>

    +
    + <% if user != current_user %> + <% if current_user.following?(user)%> + <%= link_to 'Unfollow', followship_path(user.id), method: :delete, class: "btn btn-default pull-right" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: user.id), method: :post, class: "btn btn-default pull-right" %> + <% end %> + <% end %> +
    +
    +
    +
    + <% end %> + +
    + +
    +
    diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb new file mode 100644 index 000000000..3e2594b98 --- /dev/null +++ b/app/views/users/followings.html.erb @@ -0,0 +1,66 @@ +
    +
    +
    +
    + <%= image_tag @user.avatar, size: "250" %> +
    + +
    +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    +

    <%= link_to "Following", followings_user_path(@user) %>

    +

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Likes", likes_user_path(@user) %>

    +
    + +
    + <% if @user != current_user %> + <% if current_user.following?(@user)%> + <%= link_to 'Unfollow', followship_path(@user), method: :delete, class: "btn btn-default" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default" %> + <% end %> + <% end %> +
    + +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    + +
    +
    + +
    + <% @users.each do |user| %> +
    +
    +
    + <%= image_tag user.avatar, size: "100" %> +
    +
    +

    <%= link_to user.name, tweets_user_path(user) %>

    +

    <%= truncate(user.introduction, length: 140) %>

    +
    + <% if user != current_user %> + <% if current_user.following?(user)%> + <%= link_to 'Unfollow', followship_path(user.id), method: :delete, class: "btn btn-default pull-right" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: user.id), method: :post, class: "btn btn-default pull-right" %> + <% end %> + <% end %> +
    +
    +
    +
    + <% end %> + +
    + +
    +
    diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb new file mode 100644 index 000000000..a50785a5d --- /dev/null +++ b/app/views/users/likes.html.erb @@ -0,0 +1,70 @@ +
    +
    +
    +
    + <%= image_tag @user.avatar, size: "250" %> +
    + +
    +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +
    + +
    +

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    +

    <%= link_to "Following", followings_user_path(@user) %>

    +

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Likes", likes_user_path(@user) %>

    +
    + +
    + <% if @user != current_user %> + <% if current_user.following?(@user)%> + <%= link_to 'Unfollow', followship_path(@user), method: :delete, class: "btn btn-default" %> + <% else %> + <%= link_to 'Follow',followships_path(following_id: @user), method: :post, class: "btn btn-default" %> + <% end %> + <% end %> +
    + +
    + <% if @user.email == current_user.email %> + <%= link_to 'Edit Profile', edit_user_path, class:"btn btn-primary" %> + <% end %> +
    + +
    +
    + +
    + <% @likes.each do |tweet| %> +
    +
    +
    + <%= image_tag tweet.user.avatar, size: "100" if tweet.user.avatar? %> +
    +
    +

    + <%= link_to tweet.user.name, tweets_user_path(tweet.user) %> + <%= tweet.created_at.strftime("%Y-%m-%d, %H:%M") %> +

    +

    + <%= tweet.description %> +

    +
    + <%= link_to 'Reply', tweet_replies_path(tweet), class:"links links-blue" %> + <% if tweet.is_liked?(current_user) %> + <%= link_to "Unlike(#{tweet.likes_count})", unlike_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% else %> + <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet.id), method: :post, class: "links links-red" %> + <% end %> +
    +
    +
    +
    + <% end %> + +
    + +
    +
    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index e6de4d216..307da5c8e 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -11,10 +11,10 @@
    -

    Tweets <%= @user.tweets.count %>

    -

    Following

    -

    Follower <%= @user.followers_count %>

    -

    Likes

    +

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    +

    <%= link_to "Following", followings_user_path(@user) %>

    +

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Likes", likes_user_path(@user) %>

    From 789c89ddd24a77d642513818d9770a11b3c7d52f Mon Sep 17 00:00:00 2001 From: Allie Date: Thu, 2 Aug 2018 04:26:15 +0800 Subject: [PATCH 50/51] fix like_count --- app/controllers/admin/users_controller.rb | 1 - app/controllers/users_controller.rb | 3 ++- app/models/like.rb | 2 +- app/models/tweet.rb | 6 +----- app/models/user.rb | 8 ++++++++ app/views/replies/index.html.erb | 2 +- app/views/users/followers.html.erb | 2 +- app/views/users/followings.html.erb | 2 +- app/views/users/likes.html.erb | 2 +- app/views/users/tweets.html.erb | 2 +- 10 files changed, 17 insertions(+), 13 deletions(-) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 61097d8b1..1cb09b046 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,5 @@ class Admin::UsersController < Admin::BaseController def index @users = User.all.order(tweets_count: :desc) - @tweets = Tweet.all.order(likes_count: :desc) end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 78cc0731b..b6f7c5f84 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -36,12 +36,13 @@ def followings def followers @followers # 基於測試規格,必須講定變數名稱 @user = User.find(params[:id]) - @users = @user.followships.all + @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 diff --git a/app/models/like.rb b/app/models/like.rb index 42d487577..0d1910145 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,4 +1,4 @@ class Like < ApplicationRecord - belongs_to :user + belongs_to :user, counter_cache: true belongs_to :tweet, counter_cache: true end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 46d922edf..92a04900e 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -2,17 +2,13 @@ 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 count_likes - self.likes_count = self.like.size - self.save - end def is_liked?(tweet) self.liked_tweets.include?(tweet) diff --git a/app/models/user.rb b/app/models/user.rb index 009aac994..389d7078b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,6 +8,11 @@ def admin? self.role == "admin" end + def count_likes + self.likes_count = self.likes.size + self.save + end + mount_uploader :avatar, AvatarUploader # 需要 app/views/devise 裡找到樣板,加上 name 屬性 @@ -26,6 +31,9 @@ def admin? has_many :followships, dependent: :destroy has_many :followings, through: :followships + has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" + has_many :followers, through: :inverse_followships, source: :user + def is_liked?(tweet) self.liked_tweets.include?(tweet) end diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 1d0d6e15c..5e7db822d 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -15,7 +15,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    -

    <%= link_to "Likes", likes_user_path(@user) %>

    +

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb index 3e2594b98..5987b5419 100644 --- a/app/views/users/followers.html.erb +++ b/app/views/users/followers.html.erb @@ -14,7 +14,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    -

    <%= link_to "Likes", likes_user_path(@user) %>

    +

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb index 3e2594b98..5987b5419 100644 --- a/app/views/users/followings.html.erb +++ b/app/views/users/followings.html.erb @@ -14,7 +14,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    -

    <%= link_to "Likes", likes_user_path(@user) %>

    +

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb index a50785a5d..f86bf877e 100644 --- a/app/views/users/likes.html.erb +++ b/app/views/users/likes.html.erb @@ -14,7 +14,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    -

    <%= link_to "Likes", likes_user_path(@user) %>

    +

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 307da5c8e..9456e2448 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -14,7 +14,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    -

    <%= link_to "Likes", likes_user_path(@user) %>

    +

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    From fac432e7a94e8310e5bc95e72e2a260ac79b4443 Mon Sep 17 00:00:00 2001 From: Allie Date: Thu, 2 Aug 2018 04:47:58 +0800 Subject: [PATCH 51/51] fix --- app/views/replies/index.html.erb | 2 +- app/views/users/followers.html.erb | 2 +- app/views/users/followings.html.erb | 2 +- app/views/users/likes.html.erb | 2 +- app/views/users/tweets.html.erb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 5e7db822d..52c66fb3d 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -14,7 +14,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    -

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Follower", followers_user_path(@user) %>

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb index 5987b5419..d4bf84c3f 100644 --- a/app/views/users/followers.html.erb +++ b/app/views/users/followers.html.erb @@ -13,7 +13,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    -

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Follower", followers_user_path(@user) %>

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb index 5987b5419..d4bf84c3f 100644 --- a/app/views/users/followings.html.erb +++ b/app/views/users/followings.html.erb @@ -13,7 +13,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    -

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Follower", followers_user_path(@user) %>

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb index f86bf877e..6050502be 100644 --- a/app/views/users/likes.html.erb +++ b/app/views/users/likes.html.erb @@ -13,7 +13,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    -

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Follower", followers_user_path(@user) %>

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>

    diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 9456e2448..1dfda4d31 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -13,7 +13,7 @@

    <%= link_to "Tweets #{@user.tweets.count}", tweets_user_path(@user) %>

    <%= link_to "Following", followings_user_path(@user) %>

    -

    <%= link_to "Follower #{@user.followers_count}", followers_user_path(@user) %>

    +

    <%= link_to "Follower", followers_user_path(@user) %>

    <%= link_to "Likes #{@user.likes_count}", likes_user_path(@user) %>