From 3febaff8a8c9833401148ab7263a32cabb128eda Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 20 Jul 2018 17:21:05 +0800 Subject: [PATCH 001/100] set up routes and index page for admin and public user --- app/views/admin/tweets/index.html.erb | 1 + app/views/tweets/index.html.erb | 1 + config/routes.rb | 7 ++++++- 3 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 app/views/admin/tweets/index.html.erb create mode 100644 app/views/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..6a0f94f18 --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1 @@ +

tweeds 後台

\ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb new file mode 100644 index 000000000..caa2fe653 --- /dev/null +++ b/app/views/tweets/index.html.erb @@ -0,0 +1 @@ +

tweeds前台

\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 90856d4fe..b7054fc91 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,12 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users - # 請依照專案指定規格來設定路由 + # 設定前台路由 + root "tweets#index" + # 設定後台路由 + namespace :admin do + root "tweets#index" + end end From 95b010b4da969a25c0756c3ec5790f0831d52907 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 23 Jul 2018 13:00:06 +0800 Subject: [PATCH 002/100] add login process --- app/controllers/tweets_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..0e59ee1f6 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,5 +1,6 @@ class TweetsController < ApplicationController - +# 登入認證 +before_action :authenticate_user! def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end From cda185cc35627425b4b51cfab63a1517792253d9 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 23 Jul 2018 13:03:19 +0800 Subject: [PATCH 003/100] add user name to registration form --- app/controllers/application_controller.rb | 10 ++++++++++ app/views/devise/registrations/edit.html.erb | 5 +++++ app/views/devise/registrations/new.html.erb | 5 ++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..8c3c00c91 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,17 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :configure_permitted_parameters, if: :devise_controller? # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # https://github.com/plataformatec/devise#strong-parameters # 注意有 sign_up 和 account_update 兩種參數要處理 + + protected + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) + devise_parameter_sanitizer.permit(:account_update, keys: [:name]) + + end + end diff --git a/app/views/devise/registrations/edit.html.erb b/app/views/devise/registrations/edit.html.erb index 10ed32a9e..20cf3c961 100644 --- a/app/views/devise/registrations/edit.html.erb +++ b/app/views/devise/registrations/edit.html.erb @@ -3,6 +3,11 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= devise_error_messages! %> + + <%= f.label :name %>
+ <%= f.text_field :name, autofocus: true %> +
+
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: "email" %> diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 602803cff..4a68e9ece 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -2,7 +2,10 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> - +
+ <%= f.label :name %>
+ <%= f.text_field :name, autofocus: true %> +
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: "email" %> From 267e6f38a4254e8a19dc24e28696e9c0751c8e59 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 23 Jul 2018 15:30:59 +0800 Subject: [PATCH 004/100] add admin log in process and CRUD --- app/controllers/admin/tweets_controller.rb | 3 +++ app/controllers/application_controller.rb | 8 ++++++++ app/models/user.rb | 6 +++++- app/views/layouts/application.html.erb | 7 ++++--- config/routes.rb | 1 + 5 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..4a6d380f3 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,4 +1,7 @@ class Admin::TweetsController < Admin::BaseController +# 後台登入先確認user登入再確認身份為admin,authenticate_admin方法要另外定義 +before_action :authenticate_user! +before_action :authenticate_admin def index end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 8c3c00c91..504a73a96 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,7 +11,15 @@ class ApplicationController < ActionController::Base def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) devise_parameter_sanitizer.permit(:account_update, keys: [:name]) + end + + private + def authenticate_admin + unless current_user.admin? #admin?要另外在user model定義 + flash[:alert] = "Not allow!" + redirect_to root_path + end end end diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..32321bb58 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,11 @@ class User < ApplicationRecord # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 - validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + validates_presence_of :name, :uniqueness => {:case_sensitive => false} + +def admin? + self.role == "admin" +end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 952cb7a1b..6de59d26c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,12 +10,13 @@ <% if current_user %> - <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_restaurants_path %>
  • + <% if current_user.admin? %> +
  • <%= link_to 'Admin 後台', admin_tweets_path %>
  • <% end %> +
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • +
  • <%#link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • diff --git a/config/routes.rb b/config/routes.rb index b7054fc91..345cb9d1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,6 +7,7 @@ # 設定後台路由 namespace :admin do + resources :tweets root "tweets#index" end end From 6edfdcec46d7b43c22f0ec65b7f3088e89251f09 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 23 Jul 2018 16:40:31 +0800 Subject: [PATCH 005/100] setup route and link for user profile page --- app/views/layouts/application.html.erb | 4 ++-- config/routes.rb | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6de59d26c..22eec874b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -3,7 +3,6 @@ SimpleTwitter <%= csrf_meta_tags %> - <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> @@ -15,8 +14,9 @@ <% end %>
  • +
  • <%= link_to 'Profile', user_path(current_user) %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%#link_to('修改個人資料', edit_user_path(current_user)) %>
  • +
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • diff --git a/config/routes.rb b/config/routes.rb index 345cb9d1c..7af21c24a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,4 +10,7 @@ resources :tweets root "tweets#index" end + # 設定user路由 + resources :users, only: [:show, :edit, :update] + end From 289530e43666a445a2e23c39630731425cd512a1 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 24 Jul 2018 11:56:19 +0800 Subject: [PATCH 006/100] add tweet index action and view fo admin --- app/controllers/admin/tweets_controller.rb | 1 + app/views/admin/tweets/index.html.erb | 11 ++++++++++- config/routes.rb | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 4a6d380f3..10e19d643 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -3,6 +3,7 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_user! before_action :authenticate_admin def index + @tweets = Tweet.all #管理者瀏覽所有tweet資料 end def destroy diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 6a0f94f18..a69fbf018 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1 +1,10 @@ -

    tweeds 後台

    \ No newline at end of file +

    tweets 後台

    +
      + <% @tweets.each do |tweet| %> +
    • + <%= tweets.name %> + <%= link_to 'Show', admin_tweet_path(tweet) %> + <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %> +
    • + <% end %> +
    diff --git a/config/routes.rb b/config/routes.rb index 7af21c24a..b14e333db 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ # 設定後台路由 namespace :admin do - resources :tweets + resources :tweets, only: [:index, :destroy] root "tweets#index" end # 設定user路由 From 0d4ea948975a0ef6bf6f862a5ba87c5c71931b77 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 24 Jul 2018 12:19:45 +0800 Subject: [PATCH 007/100] setup tweet resources for public users --- config/routes.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index b14e333db..a35b34186 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -13,4 +13,7 @@ # 設定user路由 resources :users, only: [:show, :edit, :update] + # 設定前台使用者行為路由 + resources :tweets, only: [:index, :create, :new] + end From a3677d79ff4f77f7630a50d2304dd4df6a609d3b Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 24 Jul 2018 14:39:03 +0800 Subject: [PATCH 008/100] link user and tweet model --- app/models/tweet.rb | 3 ++- app/models/user.rb | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6715fada2..600dff3d5 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,4 +1,5 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 - +# 建立user跟tweet的關聯,一個tweet只會有一個user + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index 32321bb58..8f58cc9c3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -6,6 +6,9 @@ class User < ApplicationRecord mount_uploader :avatar, AvatarUploader + # 設定user跟tweet關聯,一個user可以有很多tweets + has_many :tweets + # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) From 20e59a08fbf0c81bb7998042d6685aaea9215fa8 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 24 Jul 2018 21:36:56 +0800 Subject: [PATCH 009/100] user can tweet on tweet's index page --- app/controllers/tweets_controller.rb | 16 ++++++++++++++++ app/views/tweets/index.html.erb | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 0e59ee1f6..c73abf056 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -3,9 +3,19 @@ class TweetsController < ApplicationController before_action :authenticate_user! def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @tweets = Tweet.all + @tweet = current_user.tweets.build #在index建立tweet容器來建立短文 end def create + @tweet = current_user.tweets.build(tweet_params) + if @tweet.save + flash[:notice] = "Tweeted successfully" + redirect_to tweets_path + else + flash.now[:alert] = "Tweeted faield" + render :index + end end def like @@ -14,4 +24,10 @@ def like def unlike end + private + + def tweet_params + params.require(:tweet).permit(:content) + end + end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index caa2fe653..0998dcb4a 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1 +1,14 @@ -

    tweeds前台

    \ No newline at end of file +

    tweets前台

    +
    +
    + <%= 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 %> + +
    +
    From 349bd05d9feba49dbb1bf90ffef570149e7f512b Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 24 Jul 2018 22:28:52 +0800 Subject: [PATCH 010/100] install bootstrap-sass and setup scss and gem 'jquery-rails' for bootstrap js --- Gemfile | 3 +++ Gemfile.lock | 13 ++++++++++++- app/assets/javascripts/application.js | 2 ++ .../{application.css => application.scss} | 4 ++-- 4 files changed, 19 insertions(+), 3 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (92%) diff --git a/Gemfile b/Gemfile index c1f1ee3e2..14b4f5ce5 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,10 @@ gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets +gem 'bootstrap-sass', '~> 3.3.7' gem 'sass-rails', '~> 5.0' +# For bootstrap +gem 'jquery-rails' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # See https://github.com/rails/execjs#readme for more supported runtimes diff --git a/Gemfile.lock b/Gemfile.lock index e55e9522f..e300d6b8f 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.3 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..ff430067c 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -10,6 +10,8 @@ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details // about supported directives. // +//= require jquery +//= require bootstrap-sprockets //= require rails-ujs //= require turbolinks //= require_tree . 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..fada726e5 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,6 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * - *= require_tree . - *= require_self */ + @import "bootstrap-sprockets"; + @import "bootstrap"; From 0c4cde8837f71e9cea80a018fd509a622dbd9104 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 01:09:55 +0800 Subject: [PATCH 011/100] modify log in and sign up page css --- app/views/devise/registrations/new.html.erb | 2 ++ app/views/devise/sessions/new.html.erb | 2 ++ app/views/devise/shared/_links.html.erb | 4 ++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 4a68e9ece..b7b06c6a2 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -1,3 +1,4 @@ +

    Sign up

    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> @@ -30,3 +31,4 @@ <% end %> <%= render "devise/shared/links" %> +
    \ No newline at end of file diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 3ebb001d1..2c3e0647f 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -1,3 +1,4 @@ +

    Log in

    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %> @@ -24,3 +25,4 @@ <% end %> <%= render "devise/shared/links" %> +
    \ No newline at end of file diff --git a/app/views/devise/shared/_links.html.erb b/app/views/devise/shared/_links.html.erb index e6a3e4196..73627e158 100644 --- a/app/views/devise/shared/_links.html.erb +++ b/app/views/devise/shared/_links.html.erb @@ -1,9 +1,9 @@ <%- if controller_name != 'sessions' %> - <%= link_to "Log in", new_session_path(resource_name) %>
    + <%# link_to "Log in", new_session_path(resource_name) %>
    <% end -%> <%- if devise_mapping.registerable? && controller_name != 'registrations' %> - <%= link_to "Sign up", new_registration_path(resource_name) %>
    + <%# link_to "Sign up", new_registration_path(resource_name) %>
    <% end -%> <%- if devise_mapping.recoverable? && controller_name != 'passwords' && controller_name != 'registrations' %> From 4f10c71c07f8b027975bcadbac5dca8bd23a9889 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 08:11:56 +0800 Subject: [PATCH 012/100] add css for tweet index page --- app/views/layouts/application.html.erb | 46 +++++++++++++++++--------- app/views/tweets/index.html.erb | 1 - 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 22eec874b..92b16c77a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,22 +8,38 @@ - <% if current_user %> - <% if current_user.admin? %> -
  • <%= link_to 'Admin 後台', admin_tweets_path %>
  • - <% end %> +

    <%= notice %>

    +

    <%= alert %>

    + + <%= yield %> diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 0998dcb4a..d6377660d 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,4 +1,3 @@ -

    tweets前台

    <%= form_for @tweet do |f| %> From bd4b5f6e834c5b0b980f136ab0d20daa71deeddb Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 08:54:07 +0800 Subject: [PATCH 013/100] add notice and alert css for log in and sign in page --- app/views/layouts/application.html.erb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 92b16c77a..4bdccf85d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -8,8 +8,7 @@ -

    <%= notice %>

    -

    <%= alert %>

    +
    + + + <% if flash[:notice] %> +
    +
    +
    <%= notice %>
    +
    +
    + <% end %> + + <% if flash[:alert] %> +
    +
    +
    <%= alert %>
    +
    +
    + <% end %> + <%= yield %> From 246812b0f0d3303cddeb7182489445abf45a4544 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 09:15:45 +0800 Subject: [PATCH 014/100] add limit the number of words (140) in tweet and cannot be blank --- app/models/tweet.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 600dff3d5..b552e6437 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -2,4 +2,8 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 # 建立user跟tweet的關聯,一個tweet只會有一個user belongs_to :user +# tweet限制字數及空白 + validates :user_id, presence: true + validates :description, presence: true, length: {maximum: 140} + end From 3ed66ab3ea7908ef8d909948df6e73c9c44796f3 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 09:16:24 +0800 Subject: [PATCH 015/100] modify tweets --- app/controllers/tweets_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c73abf056..d9d67c020 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -27,7 +27,7 @@ def unlike private def tweet_params - params.require(:tweet).permit(:content) + params.require(:tweet).permit(:description) end end From 9bcd72589badbab821ec9d443d68bb68b026b216 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 10:33:37 +0800 Subject: [PATCH 016/100] add tweet content on tweet index page --- app/views/tweets/index.html.erb | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d6377660d..321b1cfef 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,3 +1,4 @@ +
    <%= form_for @tweet do |f| %> @@ -8,6 +9,20 @@ <%= f.submit "Tweet", class: "btn btn-primary" %>
    <% end %> -
    + +
    +
    + <% @tweets.each do |tweet| %> +
    + <%= image_tag tweet.user.avatar %> +
    +
    + <%= tweet.user.name %>, + <%= tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> +

    <%= tweet.description %>

    +
    + <% end %> +
    +
    \ No newline at end of file From 292b59776a5e4492c170495fd1f29fa470e70150 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 10:55:33 +0800 Subject: [PATCH 017/100] add style css --- app/assets/stylesheets/application.scss | 5 +++-- app/assets/stylesheets/style.scss | 8 ++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 app/assets/stylesheets/style.scss diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index fada726e5..c0378dfd3 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -11,5 +11,6 @@ * It is generally better to create a new file per style scope. * */ - @import "bootstrap-sprockets"; - @import "bootstrap"; + @import 'bootstrap-sprockets'; + @import 'bootstrap'; + @import 'style'; diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss new file mode 100644 index 000000000..3bdca7a4c --- /dev/null +++ b/app/assets/stylesheets/style.scss @@ -0,0 +1,8 @@ +//攥寫自己的css. +.tweet-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 4px; + margin-bottom: 20px; + +} \ No newline at end of file From b68a53931ae02280ab878b2311472019e0fb80c1 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 10:56:53 +0800 Subject: [PATCH 018/100] modify tweets content css in index page --- app/views/tweets/index.html.erb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 321b1cfef..17f3c0a6f 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -13,8 +13,8 @@
    -
    - <% @tweets.each do |tweet| %> + <% @tweets.each do |tweet| %> +
    <%= image_tag tweet.user.avatar %>
    @@ -23,6 +23,6 @@ <%= tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %>

    <%= tweet.description %>

    - <% end %> -
    +
    + <% end %>
    \ No newline at end of file From e4c1c0821a037451a3f1c041e56bfece8c44a58c Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 13:07:31 +0800 Subject: [PATCH 019/100] setup user edit user's info routes --- config/routes.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index a35b34186..62197d742 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,9 +11,8 @@ root "tweets#index" end # 設定user路由 - resources :users, only: [:show, :edit, :update] - + resources :users, only: [:edit, :update] # 設定前台使用者行為路由 - resources :tweets, only: [:index, :create, :new] + resources :tweets, only: [:index, :create, :new] end From 852abd509d5f219ec7e88d5ac8383e5000f8955a Mon Sep 17 00:00:00 2001 From: Cynthia Date: Wed, 25 Jul 2018 18:15:07 +0800 Subject: [PATCH 020/100] setup user edit and update controller --- app/controllers/users_controller.rb | 17 ++++++++++++++++- app/models/user.rb | 1 + 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 750e3c6b5..0088b8a26 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,12 +1,17 @@ class UsersController < ApplicationController - +before_action :set_user, only: [:edit, :update] def tweets end def edit + unless @user == current_user + redirect_to edit_user_path(@user) + end end def update + @user.update(user_params) + redirect_to edit_user_path(@user) end def followings @@ -21,4 +26,14 @@ def likes @likes # 基於測試規格,必須講定變數名稱 end + private + + def set_user + @user = User.find(params[:id]) + end + + def user_params + params.require(:user).permit(:name, :avatar, :introduction) + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 8f58cc9c3..b0ef63406 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,7 @@ class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + # user大頭照照片上傳器 mount_uploader :avatar, AvatarUploader # 設定user跟tweet關聯,一個user可以有很多tweets From 733463a186dc62f173afacba9fcfaf37db7b347e Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 06:32:07 +0800 Subject: [PATCH 021/100] add user editprofile page --- app/views/users/edit.html.erb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/views/users/edit.html.erb diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..de295723c --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,33 @@ +
    +
    + <%= form_for @user do |f| %> + +
    +
    + <% if @user.avatar? %> + <%= image_tag @user.avatar, class: "avatar-image img-responsive center-block" %> + <% else %> + + <% end %> +
    +
    + <%= f.file_field :avatar %> +
    +
    + +
    +
    + <%= f.text_field :name, placeholder: "name" %> +
    +
    + <%= f.text_area :introduction, placeholder: "Type anything about you here....",class: "form-control" %> +
    + +
    + <%= f.submit "Update", class: "btn btn-primary" %> +
    +
    +
    + <% end %> +
    +
    From f732ef6fccdd3dc94441cea89f4736d4d126959e Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 06:32:42 +0800 Subject: [PATCH 022/100] add edit profile page btn css --- app/assets/stylesheets/style.scss | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 3bdca7a4c..3c7de9e9e 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -5,4 +5,9 @@ padding: 4px; margin-bottom: 20px; +} + +//edit page按鈕 +.update-btn { + float:right; } \ No newline at end of file From 2540cfd00b64b2dd77274fbd260927c942d67ad4 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 06:48:38 +0800 Subject: [PATCH 023/100] reset routes for admin --- config/routes.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index 62197d742..97775d0ae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,8 @@ # 設定後台路由 namespace :admin do - resources :tweets, only: [:index, :destroy] + resources :tweets, only: [:index, :destroy] + resources :users, only: [:index] root "tweets#index" end # 設定user路由 From e4f8951bedf0b83f611692a30525ea9cae7ecad6 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 07:50:15 +0800 Subject: [PATCH 024/100] modify tweet index page css --- app/views/tweets/index.html.erb | 51 ++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 17f3c0a6f..57c8787c5 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,28 +1,39 @@ - +
    - <%= 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" %> + +
    + <%= 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| %> +
    +
    +
    + <%= image_tag tweet.user.avatar %> +
    +
    + <%= tweet.user.name %>, + <%= tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> +

    <%= tweet.description %>

    +
    +
    +
    + <% end %>
    - <% end %>
    - +
    - <% @tweets.each do |tweet| %> -
    -
    - <%= image_tag tweet.user.avatar %> -
    -
    - <%= tweet.user.name %>, - <%= tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> -

    <%= tweet.description %>

    -
    +
    +
    +
    - <% end %> +
    \ No newline at end of file From 355bfd3e6355d50acbd207ce4db70c1a1b5c3cbc Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 08:49:48 +0800 Subject: [PATCH 025/100] add tweets replies route --- config/routes.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 97775d0ae..6f031118c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ # 設定user路由 resources :users, only: [:edit, :update] # 設定前台使用者行為路由 - resources :tweets, only: [:index, :create, :new] - + resources :tweets, only: [:index, :create] do + resources :replies, only: [:index, :create] + end end From edcbe5c577e3ff58925ddf203af5080c46356f32 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 10:45:36 +0800 Subject: [PATCH 026/100] link tweet and reply model --- app/models/reply.rb | 1 + app/models/tweet.rb | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/reply.rb b/app/models/reply.rb index bae6f9463..ea6ff7274 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,2 +1,3 @@ class Reply < ApplicationRecord + belongs_to :tweet end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index b552e6437..3fbfd7971 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -5,5 +5,6 @@ class Tweet < ApplicationRecord # tweet限制字數及空白 validates :user_id, presence: true validates :description, presence: true, length: {maximum: 140} - +# tweet有很多回覆,當tweet被刪除時,順便刪除回覆 + has_many :replies, dependent: :destroy end From d3f8c3fbd63ae9dc0e65f8dc803a74b85606a028 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 11:13:26 +0800 Subject: [PATCH 027/100] modify avatar image css --- app/assets/stylesheets/style.scss | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 3c7de9e9e..74d75ed13 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -10,4 +10,9 @@ //edit page按鈕 .update-btn { float:right; +} + +.avatar-image { + height: 100px; + width: auto; } \ No newline at end of file From 489a9ed76c9b56c9854095f741a255b01a917b51 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 16:36:12 +0800 Subject: [PATCH 028/100] user can add replies on tweet index page --- app/controllers/replies_controller.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index a9b6a315b..444cf394a 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,9 +1,22 @@ class RepliesController < ApplicationController def index + @tweet = Tweet.find(params[:tweet_id]) + @reply = Reply.new end def create + @tweet = Tweet.find(params[:tweet_id]) + @reply = @tweet.replies.build(reply_params) + @reply.user_id = current_user.id + @reply.save! + redirect_to tweet_replies_path(@tweet) + end + + private + + def reply_params + params.require(:reply).permit(:comment) end end From d13adc636e89b00fa89eb3ba9b12559747c59535 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 18:31:27 +0800 Subject: [PATCH 029/100] add user avatar partial and modify user edit page --- app/views/shared/_avatar.html.erb | 5 +++++ app/views/users/edit.html.erb | 6 +----- 2 files changed, 6 insertions(+), 5 deletions(-) create mode 100644 app/views/shared/_avatar.html.erb diff --git a/app/views/shared/_avatar.html.erb b/app/views/shared/_avatar.html.erb new file mode 100644 index 000000000..0883dbbb0 --- /dev/null +++ b/app/views/shared/_avatar.html.erb @@ -0,0 +1,5 @@ +<% if current_user.avatar? %> + <%= image_tag current_user.avatar, class: "avatar-image img-responsive center-block" %> +<% else %> + +<% end %> \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index de295723c..1f0213310 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -4,11 +4,7 @@
    - <% if @user.avatar? %> - <%= image_tag @user.avatar, class: "avatar-image img-responsive center-block" %> - <% else %> - - <% end %> + <%= render partial: "shared/avatar" %>
    <%= f.file_field :avatar %> From 45f0f807633c48a3721252030aa77d60739cc56a Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 18:33:10 +0800 Subject: [PATCH 030/100] add tweets user avatar css in index page --- app/views/tweets/index.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 57c8787c5..4388179a0 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -16,7 +16,7 @@
    - <%= image_tag tweet.user.avatar %> + <%= image_tag tweet.user.avatar, class: "avatar-image img-responsive center-block"%>
    <%= tweet.user.name %>, From 572f1864a247a2bc01bb5209c5884f881b47bf8b Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 21:43:24 +0800 Subject: [PATCH 031/100] add reply and user relationship --- app/models/reply.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/models/reply.rb b/app/models/reply.rb index ea6ff7274..223589f44 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,3 +1,4 @@ class Reply < ApplicationRecord + belongs_to :user belongs_to :tweet end From eff1fda33d66357a6e5f5f1b8ddc2954229af835 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 21:47:29 +0800 Subject: [PATCH 032/100] add reply index page --- app/views/replies/index.html.erb | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 app/views/replies/index.html.erb diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb new file mode 100644 index 000000000..69d421e9c --- /dev/null +++ b/app/views/replies/index.html.erb @@ -0,0 +1,79 @@ + +
    +
    +
    + +
    + <%= image_tag @tweet.user.avatar, class: "avatar-image img-responsive" %> +
    + +
    +

    <%= @tweet.user.name %>

    +
    + +
    + <%= @tweet.user.introduction %> +
    + + + + + +
    + <%= link_to 'Edit Profile',edit_user_path(@tweet.user_id), class: "btn btn-primary" %> +
    +
    + + +
    +
    +
    + +

    Tweet

    +
    +
    +
    +
    + <%= image_tag @tweet.user.avatar, class: "avatar-image img-responsive img-rounded" %> +
    +
    + <%= @tweet.user.name %>, + <%= @tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> +

    <%= @tweet.description %>

    +
    +
    +
    +
    +
    + +

    Replies

    +
    + <% @tweet.replies.each do |reply| %> +
    +
    +
    + <%= image_tag reply.user.avatar, class: "avatar-image img-responsive img-rounded" %> +
    +
    + <%= reply.user.name %>, + <%= reply.user.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> +

    + <%= simple_format reply.comment %> +

    +
    +
    +
    + <% end %> +
    + + <%= form_for [@tweet, @reply] do |f| %> +
    +
    + <%= f.text_area :comment, class: "form-control" %> +
    +
    + <%= f.submit class: "btn btn-primary" %> +
    +
    + <% end %> +
    From de134fbf5b74230b7addb0ed8a8840e83739cc28 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 22:17:37 +0800 Subject: [PATCH 033/100] setup self-referential relationships within users table through followships --- app/models/followship.rb | 3 ++- app/models/user.rb | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/models/followship.rb b/app/models/followship.rb index 1aed01396..583c20e67 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,4 +1,5 @@ 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 b0ef63406..16a04b236 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,6 +10,10 @@ class User < ApplicationRecord # 設定user跟tweet關聯,一個user可以有很多tweets has_many :tweets + # 設定追蹤關聯紀錄 + has_many :followships, dependent: :destroy + has_many :followings, through: :followships + # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) From c2ffc56c7b99f0e9b460d5eee045e7fab3ec88c5 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 22:25:36 +0800 Subject: [PATCH 034/100] setup routes for user index page and follow/unfollow function --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 6f031118c..facb7abac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,4 +17,6 @@ resources :tweets, only: [:index, :create] do resources :replies, only: [:index, :create] end + # 設定追蹤路由 + resources :followships, only: [:create, :destroy] end From a15716e9256be057f197554045feca25d5d8cf05 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Thu, 26 Jul 2018 22:49:29 +0800 Subject: [PATCH 035/100] setup routes for user's profile and tweets pages --- config/routes.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index facb7abac..99a699958 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,8 +11,12 @@ resources :users, only: [:index] root "tweets#index" end - # 設定user路由 - resources :users, only: [:edit, :update] + # 設定user路由及某user的簡介及推播 + resources :users, only: [:edit, :update] do + member do + get :tweets + end + end # 設定前台使用者行為路由 resources :tweets, only: [:index, :create] do resources :replies, only: [:index, :create] From 8fe85741df96319637c5389111f2589938bb7dc2 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 10:03:38 +0800 Subject: [PATCH 036/100] modify user avatar partial and replies and tweets and user edit index page --- app/controllers/replies_controller.rb | 1 + app/views/replies/index.html.erb | 2 +- app/views/shared/_avatar.html.erb | 4 ++-- app/views/tweets/index.html.erb | 3 ++- app/views/users/edit.html.erb | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 444cf394a..65213a73b 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 69d421e9c..71f4b8c89 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -4,7 +4,7 @@
    - <%= image_tag @tweet.user.avatar, class: "avatar-image img-responsive" %> + <%= render partial: "shared/avatar", locals: {user: @user} %>
    diff --git a/app/views/shared/_avatar.html.erb b/app/views/shared/_avatar.html.erb index 0883dbbb0..acfe7fc3d 100644 --- a/app/views/shared/_avatar.html.erb +++ b/app/views/shared/_avatar.html.erb @@ -1,5 +1,5 @@ -<% if current_user.avatar? %> - <%= image_tag current_user.avatar, class: "avatar-image img-responsive center-block" %> +<% if user.avatar? %> + <%= image_tag user.avatar, class: "avatar-image img-responsive center-block" %> <% else %> <% end %> \ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 4388179a0..d2797f25e 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -16,7 +16,8 @@
    - <%= image_tag tweet.user.avatar, class: "avatar-image img-responsive center-block"%> + <%# image_tag tweet.user.avatar, class: "avatar-image img-responsive center-block" %> + <%= render partial: "shared/avatar", locals: {user: tweet.user}%>
    <%= tweet.user.name %>, diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 1f0213310..0caa797e8 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -4,7 +4,7 @@
    - <%= render partial: "shared/avatar" %> + <%= render partial: "shared/avatar", locals: {user: @user} %>
    <%= f.file_field :avatar %> From 3c808aca3a993218be8b49344f47e059b06a1a3c Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 16:29:45 +0800 Subject: [PATCH 037/100] modify avatar partial page --- app/views/shared/_avatar.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_avatar.html.erb b/app/views/shared/_avatar.html.erb index acfe7fc3d..0bf31e2f1 100644 --- a/app/views/shared/_avatar.html.erb +++ b/app/views/shared/_avatar.html.erb @@ -1,5 +1,5 @@ -<% if user.avatar? %> - <%= image_tag user.avatar, class: "avatar-image img-responsive center-block" %> +<% if user_avatar.avatar? %> + <%= image_tag user_avatar.avatar, class: "avatar-image img-responsive center-block" %> <% else %> <% end %> \ No newline at end of file From 0a71a4fef5ed077be49e203381e538b85ce68e43 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 16:30:33 +0800 Subject: [PATCH 038/100] add tweeter partial page --- app/views/shared/_tweeter.html.erb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/views/shared/_tweeter.html.erb diff --git a/app/views/shared/_tweeter.html.erb b/app/views/shared/_tweeter.html.erb new file mode 100644 index 000000000..a3e2729c7 --- /dev/null +++ b/app/views/shared/_tweeter.html.erb @@ -0,0 +1,16 @@ +
    +
    +
    +
    + <%= render partial: "shared/avatar", locals: {user_avatar: tweeter.user}%> +
    +
    + <%= tweeter.user.name %>, + <%= tweeter.created_at.strftime('%Y-%m-%d, %H:%M:%S') %> +

    + <%= tweeter.description %> +

    +
    +
    +
    +
    From 16da6cb5db5dc8b310ad67e6e3dcbad572225576 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 16:34:28 +0800 Subject: [PATCH 039/100] modify avatar partial on replies and user edit page and add tweeter partia on tweets index page --- app/views/replies/index.html.erb | 17 ++--------------- app/views/tweets/index.html.erb | 21 +++------------------ app/views/users/edit.html.erb | 2 +- 3 files changed, 6 insertions(+), 34 deletions(-) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 71f4b8c89..248194c41 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -4,7 +4,7 @@
    - <%= render partial: "shared/avatar", locals: {user: @user} %> + <%= render partial: "shared/avatar", locals: {user_avatar: @user} %>
    @@ -30,20 +30,7 @@

    Tweet

    -
    -
    -
    -
    - <%= image_tag @tweet.user.avatar, class: "avatar-image img-responsive img-rounded" %> -
    -
    - <%= @tweet.user.name %>, - <%= @tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> -

    <%= @tweet.description %>

    -
    -
    -
    -
    + <%= render partial: "shared/tweeter", locals: {tweeter: @tweet } %>

    Replies

    diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d2797f25e..6f71e8224 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -12,24 +12,9 @@
    <% end %> - <% @tweets.each do |tweet| %> -
    -
    -
    - <%# image_tag tweet.user.avatar, class: "avatar-image img-responsive center-block" %> - <%= render partial: "shared/avatar", locals: {user: tweet.user}%> -
    -
    - <%= tweet.user.name %>, - <%= tweet.created_at.strftime('%Y-%m-%d , %H:%M:%S') %> -

    <%= tweet.description %>

    -
    -
    -
    - <% end %> -
    -
    -
    +<% @tweets.each do |tweet| %> + <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> +<% end %>
    diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 0caa797e8..87b0b5a88 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -4,7 +4,7 @@
    - <%= render partial: "shared/avatar", locals: {user: @user} %> + <%= render partial: "shared/avatar", locals: {user_avatar: @user} %>
    <%= f.file_field :avatar %> From ef9b15bdae3c497777b49d90295a5c7b9c4cd98c Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 17:12:23 +0800 Subject: [PATCH 040/100] add tweets count on replies index page --- app/views/replies/index.html.erb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 248194c41..b16637f95 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -8,13 +8,16 @@
    -

    <%= @tweet.user.name %>

    +

    <%= @tweet.user.name %>

    - <%= @tweet.user.introduction %> +
    <%= @tweet.user.introduction %>
    +
    +

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

    +
    From ff8bbc35880d48d59e1f24045f4968ee3eb2e642 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 17:15:26 +0800 Subject: [PATCH 041/100] modify tweets controller --- app/controllers/tweets_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index d9d67c020..625b02aed 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -4,7 +4,7 @@ class TweetsController < ApplicationController def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweets = Tweet.all - @tweet = current_user.tweets.build #在index建立tweet容器來建立短文 + @tweet = Tweet.new #在index建立tweet容器來建立短文 end def create From 19d93247e65bc0c5f98a6243733c5056ebaa22dc Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 18:27:06 +0800 Subject: [PATCH 042/100] add follow button on tweet index page --- app/controllers/tweets_controller.rb | 1 + app/views/tweets/index.html.erb | 35 +++++++++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 625b02aed..3d689dc7b 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -5,6 +5,7 @@ def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweets = Tweet.all @tweet = Tweet.new #在index建立tweet容器來建立短文 + @users = User.all #在index上建立popular前先測試資料 end def create diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 6f71e8224..b1ba5fab5 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,8 +1,8 @@
    +
    -
    <%= form_for @tweet do |f| %>
    <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control" %> @@ -11,15 +11,34 @@ <%= f.submit "Tweet", class: "btn btn-primary" %>
    <% end %> + -<% @tweets.each do |tweet| %> - <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> -<% end %> +
    + <% @tweets.each do |tweet| %> + <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> + <% end %> +
    +
    -
    -
    -
    - +
    + +

    Popular

    +
    +
      + <% @users.each do |user| %> +
    • + <%= user.name %> + <%= link_to 'Follow', followships_path(following_id: user.id), method: :post %> + <% end %> +
    • +
    + +
    + +
    + +
    +
    \ No newline at end of file From c2fb784d4b89013e7df975208ea3be627466b910 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 18:49:46 +0800 Subject: [PATCH 043/100] add create action for followship record --- app/controllers/followships_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 05f01b552..647407f1b 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,7 +1,17 @@ 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 end end From f16457661f3e093461da3ab989e055e5f6083166 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 19:02:52 +0800 Subject: [PATCH 044/100] add unfollow button on tweet index page and destroy action --- app/controllers/followships_controller.rb | 7 ++++--- app/views/tweets/index.html.erb | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 647407f1b..b5eb0a050 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -9,9 +9,10 @@ def create redirect_back(fallback_location: root_path) end end - - - def destroy + @followship = current_user.followships.where(following_id: params[:id]) + @followship.destroy_all + flash[:alert] = "Followship destroyed" + redirect_back(fallback_location: root_path) end end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index b1ba5fab5..5f80f41c7 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -29,6 +29,7 @@
  • <%= user.name %> <%= link_to 'Follow', followships_path(following_id: user.id), method: :post %> + <%= link_to 'Unfollow', followship_path(user), method: :delete %> <% end %>
  • From 5d2ba6cee02454d2008e7203c3cf7a216a6dd366 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 19:18:51 +0800 Subject: [PATCH 045/100] add following include? method and follow partial --- app/models/user.rb | 4 ++++ app/views/shared/_follow.html.erb | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 app/views/shared/_follow.html.erb diff --git a/app/models/user.rb b/app/models/user.rb index 16a04b236..f0d9b33b3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -23,4 +23,8 @@ def admin? self.role == "admin" end +def following?(user) + self.followings.include?(user) +end + end diff --git a/app/views/shared/_follow.html.erb b/app/views/shared/_follow.html.erb new file mode 100644 index 000000000..571c153e8 --- /dev/null +++ b/app/views/shared/_follow.html.erb @@ -0,0 +1,7 @@ +<% if user != current_user %> + <% if current_user.following?(user) %> + <%= link_to 'Unfollow', followship_path(user), method: :delete %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: user.id), method: :post %> + <% end %> +<% end %> \ No newline at end of file From 932762be0a6ad4a568ee22a81bf5ddbda8d73e74 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 19:19:28 +0800 Subject: [PATCH 046/100] add follow partial on tweet index page --- app/views/tweets/index.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 5f80f41c7..d0560cf47 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -23,13 +23,13 @@

    Popular

    +
      <% @users.each do |user| %>
    • <%= user.name %> - <%= link_to 'Follow', followships_path(following_id: user.id), method: :post %> - <%= link_to 'Unfollow', followship_path(user), method: :delete %> + <%= render partial: "shared/follow", locals: {user: user} %> <% end %>
    @@ -37,7 +37,7 @@
    - +
    From aff8e78af6d8c4b08646c3915f88efb90992a833 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Fri, 27 Jul 2018 19:31:06 +0800 Subject: [PATCH 047/100] add follow/unfollow button css --- app/views/shared/_follow.html.erb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/shared/_follow.html.erb b/app/views/shared/_follow.html.erb index 571c153e8..66ee4fca3 100644 --- a/app/views/shared/_follow.html.erb +++ b/app/views/shared/_follow.html.erb @@ -1,7 +1,7 @@ <% if user != current_user %> <% if current_user.following?(user) %> - <%= link_to 'Unfollow', followship_path(user), method: :delete %> + <%= link_to 'Unfollow', followship_path(user), method: :delete, class: "btn btn-primary" %> <% else %> - <%= link_to 'Follow', followships_path(following_id: user.id), method: :post %> + <%= link_to 'Follow', followships_path(following_id: user.id), method: :post, class: "btn btn-primary" %> <% end %> <% end %> \ No newline at end of file From a5e7cda271be7b888cf66525ed8b265d42b118a1 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sat, 28 Jul 2018 13:27:08 +0800 Subject: [PATCH 048/100] setup user followers and following page routes --- config/routes.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 99a699958..ccf52e797 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,8 @@ resources :users, only: [:edit, :update] do member do get :tweets + get :followings + get :followers end end # 設定前台使用者行為路由 From 3d5ab530275ee5957a215ad0d9540e5106b06cc0 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sat, 28 Jul 2018 13:36:02 +0800 Subject: [PATCH 049/100] setup user and follower relationship --- app/models/user.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index f0d9b33b3..062344c94 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -14,6 +14,10 @@ class User < ApplicationRecord 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 + # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) From e8d2550b4c260274f72765986d3c75464ca84748 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sat, 28 Jul 2018 21:07:17 +0800 Subject: [PATCH 050/100] setup follower and following action --- app/controllers/users_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0088b8a26..5744252aa 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,8 +1,10 @@ class UsersController < ApplicationController before_action :set_user, only: [:edit, :update] def tweets + @user = User.find(params[:id]) end + def edit unless @user == current_user redirect_to edit_user_path(@user) @@ -15,11 +17,14 @@ def update end def followings - @followings # 基於測試規格,必須講定變數名稱 + @user = User.find(params[:id]) + @followings = @user.followings # 基於測試規格,必須講定變數名稱 + end def followers - @followers # 基於測試規格,必須講定變數名稱 + @user = User.find(params[:id]) + @followers = @user.followers # 基於測試規格,必須講定變數名稱 end def likes From 05a1310e0420f3bf625f79f5bc222ae44f8d5bdd Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sat, 28 Jul 2018 21:08:40 +0800 Subject: [PATCH 051/100] add followers and followings page --- app/views/users/followers.html.erb | 35 +++++++++++++++++++++++++++++ app/views/users/followings.html.erb | 35 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 app/views/users/followers.html.erb create mode 100644 app/views/users/followings.html.erb diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb new file mode 100644 index 000000000..5c7c2e8ce --- /dev/null +++ b/app/views/users/followers.html.erb @@ -0,0 +1,35 @@ + +
    +
    +
    +
    +

    userporfile partial

    +
    +
    + + +
    + +
    +
    +

    Follower

    +
    + <% @followers.each do |follower| %> +
    +
    + <%= render partial: "shared/avatar", locals: {user_avatar: follower}%> +
    +
    +
    <%= follower.name %>
    +

    + <%= follower.introduction %> +

    +
    +
    + <% end %> +
    +
    +
    +
    +
    + diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb new file mode 100644 index 000000000..6c67fe649 --- /dev/null +++ b/app/views/users/followings.html.erb @@ -0,0 +1,35 @@ + +
    +
    +
    +
    +

    userporfile partial

    +
    +
    + + +
    + +
    +
    +

    Following

    +
    + <% @followings.each do |following| %> +
    +
    + <%= render partial: "shared/avatar", locals: {user_avatar: following}%> +
    +
    +
    <%= following.name %>
    +

    + <%= following.introduction %> +

    +
    +
    + <% end %> +
    +
    +
    +
    +
    + From 879842e93f675b76a98d57b644a092a77ebae856 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 13:06:32 +0800 Subject: [PATCH 052/100] add following and follower sort by follow created time --- app/controllers/users_controller.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 5744252aa..ed7794f46 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,5 @@ class UsersController < ApplicationController -before_action :set_user, only: [:edit, :update] +before_action :set_user, only: [:edit, :update, :followings, :followers] def tweets @user = User.find(params[:id]) end @@ -17,14 +17,12 @@ def update end def followings - @user = User.find(params[:id]) - @followings = @user.followings # 基於測試規格,必須講定變數名稱 - + # For ordering on the attribute of an associated model you have to include it: + @followings = @user.followings.includes(:followships).order("followships.created_at desc") # 基於測試規格,必須講定變數名稱 end def followers - @user = User.find(params[:id]) - @followers = @user.followers # 基於測試規格,必須講定變數名稱 + @followers = @user.followers.includes(:followships).order("followships.created_at desc") # 基於測試規格,必須講定變數名稱 end def likes From dc5586bb1b6665c9747429091e0c9af1481ab53c Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 20:36:49 +0800 Subject: [PATCH 053/100] setup routes for like and unlike and user like page --- config/routes.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index ccf52e797..0623e9b13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,11 +17,16 @@ get :tweets get :followings get :followers + get :likes end end # 設定前台使用者行為路由 resources :tweets, only: [:index, :create] do resources :replies, only: [:index, :create] + member do + post :like + post :unlike + end end # 設定追蹤路由 resources :followships, only: [:create, :destroy] From c296fb0295d4227c913d1a1e70ae332322000138 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 20:38:40 +0800 Subject: [PATCH 054/100] setup relation among user, tweet and like --- app/models/like.rb | 2 ++ app/models/tweet.rb | 4 ++++ app/models/user.rb | 4 ++++ 3 files changed, 10 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 3fbfd7971..a2bcd7764 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -7,4 +7,8 @@ class Tweet < ApplicationRecord validates :description, presence: true, length: {maximum: 140} # tweet有很多回覆,當tweet被刪除時,順便刪除回覆 has_many :replies, dependent: :destroy + +# 使用者可以喜歡很多評論 + has_many :likes, dependent: :destroy + has_many :liked_users, through: :likes, source: :user end diff --git a/app/models/user.rb b/app/models/user.rb index 062344c94..a7c0de179 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,10 @@ class User < ApplicationRecord has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" has_many :followers, through: :inverse_followships, source: :user + # 設定使用者喜歡很多評論多對多關聯 + has_many :likes, dependent: :destroy + has_many :liked_tweets, through: :likes, source: :tweet + # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) From eab22c4f375feda7a732875a694330a1d80608a0 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 21:55:32 +0800 Subject: [PATCH 055/100] setup user like or unlike tweet routes --- config/routes.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 0623e9b13..ec0e90c8d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,11 +23,14 @@ # 設定前台使用者行為路由 resources :tweets, only: [:index, :create] do resources :replies, only: [:index, :create] - member do - post :like - post :unlike - end + + # 設定user喜歡或取消喜歡tweet + member do + post :like + post :unlike + end end # 設定追蹤路由 resources :followships, only: [:create, :destroy] -end + + end From 4d72980f709ea61318b8be16e6445c46a2f25de4 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 21:57:33 +0800 Subject: [PATCH 056/100] add user like and unlike tweet action --- app/controllers/tweets_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 3d689dc7b..42c1298d0 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -20,9 +20,16 @@ def create 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 From 46afd7030e93ca9c3ea5418dcac310fff6bd35c3 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 21:59:58 +0800 Subject: [PATCH 057/100] add user like/unlike tweet button --- app/views/tweets/index.html.erb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d0560cf47..7435df70b 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -16,6 +16,11 @@
    <% @tweets.each do |tweet| %> <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> + + + <%= link_to 'Unlike', unlike_tweet_path(tweet),method: :post, class: "btn btn-info" %> + <%= link_to 'Like', like_tweet_path(tweet), method: :post, class: "btn btn-primary" %> + <% end %>
    From 23d8b21ae55bdb363031153edd077565ecd87f9d Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 22:22:29 +0800 Subject: [PATCH 058/100] add is_liked? method to tweet model for checking like/unlike button --- app/models/tweet.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/tweet.rb b/app/models/tweet.rb index a2bcd7764..1d2ab7b9b 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -11,4 +11,8 @@ class Tweet < ApplicationRecord # 使用者可以喜歡很多評論 has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user + + def is_liked?(user) + self.liked_users.include?(user) + end end From b93f46d7f101e9d67d281726a1b2f88d0b2c9980 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Sun, 29 Jul 2018 22:23:21 +0800 Subject: [PATCH 059/100] add like/unlike button on tweet index page --- app/views/tweets/index.html.erb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 7435df70b..9cde3db43 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -18,8 +18,11 @@ <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> - <%= link_to 'Unlike', unlike_tweet_path(tweet),method: :post, class: "btn btn-info" %> - <%= link_to 'Like', like_tweet_path(tweet), method: :post, class: "btn btn-primary" %> + <% if tweet.is_liked?(current_user) %> + <%= link_to 'Unlike', unlike_tweet_path(tweet),method: :post, class: "btn btn-info" %> + <% else %> + <%= link_to 'Like', like_tweet_path(tweet), method: :post, class: "btn btn-primary" %> + <% end %> <% end %>
    From 5770282a89e763f4327da8fa2c495a4e5614e467 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 11:23:38 +0800 Subject: [PATCH 060/100] add like link partial --- app/views/shared/_liked.html.erb | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 app/views/shared/_liked.html.erb diff --git a/app/views/shared/_liked.html.erb b/app/views/shared/_liked.html.erb new file mode 100644 index 000000000..fd63f7155 --- /dev/null +++ b/app/views/shared/_liked.html.erb @@ -0,0 +1,5 @@ +<% if tweet.is_liked?(current_user) %> + <%= link_to "Unlike(#{tweet.likes.count}) ", unlike_tweet_path(tweet),method: :post %> +<% else %> + <%= link_to "Like(#{tweet.likes.count})", like_tweet_path(tweet), method: :post %> +<% end %> \ No newline at end of file From e5f27db7de8167868f02b44a235672a144693c47 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 11:40:30 +0800 Subject: [PATCH 061/100] modify like partia on tweeter and tweets index page --- app/views/shared/_tweeter.html.erb | 6 ++++-- app/views/tweets/index.html.erb | 7 ------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/app/views/shared/_tweeter.html.erb b/app/views/shared/_tweeter.html.erb index a3e2729c7..29cf38b7c 100644 --- a/app/views/shared/_tweeter.html.erb +++ b/app/views/shared/_tweeter.html.erb @@ -5,11 +5,13 @@ <%= render partial: "shared/avatar", locals: {user_avatar: tweeter.user}%>
    - <%= tweeter.user.name %>, - <%= tweeter.created_at.strftime('%Y-%m-%d, %H:%M:%S') %> + <%= tweeter.user.name %>, + <%= tweeter.created_at.strftime('%Y-%m-%d, %H:%M:%S') %>

    <%= tweeter.description %>

    + <%= render partial: "shared/liked", locals: {tweet: tweeter}%> +

    diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 9cde3db43..a9a2442a4 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -17,13 +17,6 @@ <% @tweets.each do |tweet| %> <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> - - <% if tweet.is_liked?(current_user) %> - <%= link_to 'Unlike', unlike_tweet_path(tweet),method: :post, class: "btn btn-info" %> - <% else %> - <%= link_to 'Like', like_tweet_path(tweet), method: :post, class: "btn btn-primary" %> - <% end %> - <% end %>
    From d0eae51d9f71937bb0673f1fd969fa10848691f5 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 12:02:37 +0800 Subject: [PATCH 062/100] add reply link on tweeter partial page --- app/views/shared/_tweeter.html.erb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/views/shared/_tweeter.html.erb b/app/views/shared/_tweeter.html.erb index 29cf38b7c..06ebf61f3 100644 --- a/app/views/shared/_tweeter.html.erb +++ b/app/views/shared/_tweeter.html.erb @@ -10,7 +10,9 @@

    <%= tweeter.description %>

    - <%= render partial: "shared/liked", locals: {tweet: tweeter}%> +

    + <%= render partial: "shared/liked", locals: {tweet: tweeter}%> + <%= link_to "Reply(#{tweeter.replies.count}) ", tweet_replies_path(tweeter), class: "links" %>

    From 00347f0d99b784555a26adeca88ac40441716fc0 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 13:46:08 +0800 Subject: [PATCH 063/100] modify unfollow button css --- app/views/shared/_follow.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/_follow.html.erb b/app/views/shared/_follow.html.erb index 66ee4fca3..c16548b12 100644 --- a/app/views/shared/_follow.html.erb +++ b/app/views/shared/_follow.html.erb @@ -1,6 +1,6 @@ <% if user != current_user %> <% if current_user.following?(user) %> - <%= link_to 'Unfollow', followship_path(user), method: :delete, class: "btn btn-primary" %> + <%= link_to 'Unfollow', followship_path(user), method: :delete, class: "btn btn-info" %> <% else %> <%= link_to 'Follow', followships_path(following_id: user.id), method: :post, class: "btn btn-primary" %> <% end %> From 6fc50169687eea25917c9ffd8c6171aae7a9a4a5 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 19:41:32 +0800 Subject: [PATCH 064/100] add user info partial page --- app/controllers/users_controller.rb | 1 + app/views/shared/_userinfo.html.erb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 app/views/shared/_userinfo.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ed7794f46..55334c709 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -26,6 +26,7 @@ def followers end def likes + @user = User.find(params[:id]) @likes # 基於測試規格,必須講定變數名稱 end diff --git a/app/views/shared/_userinfo.html.erb b/app/views/shared/_userinfo.html.erb new file mode 100644 index 000000000..18cde286c --- /dev/null +++ b/app/views/shared/_userinfo.html.erb @@ -0,0 +1,22 @@ +
    +
    + <%= render partial: "shared/avatar", locals: {user_avatar: user}%> +

    <%= user.name %>

    +

    <%= user.introduction %>

    +

    Tweet <%= user.tweets.count %>

    +

    Following <%= user.followings.count %>

    +

    Follower <%= user.followers.count %>

    +

    likes <%= user.likes.count %>

    + + <% if user != current_user %> +

    <%= render partial: "shared/follow", locals: {user: user} %>

    + <% else %> +

    <%= link_to "Edit Profile", edit_user_path(user), class:"btn btn-info" %>

    + <% end %> + + +
    +
    +
    + +
    \ No newline at end of file From c5f4f82e3a44cd516e80a36fd9632afe8ea908b4 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 19:47:27 +0800 Subject: [PATCH 065/100] add like page --- app/controllers/users_controller.rb | 3 ++- app/views/users/likes.html.erb | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/views/users/likes.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 55334c709..597c7dbdb 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -27,7 +27,8 @@ def followers def likes @user = User.find(params[:id]) - @likes # 基於測試規格,必須講定變數名稱 + @likes = @user.liked_tweets + # 基於測試規格,必須講定變數名稱 end private diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb new file mode 100644 index 000000000..5182b23c7 --- /dev/null +++ b/app/views/users/likes.html.erb @@ -0,0 +1,15 @@ +

    like

    +
    +
    + +
    + <%= render partial: "shared/userinfo", locals: {user: @user}%> +
    + +
    + <% @likes.each do |tweet| %> + <%= render partial: "shared/tweeter", locals: {tweeter: tweet} %> + <% end %> +
    +
    +
    \ No newline at end of file From c9501b6b202ab32c93541fff296bdc191c18c39d Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 19:52:02 +0800 Subject: [PATCH 066/100] add like tweet order desc by like created time --- app/controllers/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 597c7dbdb..b827ec61e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -27,7 +27,7 @@ def followers def likes @user = User.find(params[:id]) - @likes = @user.liked_tweets + @likes = @user.liked_tweets.includes(:likes).order("likes.created_at desc") # 基於測試規格,必須講定變數名稱 end From b0e9f2cb80327eb4e2156471ecd99a0d4a863137 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Mon, 30 Jul 2018 21:37:59 +0800 Subject: [PATCH 067/100] add tweet,following,follower and like link on user info partial page --- app/views/shared/_userinfo.html.erb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/views/shared/_userinfo.html.erb b/app/views/shared/_userinfo.html.erb index 18cde286c..515228aa5 100644 --- a/app/views/shared/_userinfo.html.erb +++ b/app/views/shared/_userinfo.html.erb @@ -3,10 +3,10 @@ <%= render partial: "shared/avatar", locals: {user_avatar: user}%>

    <%= user.name %>

    <%= user.introduction %>

    -

    Tweet <%= user.tweets.count %>

    -

    Following <%= user.followings.count %>

    -

    Follower <%= user.followers.count %>

    -

    likes <%= user.likes.count %>

    +

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

    +

    <%= link_to "Following (#{user.followings.count}) ",followings_user_path(user) %>

    +

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

    +

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

    <% if user != current_user %>

    <%= render partial: "shared/follow", locals: {user: user} %>

    From 470a257dfe6521f169b0bc1810e70298cd73f191 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 05:59:37 +0800 Subject: [PATCH 068/100] add find user tweets order created time desc and modify another contorller --- app/controllers/users_controller.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b827ec61e..f718659a9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,10 +1,10 @@ class UsersController < ApplicationController -before_action :set_user, only: [:edit, :update, :followings, :followers] +before_action :set_user, only: [:edit, :update, :followings, :followers, :likes, :tweets] def tweets @user = User.find(params[:id]) + @tweets = @user.tweets.includes(:user).order("tweets.created_at desc") end - - + def edit unless @user == current_user redirect_to edit_user_path(@user) @@ -26,7 +26,6 @@ def followers end def likes - @user = User.find(params[:id]) @likes = @user.liked_tweets.includes(:likes).order("likes.created_at desc") # 基於測試規格,必須講定變數名稱 end From b97836b52d4a7cde6626856d7da313e6fb4bf964 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 06:00:14 +0800 Subject: [PATCH 069/100] modify user model for tweets --- app/models/user.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index a7c0de179..675bc44df 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,8 +8,7 @@ class User < ApplicationRecord mount_uploader :avatar, AvatarUploader # 設定user跟tweet關聯,一個user可以有很多tweets - has_many :tweets - + has_many :tweets, dependent: :destroy # 設定追蹤關聯紀錄 has_many :followships, dependent: :destroy has_many :followings, through: :followships From b11c6f796cc91016dd6da183917e85fecc1f382b Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 06:00:52 +0800 Subject: [PATCH 070/100] add user tweet page --- app/views/users/tweets.html.erb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 app/views/users/tweets.html.erb diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb new file mode 100644 index 000000000..90f44600c --- /dev/null +++ b/app/views/users/tweets.html.erb @@ -0,0 +1,14 @@ +

    tweet

    +
    +
    + +
    + <%= render partial: "shared/userinfo", locals: {user: @user}%> +
    +
    + <% @tweets.each do |tweet| %> + <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> + <% end %> +
    +
    +
    \ No newline at end of file From 512946ada48448ecc8db66349d205213e86b58d7 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 06:21:14 +0800 Subject: [PATCH 071/100] add user info partial and follow/unfollow button on following and follower page --- app/views/users/followers.html.erb | 7 ++++--- app/views/users/followings.html.erb | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb index 5c7c2e8ce..654126f61 100644 --- a/app/views/users/followers.html.erb +++ b/app/views/users/followers.html.erb @@ -1,10 +1,9 @@
    +
    -
    -

    userporfile partial

    -
    + <%= render partial: "shared/userinfo", locals: {user: @user}%>
    @@ -24,6 +23,8 @@

    <%= follower.introduction %>

    + <%= render partial: "shared/follow", locals: {user: follower} %> +
    <% end %> diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb index 6c67fe649..d8a1ef011 100644 --- a/app/views/users/followings.html.erb +++ b/app/views/users/followings.html.erb @@ -1,10 +1,10 @@
    + +
    -
    -

    userporfile partial

    -
    + <%= render partial: "shared/userinfo", locals: {user: @user}%>
    @@ -24,6 +24,8 @@

    <%= following.introduction %>

    + <%= render partial: "shared/follow", locals: {user: following} %> +
    <% end %> From 7edf2d9df7b1d4583244daa3fec660b6e8cb9289 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 06:27:52 +0800 Subject: [PATCH 072/100] add tweet's user name link to user tweet page on tweeter partial --- app/views/shared/_tweeter.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/shared/_tweeter.html.erb b/app/views/shared/_tweeter.html.erb index 06ebf61f3..a53030495 100644 --- a/app/views/shared/_tweeter.html.erb +++ b/app/views/shared/_tweeter.html.erb @@ -5,7 +5,7 @@ <%= render partial: "shared/avatar", locals: {user_avatar: tweeter.user}%>
    - <%= tweeter.user.name %>, + <%= link_to"#{tweeter.user.name}", tweets_user_path(tweeter.user) %>, <%= tweeter.created_at.strftime('%Y-%m-%d, %H:%M:%S') %>

    <%= tweeter.description %> From 6e315fe1ce8bd20529e2685cee809e58567321ff Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 06:41:56 +0800 Subject: [PATCH 073/100] add user info partial on reply page --- app/views/replies/index.html.erb | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index b16637f95..5688274f5 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -4,32 +4,10 @@

    - <%= render partial: "shared/avatar", locals: {user_avatar: @user} %> + <%= render partial: "shared/userinfo", locals: {user: @tweet.user} %>
    - -
    -

    <%= @tweet.user.name %>

    -
    - -
    -
    <%= @tweet.user.introduction %>
    -
    - -
    -

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

    -
    - - - - -
    - <%= link_to 'Edit Profile',edit_user_path(@tweet.user_id), class: "btn btn-primary" %> -
    -
    - +
    -
    -

    Tweet

    @@ -67,3 +45,5 @@
    <% end %>
    +
    +
    \ No newline at end of file From 468a30b40531ed7b2dfa37b0568686c2dc266fd3 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 10:05:35 +0800 Subject: [PATCH 074/100] add follower count method on user model --- app/models/user.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 675bc44df..e7e69c994 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -33,5 +33,9 @@ def admin? def following?(user) self.followings.include?(user) end +def followers_count + self.followers_count = self.followers.count + self.save +end end From c91f63c64840d05d5fc4bfb9f51f3754aa3409af Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 10:06:25 +0800 Subject: [PATCH 075/100] add follower count action on user and tweet controller --- app/controllers/tweets_controller.rb | 5 +++-- app/controllers/users_controller.rb | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 42c1298d0..fce6b60d3 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -2,10 +2,11 @@ class TweetsController < ApplicationController # 登入認證 before_action :authenticate_user! def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @users = User.order(followers_count: :desc).limit(10) @tweets = Tweet.all @tweet = Tweet.new #在index建立tweet容器來建立短文 - @users = User.all #在index上建立popular前先測試資料 + #@users = User.all 在index上建立popular前先測試資料 end def create diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f718659a9..631cd3155 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -23,6 +23,7 @@ def followings def followers @followers = @user.followers.includes(:followships).order("followships.created_at desc") # 基於測試規格,必須講定變數名稱 + @user.followers_count end def likes From c88efec8a11842699ec4fcfbb83a814299c66f63 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 10:17:09 +0800 Subject: [PATCH 076/100] add popular user on tweet index page --- app/views/tweets/index.html.erb | 43 ++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index a9a2442a4..e15152846 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -16,31 +16,36 @@
    <% @tweets.each do |tweet| %> <%= render partial: "shared/tweeter", locals: {tweeter: tweet}%> - <% end %>
    -
    -

    Popular

    - -
    -
      - <% @users.each do |user| %> -
    • - <%= user.name %> - <%= render partial: "shared/follow", locals: {user: user} %> - <% end %> -
    • -
    +
    +
    +
    +

    Popular

    +
    + <% @users.each do |user| %> +
    +
    + <%= render partial: "shared/avatar", locals: {user_avatar: user}%> +
    +
    +
    <%= link_to "#{user.name}", tweets_user_path(user) %>
    +

    + <%= user.introduction %> +

    +

    + <%= render partial: "shared/follow", locals: {user: user} %> +

    +
    +
    + <% end %> + +
    - -
    - +
    -
    -
    -
    \ No newline at end of file From af7ffbbdf176c88afede6ad6c0ffc70368690b93 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 10:38:21 +0800 Subject: [PATCH 077/100] add current user image link on navigation --- app/views/layouts/application.html.erb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4bdccf85d..40dda6376 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -28,8 +28,10 @@ <% if current_user.admin? %>
  • <% link_to 'Admin 後台', admin_tweets_path %>
  • <% end %> -
  • -
  • <%= link_to 'Profile', user_path(current_user) %>
  • +
  • <%= link_to tweets_user_path(current_user) do %> + <%= render partial: "shared/avatar", locals: {user_avatar: current_user} %> + <% end %>
  • +
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • From 3584c3ef4f4accc86ad2499b88ce4b487731f934 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 12:55:19 +0800 Subject: [PATCH 078/100] add admin delete tweet action --- 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 10e19d643..9189ff41e 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -7,5 +7,9 @@ def index end def destroy + @tweets = Tweet.find(params[:id]) + @tweets.destroy + redirect_to admin_tweets_path + flash[:alert] = "tweet was deleted" end end From 7eaa5a4fa26f92bc37e21365b59163f265097b28 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 12:56:03 +0800 Subject: [PATCH 079/100] add delete tweet button on admin index page --- app/views/admin/tweets/index.html.erb | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index a69fbf018..2242924df 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,10 +1,16 @@ + +
    +

    tweets 後台

      <% @tweets.each do |tweet| %> -
    • - <%= tweets.name %> - <%= link_to 'Show', admin_tweet_path(tweet) %> - <%= link_to 'Delete', admin_tweet_path(tweet), method: :delete, data: {confirm:"Are you sure?"} %> -
    • - <% end %> + + <%= render partial: "shared/tweeter", locals: {tweeter: tweet} %> + <% end %>
    + + +
    + From bc2579f7bde824464b282f3255bcdf3ec4526c91 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 13:23:34 +0800 Subject: [PATCH 080/100] transfer admin authenticate to admin base controller --- app/controllers/admin/base_controller.rb | 11 +++++++++++ app/controllers/application_controller.rb | 9 --------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 4a89583f5..735ddcbd4 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,3 +1,14 @@ class Admin::BaseController < ApplicationController +# 後台登入先確認user登入再確認身份為admin,authenticate_admin方法要另外定義 +before_action :authenticate_user! +before_action :authenticate_admin +private + +def authenticate_admin + unless current_user.admin? #admin?要另外在user model定義 + flash[:alert] = "Not allow!" + redirect_to root_path + end + end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 504a73a96..a46bed38a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,13 +13,4 @@ def configure_permitted_parameters devise_parameter_sanitizer.permit(:account_update, keys: [:name]) end - private - - def authenticate_admin - unless current_user.admin? #admin?要另外在user model定義 - flash[:alert] = "Not allow!" - redirect_to root_path - end - end - end From 4ee707cf99f5ed20159083935c9c76dbff0d46c5 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 17:05:37 +0800 Subject: [PATCH 081/100] add tweets_count column on users table --- db/migrate/20180731082254_add_tweets_count_to_users.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180731082254_add_tweets_count_to_users.rb diff --git a/db/migrate/20180731082254_add_tweets_count_to_users.rb b/db/migrate/20180731082254_add_tweets_count_to_users.rb new file mode 100644 index 000000000..abb23c746 --- /dev/null +++ b/db/migrate/20180731082254_add_tweets_count_to_users.rb @@ -0,0 +1,5 @@ +class AddTweetsCountToUsers < ActiveRecord::Migration[5.1] + def change + add_column :users, :tweets_count, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a6842c86..6726912b4 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: 20180731082254) 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", default: 0 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 286e742fd1d53e287a266c05807595e5be19505b Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 17:07:35 +0800 Subject: [PATCH 082/100] add tweets_count method on user model and add user order by tweets count action --- app/controllers/admin/users_controller.rb | 1 + app/models/user.rb | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 3ba9f0a36..d1d478011 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,4 +1,5 @@ class Admin::UsersController < Admin::BaseController def index + @users = User.order("tweets_count desc") end end diff --git a/app/models/user.rb b/app/models/user.rb index e7e69c994..ed7cda637 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -37,5 +37,9 @@ def followers_count self.followers_count = self.followers.count self.save end +def tweets_count + self.tweets_count = self.tweets.count + self.save +end end From d2f7e00633e7b73cad3595c1078488c79c549283 Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 17:09:17 +0800 Subject: [PATCH 083/100] add all user list on admin user index page --- app/views/admin/users/index.html.erb | 32 ++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 app/views/admin/users/index.html.erb diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb new file mode 100644 index 000000000..03f08a4ef --- /dev/null +++ b/app/views/admin/users/index.html.erb @@ -0,0 +1,32 @@ +
    +
    +

    User List

    + + + + + + + + + + + + + <% @users.each do |user| %> + + + + + + + + + <% end %> + +
    User IDNameTweetsFollowingsFollowersLike Tweets
    <%= user.id %><%= user.name %><%= user.tweets.count %><%= user.followers.count %><%= user.followings.count %><%= user.likes.count %>
    +
    +
    + \ No newline at end of file From 9266cf981772ee7b5817cea404658db4a6cfa80a Mon Sep 17 00:00:00 2001 From: Cynthia Date: Tue, 31 Jul 2018 17:39:30 +0800 Subject: [PATCH 084/100] add tweets and users list for admin link --- app/controllers/application_controller.rb | 9 +++++++++ app/views/layouts/application.html.erb | 5 +++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a46bed38a..614f44640 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -13,4 +13,13 @@ def configure_permitted_parameters devise_parameter_sanitizer.permit(:account_update, keys: [:name]) end + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "Not allow" + redirect_to root_path + end + end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 40dda6376..01c608c9c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -19,14 +19,15 @@ - LOGO + <%= link_to 'LOGO', tweets_path %>