From 613c6eb49e1e94d0d8559d5c799f561c77583755 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 17 Jul 2018 13:09:02 +0800 Subject: [PATCH 01/48] add twitter index action and view for admin --- app/controllers/tweets_controller.rb | 1 - app/views/admin/tweets/index.html.erb | 4 ++++ app/views/tweets/index.html.erb | 6 ++++++ app/views/tweets/show.html.erb | 7 +++++++ config/routes.rb | 10 +++++++++- 5 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 app/views/admin/tweets/index.html.erb create mode 100644 app/views/tweets/index.html.erb create mode 100644 app/views/tweets/show.html.erb diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..b38c2c660 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,7 +1,6 @@ class TweetsController < ApplicationController def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end def create diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb new file mode 100644 index 000000000..2ea8e5bb9 --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1,4 @@ +
+

後台

+ +
\ 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..9c2169a22 --- /dev/null +++ b/app/views/tweets/index.html.erb @@ -0,0 +1,6 @@ +
+
+

前台

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

<%= @tweets.name %>

+ +
+ +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 90856d4fe..ad25daca2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,14 @@ # 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 + resources :tweets + root "tweets#index" + end end From 51e95c6498b044d4d07e61b4728c553fbdb2abed Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 17 Jul 2018 14:42:03 +0800 Subject: [PATCH 02/48] add validates uniqueness of name and email --- app/controllers/application_controller.rb | 20 ++++++++++++++++++++ app/controllers/tweets_controller.rb | 1 + app/models/user.rb | 10 +++++++++- app/views/devise/registrations/new.html.erb | 5 +++++ app/views/layouts/application.html.erb | 7 ++++--- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..7a39e5e95 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,4 +4,24 @@ class ApplicationController < ActionController::Base # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 # https://github.com/plataformatec/devise#strong-parameters # 注意有 sign_up 和 account_update 兩種參數要處理 + + before_action :authenticate_user! + before_action :configure_permitted_parameters, if: :devise_controller? + + + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "Not allow!" + redirect_to root_path + end + end + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:sign_up, keys: [:name]) + end + + + end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index b38c2c660..df08d46ea 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,6 +1,7 @@ class TweetsController < ApplicationController def index + @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end def create diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..6c5912e93 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,15 @@ class User < ApplicationRecord # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 - validates_presence_of :name + validates_presence_of :name + validates_uniqueness_of :name + validates_uniqueness_of :email # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) +def admin? + self.role== "admin" +end + + + end diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 602803cff..b4f3daf2b 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,6 +3,11 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> +

+ <%= f.label :name %> + <%= f.text_field :name %> +
+
<%= f.label :email %>
<%= f.email_field :email, autofocus: true, autocomplete: "email" %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 952cb7a1b..f32b5e560 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,18 +11,19 @@ <% if current_user %> <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_restaurants_path %>
  • +
  • <%= link_to 'Admin Panel', admin_tweets_path %>
  • <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • <%= link_to('登入', new_user_session_path) %>
  • <% end %> +

    <%= notice %>

    -

    <%= alert %>

    +

    <%= alert %>

    + <%= yield %> From 165f4dd6f7e03570519ec8069f7d166382f3a064 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 10:26:35 +0800 Subject: [PATCH 03/48] install bootstrap-sass, jquery-rails, and change navbar --- Gemfile | 4 +- Gemfile.lock | 15 ++++- app/assets/javascripts/application.js | 3 + .../{application.css => application.scss} | 5 +- app/views/layouts/application.html.erb | 65 +++++++++++++++---- 5 files changed, 74 insertions(+), 18 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (92%) diff --git a/Gemfile b/Gemfile index c1f1ee3e2..154901ba4 100644 --- a/Gemfile +++ b/Gemfile @@ -21,7 +21,9 @@ gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets -gem 'sass-rails', '~> 5.0' +gem 'bootstrap-sass', '~> 3.3.7' +gem 'sass-rails', '>= 3.2' +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..61c33bd8c 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,12 +269,13 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + jquery-rails listen (>= 3.0.5, < 3.2) puma (~> 3.7) rails (~> 5.1.4) rails-controller-testing rspec-rails (~> 3.7) - sass-rails (~> 5.0) + sass-rails (>= 3.2) selenium-webdriver shoulda-matchers (~> 3.1) spring @@ -276,4 +287,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..2b4854415 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -12,4 +12,7 @@ // //= require rails-ujs //= require turbolinks +//= require jquery +//= require bootstrap-sprockets //= 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..9a3100e97 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,7 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * - *= require_tree . - *= require_self */ + +@import 'bootstrap-sprockets'; +@import 'bootstrap'; \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f32b5e560..a93366025 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,21 +9,60 @@ - <% if current_user %> - <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_tweets_path %>
  • - <% end %> -
  • -
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • - <% else %> -
  • <%= link_to('註冊', new_user_registration_path) %>
  • -
  • <%= link_to('登入', new_user_session_path) %>
  • - <% end %> -

    <%= notice %>

    -

    <%= alert %>

    + + + <% if flash[:notice] %> +
    +
    + +
    + <% end %> + + <% if flash[:alert] %> +
    +
    + +
    + <% end %> + <%= yield %> From 6559031f94b6b254e00f6d37913fc1a009268f5f Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 10:40:41 +0800 Subject: [PATCH 04/48] steup Heroku --- Gemfile | 7 +++++-- config/database.yml | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 154901ba4..a8c6c69b1 100644 --- a/Gemfile +++ b/Gemfile @@ -16,8 +16,6 @@ gem 'ffaker' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' -# Use sqlite3 as the database for Active Record -gem 'sqlite3' # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets @@ -43,7 +41,12 @@ gem 'jbuilder', '~> 2.5' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +group :production do + gem 'pg', '~> 0.20' +end + group :development, :test do + gem 'sqlite3' # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] # Adds support for Capybara system testing and selenium driver diff --git a/config/database.yml b/config/database.yml index 0d02f2498..816288eaa 100644 --- a/config/database.yml +++ b/config/database.yml @@ -23,3 +23,5 @@ test: production: <<: *default database: db/production.sqlite3 + adapter: postgresql + encoding: Unicode From 6faf1c541ae6205e56413c53e19b1701243f95c9 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 10:58:04 +0800 Subject: [PATCH 05/48] Test --- Gemfile.lock | 4 ++++ config/database.yml | 2 -- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 61c33bd8c..4659a1e34 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -133,6 +133,9 @@ GEM nokogiri (1.8.1-x86-mingw32) mini_portile2 (~> 2.3.0) orm_adapter (0.5.0) + pg (0.21.0) + pg (0.21.0-x64-mingw32) + pg (0.21.0-x86-mingw32) public_suffix (3.0.1) puma (3.11.2) puma (3.11.2-java) @@ -271,6 +274,7 @@ DEPENDENCIES jbuilder (~> 2.5) jquery-rails listen (>= 3.0.5, < 3.2) + pg (~> 0.20) puma (~> 3.7) rails (~> 5.1.4) rails-controller-testing diff --git a/config/database.yml b/config/database.yml index 816288eaa..1c8d40f2a 100644 --- a/config/database.yml +++ b/config/database.yml @@ -21,7 +21,5 @@ test: database: db/test.sqlite3 production: - <<: *default - database: db/production.sqlite3 adapter: postgresql encoding: Unicode From 8edbd453b50ecfece20e0dd6c4a48d1f1923d224 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 11:13:14 +0800 Subject: [PATCH 06/48] Test for Admin --- app/controllers/admin/tweets_controller.rb | 2 ++ db/seeds.rb | 3 +++ 2 files changed, 5 insertions(+) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..d19b5f0ef 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,4 +1,6 @@ class Admin::TweetsController < Admin::BaseController + before_action :authenticate_user! + before_action :authenticate_admin def index end diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2acc..19f60166b 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,6 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + +User.create(email: "admin@aaa.com", password: "asdasd", role: "admin") +puts "Default admin created!" \ No newline at end of file From 0f69b715474ae04b8a7e89bb58ac55d045dab7a9 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 15:16:19 +0800 Subject: [PATCH 07/48] user can edit/update the profile details --- app/controllers/users_controller.rb | 27 ++++++++++++++++- app/views/layouts/application.html.erb | 1 + app/views/users/edit.html.erb | 42 ++++++++++++++++++++++++++ config/routes.rb | 4 +-- lib/tasks/dev.rake | 6 +++- 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 app/views/users/edit.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 750e3c6b5..79422d9ef 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,12 +3,35 @@ class UsersController < ApplicationController def tweets end + def edit + @user = User.find(params[:id]) + + unless @user.name + @user.name = @user.email.split('@').first + end + + unless @user.id == current_user.id + flash[:notice] = "Don't Allow!"; + # redirect_to user_path(@user) + redirect_back(fallback_location: tweets_user_path(current_user)) + end end - + def update + @user = User.find(params[:id]) + if @user.update(user_params) + #redirect_to user_path(@user.id) + redirect_back(fallback_location: tweets_user_path(@user)) + flash[:notice] = "user was successfully updated" + else + redirect_back(fallback_location: tweets_user_path(@user)) + flash[:alert] = "failed" + end end + + def followings @followings # 基於測試規格,必須講定變數名稱 end @@ -21,4 +44,6 @@ def likes @likes # 基於測試規格,必須講定變數名稱 end + private + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a93366025..b8eb020ac 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -32,6 +32,7 @@
  • <%= link_to 'Admin後台', admin_tweets_path %>
  • <% end %>
  • +
  • <%= link_to('修改個人資料', user_path(current_user)) %>
  • <%= link_to('Logoout', destroy_user_session_path, method: :delete) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..76c4875f7 --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,42 @@ +
    +
    +
    + + <% if @user.errors.any? %> +

    Find some errors !

    +
      + <% @user.errors.full_messages.each do |msg| %> +
    • <%= msg %>
    • + <% end %> +
    +
    + <% end %> + + <%= form_for @user do |f| %> +
    + +
    + <%= f.label :name, "Name" %> + <%= f.text_field :name, class: "form-control" %> +
    + +
    + <%= f.label :introduction, "Intro" %> + <%= f.text_area :introduction, class: "form-control" %> +
    + +
    + <%= f.label :avatar, "Avatar" %> + <%= image_tag @user.avatar, width: '200px' if @user.avatar? %> + <%= f.file_field :avatar %> +
    + +
    + <%= f.submit class: "btn btn-primary" %> + <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> +
    + <% end %> +
    +
    +
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ad25daca2..b4197c6cf 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,13 +3,13 @@ devise_for :users # 請依照專案指定規格來設定路由 - + resources :tweets, only: [:index, :create] root "tweets#index" + resources :users, only: [:edit, :update ] namespace :admin do resources :tweets root "tweets#index" end - end diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 9b1e87ae4..f5aafdcb3 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,7 +2,11 @@ namespace :dev do # 請先執行 rails dev:fake_user,可以產生 20 個資料完整的 User 紀錄 # 其他測試用的假資料請依需要自行撰寫 task fake_user: :environment do - User.destroy_all + + User.all.each do | user | + user.destroy unless user.admin? + end + 20.times do |i| name = FFaker::Name::first_name file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") From c34a755e82712d4ffaba3b1cb8d133aa6d3be1c4 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 17:37:06 +0800 Subject: [PATCH 08/48] user can edit profile --- app/controllers/tweets_controller.rb | 7 +++-- app/controllers/users_controller.rb | 37 ++++++++--------------- app/models/tweet.rb | 1 + app/models/user.rb | 2 ++ app/views/admin/tweets/index.html.erb | 3 +- app/views/layouts/application.html.erb | 12 +++++--- app/views/shared/_profile.html.erb | 0 app/views/tweets/index.html.erb | 25 ++++++++++++++- app/views/tweets/show.html.erb | 7 ----- app/views/users/edit.html.erb | 42 -------------------------- config/routes.rb | 4 ++- 11 files changed, 56 insertions(+), 84 deletions(-) create mode 100644 app/views/shared/_profile.html.erb delete mode 100644 app/views/tweets/show.html.erb diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index df08d46ea..da7fff3ea 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,7 +1,8 @@ class TweetsController < ApplicationController - def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + def index # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @users = User.order(followers_count: :desc).limit(10) + @tweet = Tweet.new end def create @@ -13,4 +14,4 @@ def like def unlike end -end +end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 79422d9ef..fa0ce400a 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,37 +1,18 @@ class UsersController < ApplicationController - + before_action :set_user, only: [:edit, :update] def tweets end - - def edit - @user = User.find(params[:id]) - - unless @user.name - @user.name = @user.email.split('@').first - end - - unless @user.id == current_user.id - flash[:notice] = "Don't Allow!"; - # redirect_to user_path(@user) - redirect_back(fallback_location: tweets_user_path(current_user)) - end - end - def update - @user = User.find(params[:id]) if @user.update(user_params) - #redirect_to user_path(@user.id) - redirect_back(fallback_location: tweets_user_path(@user)) - flash[:notice] = "user was successfully updated" + redirect_to root_path, notice: "Profile Updated " else - redirect_back(fallback_location: tweets_user_path(@user)) - flash[:alert] = "failed" + flash[:alert] = @user.errors.full_messages.to_sentence + + render :edit end end - - def followings @followings # 基於測試規格,必須講定變數名稱 end @@ -44,6 +25,12 @@ def likes @likes # 基於測試規格,必須講定變數名稱 end - private + private + def set_user + @user = User.find(params[:id]) + end + def user_params + params.require(:user).permit(:name, :introduction, :avatar) + end end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6715fada2..66a52974e 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,4 +1,5 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 + belongs_to :user end diff --git a/app/models/user.rb b/app/models/user.rb index 6c5912e93..d519be368 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,6 +13,8 @@ class User < ApplicationRecord validates_uniqueness_of :email # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + has_many :tweets, dependent: :destroy + def admin? self.role== "admin" end diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 2ea8e5bb9..cf618d868 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,4 +1,5 @@
    -

    後台

    + +

    後台

    \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b8eb020ac..634732f33 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -31,10 +31,14 @@ <% if current_user.admin? %>
  • <%= link_to 'Admin後台', admin_tweets_path %>
  • <% end %> -
  • -
  • <%= link_to('修改個人資料', user_path(current_user)) %>
  • -
  • <%= link_to('Logoout', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • +
  • <%= link_to('Logoout', destroy_user_session_path, method: :delete, class: "pull-right btn btn-default ") + %>
  • <% else %>
  • <%= link_to('註冊', new_user_registration_path) %>
  • <%= link_to('登入', new_user_session_path) %>
  • diff --git a/app/views/shared/_profile.html.erb b/app/views/shared/_profile.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 9c2169a22..f45d7767c 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,6 +1,29 @@
    -

    前台

    +
    + <%= form_for @tweet, remote: true do |f| %> +
    + <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %> +
    +
    + <%= f.submit "Tweet", :disable_with => 'Tweeting...', remote: true, class: "pull-right btn btn-primary my-btn btn-lg" %> +
    + <% end %> +
    + +
    + +
    +
    \ No newline at end of file diff --git a/app/views/tweets/show.html.erb b/app/views/tweets/show.html.erb deleted file mode 100644 index 2736b79f2..000000000 --- a/app/views/tweets/show.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -
    -
    -

    <%= @tweets.name %>

    - -
    - -
    \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 76c4875f7..e69de29bb 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -1,42 +0,0 @@ -
    -
    -
    - - <% if @user.errors.any? %> -

    Find some errors !

    -
      - <% @user.errors.full_messages.each do |msg| %> -
    • <%= msg %>
    • - <% end %> -
    -
    - <% end %> - - <%= form_for @user do |f| %> -
    - -
    - <%= f.label :name, "Name" %> - <%= f.text_field :name, class: "form-control" %> -
    - -
    - <%= f.label :introduction, "Intro" %> - <%= f.text_area :introduction, class: "form-control" %> -
    - -
    - <%= f.label :avatar, "Avatar" %> - <%= image_tag @user.avatar, width: '200px' if @user.avatar? %> - <%= f.file_field :avatar %> -
    - -
    - <%= f.submit class: "btn btn-primary" %> - <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> -
    - <% end %> -
    -
    -
    -
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index b4197c6cf..d8961d07a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,9 @@ resources :tweets, only: [:index, :create] root "tweets#index" - resources :users, only: [:edit, :update ] + resources :users, only: [:edit, :update] + + resources :tweets, only: [:index, :show, :create] namespace :admin do resources :tweets From 79da53f587c698778414c852eed3576397d6f773 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 18 Jul 2018 17:43:28 +0800 Subject: [PATCH 09/48] complete twitter index for public users --- 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 f45d7767c..7807c6593 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -17,7 +17,7 @@ <% @users.each do |user| %>
    - <%= truncate user.introduction, length: 150 %> + <%= truncate user.introduction, length: 200 %>
    <% end %> From e72edf0d10e1cfaab9188ae24aee89d1a349dbfc Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 19 Jul 2018 12:33:06 +0800 Subject: [PATCH 10/48] add rails dev:fake_tweet --- app/views/tweets/index.html.erb | 14 ++++++-------- app/views/users/edit.html.erb | 21 +++++++++++++++++++++ lib/tasks/dev.rake | 23 +++++++++++++++++++---- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 7807c6593..25fa4d04b 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -6,24 +6,22 @@ <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %>
    - <%= f.submit "Tweet", :disable_with => 'Tweeting...', remote: true, class: "pull-right btn btn-primary my-btn btn-lg" %> + <%= f.submit "Tweet", class: "pull-right btn btn-primary my-btn btn-lg" %>
    - <% end %> -
    + <% end %> +
    - - - \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index e69de29bb..d69051fc8 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -0,0 +1,21 @@ +
    + <%= form_for @user do |f| %> +
    +
    + <%= image_tag @user.avatar, width: "100%" if @user.avatar? %>
    + +
    +
    +
    +
    + <%= f.text_field :name, placeholder: "Name", class: "form-control text-field" %> +
    +
    + <%= f.text_area :introduction, placeholder: "About Me....", class: "form-control text-area" %> +
    +
    + <%= f.submit "Update", :disable_with => 'Submiting...', class: "btn btn-default my-btn pull-right btn-lg" %> +
    +
    + <% end %> +
    \ No newline at end of file diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index f5aafdcb3..8dc4251a4 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -11,17 +11,32 @@ namespace :dev do name = FFaker::Name::first_name file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") - user = User.new( + user.create( name: name, - email: "#{name}@example.co", + email: "#{name}@example.com", password: "12345678", introduction: FFaker::Lorem::sentence(30), avatar: file ) - + end user.save! - puts user.name + puts "created fake users" end + + task fake_tweet: :environment do + + User.all.each do | user | + Tweet.destroy_all + + 50.times do |i| + user.tweets.create!( + description: FFaker::Lorem::sentence(7), + ) + end + puts "created fake tweets" + end + + end end From bd574415d35818fec014397d41ebbd8c093aed48 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Jul 2018 10:04:58 +0800 Subject: [PATCH 11/48] add pagination using Kaminari gem --- Gemfile | 2 + Gemfile.lock | 13 +++++++ app/controllers/admin/tweets_controller.rb | 2 + app/controllers/tweets_controller.rb | 5 +++ app/views/admin/tweets/index.html.erb | 2 + app/views/tweets/index.html.erb | 11 +++--- app/views/tweets/show.html.erb | 44 ++++++++++++++++++++++ 7 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 app/views/tweets/show.html.erb diff --git a/Gemfile b/Gemfile index a8c6c69b1..34f1a04ca 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,8 @@ gem 'jbuilder', '~> 2.5' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +gem 'kaminari' + group :production do gem 'pg', '~> 0.20' end diff --git a/Gemfile.lock b/Gemfile.lock index 4659a1e34..5e2286d58 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -106,6 +106,18 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) + kaminari (1.1.1) + activesupport (>= 4.1.0) + kaminari-actionview (= 1.1.1) + kaminari-activerecord (= 1.1.1) + kaminari-core (= 1.1.1) + kaminari-actionview (1.1.1) + actionview + kaminari-core (= 1.1.1) + kaminari-activerecord (1.1.1) + activerecord + kaminari-core (= 1.1.1) + kaminari-core (1.1.1) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -273,6 +285,7 @@ DEPENDENCIES ffaker jbuilder (~> 2.5) jquery-rails + kaminari listen (>= 3.0.5, < 3.2) pg (~> 0.20) puma (~> 3.7) diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index d19b5f0ef..6eae14cb7 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,7 +1,9 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_user! before_action :authenticate_admin + def index + @tweets = Tweet.page(params[:page]).per(10) end def destroy diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index da7fff3ea..6e3bbfaae 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -6,6 +6,11 @@ def index # 基於測試規格,必須講定變數名稱,請用此變數中 end def create + @user = User.find(params:[:user_id]) + @tweet = @user.tweets.build(tweet_params) + @tweet.user = current_user + @tweet.save! + redirect_to user_path(@user) end def like diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index cf618d868..f31691489 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -2,4 +2,6 @@

    後台

    + + <%= paginate @restaurants %> \ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 25fa4d04b..0b3972e39 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,4 +1,4 @@ -
    +
    <%= form_for @tweet, remote: true do |f| %> @@ -6,13 +6,14 @@ <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %>
    - <%= f.submit "Tweet", class: "pull-right btn btn-primary my-btn btn-lg" %> + <%= f.submit "Tweet", class: "pull-right btn btn-primary my-btn btn-lg" %>
    <% end %>
    +
    - -
    \ No newline at end of file +
    +
    diff --git a/app/views/tweets/show.html.erb b/app/views/tweets/show.html.erb new file mode 100644 index 000000000..6b81cddc6 --- /dev/null +++ b/app/views/tweets/show.html.erb @@ -0,0 +1,44 @@ +
    +
    + +
    + + <%= image_tag @tweet.user.avatar, class: "img-responsive center-block" %> + +

    <%= @tweet.user.name %>

    +

    <%= @tweet.user.email %>

    +
    + + <% if @tweet.user.introduction == nil %> +

    未新增自我介紹

    + <% else %> +

    <%= @tweet.user.introduction %>

    + <% end %> +
    +
    + +
    +
    + +
    +

    Tweets

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

    <%= @tweet.user.name %> <%= @tweet.created_at.strftime('%F') %>, <%= @tweet.created_at.strftime('%H:%M') %>

    +

    <%= simple_format @tweet.description %>

    +
    回覆數(<% @tweet.count_replies %><%= @tweet.replies_count %>)
    +
    +
    +
    + +
    + + +
    + +
    +
    \ No newline at end of file From 11f3f2e63c47942950ba81f8ae745eeb477ca55d Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Jul 2018 10:42:50 +0800 Subject: [PATCH 12/48] add config route --- app/views/users/edit.html.erb | 2 +- config/routes.rb | 31 ++++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index d69051fc8..fb9f44495 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -2,7 +2,7 @@ <%= form_for @user do |f| %>
    - <%= image_tag @user.avatar, width: "100%" if @user.avatar? %>
    +
    diff --git a/config/routes.rb b/config/routes.rb index d8961d07a..82eeea7d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,15 +3,32 @@ devise_for :users # 請依照專案指定規格來設定路由 - resources :tweets, only: [:index, :create] - root "tweets#index" + root "tweets#index" - resources :users, only: [:edit, :update] - resources :tweets, only: [:index, :show, :create] + resources :tweets, only: [:index, :create] do + resources :replies, only: [:index, :create] - namespace :admin do - resources :tweets - root "tweets#index" + member do + post :like + post :unlike + end end + + resources :users, only: [:edit, :update] do + member do + get :tweets + get :followings + get :followers + get :likes + end + end + + resource :followships, only: [:create, :destroy] + + namespace :admin do + resources :tweets, only: [:index, :destroy] + resources :users, only:[:index] + end + end From ee8e42175b18bd9c41d8c77572dcd64e94e55ce8 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Jul 2018 11:22:33 +0800 Subject: [PATCH 13/48] =?UTF-8?q?set=20tweet=20model/controller(=E4=BE=9D?= =?UTF-8?q?=E6=99=82=E9=96=93=E6=8E=92=E5=BA=8F=E7=9A=84=E9=A1=AF=E7=A4=BA?= =?UTF-8?q?&=E9=99=90140=E5=AD=97)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/tweets_controller.rb | 9 ++++++++- app/models/tweet.rb | 2 +- app/views/tweets/index.html.erb | 12 ++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 6e3bbfaae..282294473 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,7 +1,7 @@ class TweetsController < ApplicationController def index # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 - @users = User.order(followers_count: :desc).limit(10) + @tweets = Tweet.order(updated_at: :desc).page(params[:page]).per(15) @tweet = Tweet.new end @@ -19,4 +19,11 @@ def like def unlike end + private + + def tweet_params + params.require(:tweet).permit(:description) + end + + end \ No newline at end of file diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 66a52974e..3815dc754 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,5 +1,5 @@ class Tweet < ApplicationRecord - validates_length_of :description, maximum: 140 + validates_length_of :description, maximum: 140,:message => "限字140" belongs_to :user end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 0b3972e39..739c80e63 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -12,17 +12,17 @@ +

    Hot Topics

    - <% @users.each do |user| %> -
    + <% @tweets.each do |tweet| %> +
    - <%= truncate user.introduction, length: 200 %> + <%= truncate tweet.description, length: 150 %>
    - <% end %> -
    + <% end %>
    -
    + From 1be127cb06d26ac4d7a1912973281c8f251a345c Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Jul 2018 11:32:05 +0800 Subject: [PATCH 14/48] add tweet index page --- app/views/tweets/index.html.erb | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 739c80e63..6d9ca7bc9 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -10,8 +10,24 @@ <% end %> +
    +
    + <%@tweets.each do |tweet| %> +
    +
    + <%=image_tag tweet.user.avatar, class:"img-responsive" %> +
    + +

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

    +

    +

    <%=tweet.description%>

    +
    +
    + <%end%> +
    +
    From 2b93f6c1370c53a490e6b4dd2f810dc14255ef77 Mon Sep 17 00:00:00 2001 From: Henry Date: Fri, 20 Jul 2018 16:05:16 +0800 Subject: [PATCH 15/48] setup relation among user,tweet and reply --- app/assets/stylesheets/application.scss | 3 ++- app/assets/stylesheets/style.scss | 6 ++++++ app/models/reply.rb | 2 ++ app/models/tweet.rb | 2 ++ app/models/user.rb | 2 ++ 5 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/style.scss diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss index 9a3100e97..f71b82d56 100644 --- a/app/assets/stylesheets/application.scss +++ b/app/assets/stylesheets/application.scss @@ -13,4 +13,5 @@ */ @import 'bootstrap-sprockets'; -@import 'bootstrap'; \ No newline at end of file +@import 'bootstrap'; +@import 'style'; \ No newline at end of file diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss new file mode 100644 index 000000000..2291072b5 --- /dev/null +++ b/app/assets/stylesheets/style.scss @@ -0,0 +1,6 @@ + +.item-info{ + font-size: 9x; + font-weight: bold; + color: #3b70c9; +} diff --git a/app/models/reply.rb b/app/models/reply.rb index bae6f9463..223589f44 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,2 +1,4 @@ class Reply < ApplicationRecord + belongs_to :user + belongs_to :tweet end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 3815dc754..1c44ad22f 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -2,4 +2,6 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140,:message => "限字140" belongs_to :user + has_many :replies, dependent: :destroy + end diff --git a/app/models/user.rb b/app/models/user.rb index d519be368..f424386c4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,6 +15,8 @@ class User < ApplicationRecord has_many :tweets, dependent: :destroy + has_many :replies, dependent: :destroy + def admin? self.role== "admin" end From bffd69a1e7bc6d3eeebb070c594d6e024e29e686 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 23 Jul 2018 09:16:14 +0800 Subject: [PATCH 16/48] add twitter index and some reply fuction --- app/controllers/replies_controller.rb | 19 ++++++++++-- app/controllers/tweets_controller.rb | 22 ++++++++------ app/views/replies/index.html.erb | 10 ++++++ app/views/shared/_profile.html.erb | 0 app/views/tweets/index.html.erb | 10 +++--- app/views/tweets/show.html.erb | 44 --------------------------- lib/tasks/dev.rake | 17 +++++------ 7 files changed, 52 insertions(+), 70 deletions(-) create mode 100644 app/views/replies/index.html.erb delete mode 100644 app/views/shared/_profile.html.erb delete mode 100644 app/views/tweets/show.html.erb diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index a9b6a315b..213bb43bf 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,9 +1,24 @@ class RepliesController < ApplicationController - + def index + @tweet = Tweet.find(params[:tweet_id]) + @user = @tweet.user + @replies = @tweet.replies.order(updated_at: :desc) + @reply = Reply.new end def create + @tweet = Tweet.find(params[:tweet_id]) + @reply = tweet.replies.create(reply_params) + @reply.user = current_user + reply.save + redirect_to tweet_replies_path(@tweet) + end + + private + + def reply_params + params.require(:reply).permit(:comment) end -end +end \ No newline at end of file diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 282294473..a088ae4f4 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,16 +1,20 @@ class TweetsController < ApplicationController - def index # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 - @tweets = Tweet.order(updated_at: :desc).page(params[:page]).per(15) + def index + @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweet = Tweet.new + @tweets = Tweet.order(created_at: :desc) end def create - @user = User.find(params:[:user_id]) - @tweet = @user.tweets.build(tweet_params) - @tweet.user = current_user - @tweet.save! - redirect_to user_path(@user) + @tweet = current_user.tweets.build(tweet_params) + if @tweet.save + flash[:notice] = "Tweet successfully!!!" + redirect_back fallback_location: root_path + else + flash[:alert] = "Your tweet is more than 140 characters!!!Please simplify your tweet." + render :index + end end def like @@ -19,11 +23,9 @@ def like def unlike end - private + private def tweet_params params.require(:tweet).permit(:description) end - - end \ No newline at end of file diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb new file mode 100644 index 000000000..90b222dc0 --- /dev/null +++ b/app/views/replies/index.html.erb @@ -0,0 +1,10 @@ + +
    +
    + <%=image_tag reply.user.avatar, class:"img-responsive" %> +
    +

    + <%="@"+reply.user.name + ", "%><%=reply.updated_at.strftime("%Y-%m-%d,%H:%M")%>

    +

    <%=reply.comment%>

    +
    +
    \ No newline at end of file diff --git a/app/views/shared/_profile.html.erb b/app/views/shared/_profile.html.erb deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 6d9ca7bc9..d981d8c70 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,7 +1,7 @@
    - <%= form_for @tweet, remote: true do |f| %> + <%= form_for @tweet do |f| %>
    <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %>
    @@ -12,9 +12,8 @@

    - -
    - <%@tweets.each do |tweet| %> +
    + <% @tweets.each do |tweet| %>
    <%=image_tag tweet.user.avatar, class:"img-responsive" %> @@ -29,6 +28,7 @@
    +

    Hot Topics

    @@ -41,4 +41,4 @@ <% end %>
    -
    +
    \ No newline at end of file diff --git a/app/views/tweets/show.html.erb b/app/views/tweets/show.html.erb deleted file mode 100644 index 6b81cddc6..000000000 --- a/app/views/tweets/show.html.erb +++ /dev/null @@ -1,44 +0,0 @@ -
    -
    - -
    - - <%= image_tag @tweet.user.avatar, class: "img-responsive center-block" %> - -

    <%= @tweet.user.name %>

    -

    <%= @tweet.user.email %>

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

    未新增自我介紹

    - <% else %> -

    <%= @tweet.user.introduction %>

    - <% end %> -
    -
    - -
    -
    - -
    -

    Tweets

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

    <%= @tweet.user.name %> <%= @tweet.created_at.strftime('%F') %>, <%= @tweet.created_at.strftime('%H:%M') %>

    -

    <%= simple_format @tweet.description %>

    -
    回覆數(<% @tweet.count_replies %><%= @tweet.replies_count %>)
    -
    -
    -
    - -
    - - -
    - -
    -
    \ No newline at end of file diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 8dc4251a4..4d2ee3f0b 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -11,7 +11,7 @@ namespace :dev do name = FFaker::Name::first_name file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") - user.create( + user.create!( name: name, email: "#{name}@example.com", password: "12345678", @@ -23,19 +23,18 @@ namespace :dev do puts "created fake users" end - task fake_tweet: :environment do + task fake_tweet: :environment do + Tweet.destroy_all - User.all.each do | user | - Tweet.destroy_all - - 50.times do |i| + User.all.each do |user| + 3.times do |i| user.tweets.create!( - description: FFaker::Lorem::sentence(7), + description: FFaker::Lorem::phrase(10) ) - end - puts "created fake tweets" end + puts "reated fake tweets" + end end From 20f4a371f6bc7dd3fed65a78cd85af80e1d74de8 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 23 Jul 2018 14:43:58 +0800 Subject: [PATCH 17/48] update profile --- app/assets/stylesheets/style.scss | 2 ++ app/controllers/admin/base_controller.rb | 1 - app/controllers/admin/tweets_controller.rb | 10 ++++++++-- app/controllers/admin/users_controller.rb | 3 +++ app/controllers/replies_controller.rb | 10 ++++++---- app/controllers/tweets_controller.rb | 9 +++++---- app/controllers/users_controller.rb | 9 ++++++++- app/models/reply.rb | 2 +- app/models/tweet.rb | 1 + app/models/user.rb | 1 + app/views/admin/tweets/index.html.erb | 22 +++++++++++++++------- app/views/kaminari/_first_page.html.erb | 3 +++ app/views/kaminari/_gap.html.erb | 3 +++ app/views/kaminari/_last_page.html.erb | 3 +++ app/views/kaminari/_next_page.html.erb | 3 +++ app/views/kaminari/_page.html.erb | 9 +++++++++ app/views/kaminari/_paginator.html.erb | 15 +++++++++++++++ app/views/kaminari/_prev_page.html.erb | 3 +++ app/views/replies/index.html.erb | 20 ++++++++++++-------- app/views/tweets/index.html.erb | 11 +++++++---- app/views/users/edit.html.erb | 7 ++++--- lib/tasks/dev.rake | 1 + 22 files changed, 113 insertions(+), 35 deletions(-) create mode 100644 app/views/kaminari/_first_page.html.erb create mode 100644 app/views/kaminari/_gap.html.erb create mode 100644 app/views/kaminari/_last_page.html.erb create mode 100644 app/views/kaminari/_next_page.html.erb create mode 100644 app/views/kaminari/_page.html.erb create mode 100644 app/views/kaminari/_paginator.html.erb create mode 100644 app/views/kaminari/_prev_page.html.erb diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 2291072b5..1c992064e 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -4,3 +4,5 @@ font-weight: bold; color: #3b70c9; } + + diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 4a89583f5..61c9a1463 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,3 +1,2 @@ class Admin::BaseController < ApplicationController - end diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 6eae14cb7..e1739df10 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -3,9 +3,15 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_admin def index - @tweets = Tweet.page(params[:page]).per(10) + @tweets = Tweet.all end - def destroy + def destroy + @tweet = Tweet.find(params[:id]) + @tweet.destroy + flash[:alert] = "Tweet was deleted!" + redirect_to admin_root_path(t) end + + end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 3ba9f0a36..e2f4e5d0a 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,4 +1,7 @@ class Admin::UsersController < Admin::BaseController + def index + @users = User.all end + end diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 213bb43bf..ea2cf1f64 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -2,17 +2,19 @@ class RepliesController < ApplicationController def index @tweet = Tweet.find(params[:tweet_id]) - @user = @tweet.user @replies = @tweet.replies.order(updated_at: :desc) @reply = Reply.new end def create @tweet = Tweet.find(params[:tweet_id]) - @reply = tweet.replies.create(reply_params) + @reply = @tweet.replies.build(reply_params) @reply.user = current_user - reply.save - redirect_to tweet_replies_path(@tweet) + if @reply.save! + redirect_to tweet_replies_path(@tweet) + else + render :index + end end private diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index a088ae4f4..01d12ed9a 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,9 +1,10 @@ class TweetsController < ApplicationController def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweet = Tweet.new - @tweets = Tweet.order(created_at: :desc) + @tweets = Tweet.all.order(created_at: :desc) + end def create @@ -12,8 +13,7 @@ def create flash[:notice] = "Tweet successfully!!!" redirect_back fallback_location: root_path else - flash[:alert] = "Your tweet is more than 140 characters!!!Please simplify your tweet." - render :index + flash[:alert] = "限字140" end end @@ -28,4 +28,5 @@ def unlike def tweet_params params.require(:tweet).permit(:description) end + end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index fa0ce400a..a804ebd05 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,6 +1,12 @@ class UsersController < ApplicationController - before_action :set_user, only: [:edit, :update] + before_action :set_user, only: [:tweets, :edit, :update, :followings, :followers, :likes] + def tweets + if @user == current_user + @tweets = current_user.tweets.order(created_at: :desc) + else + @tweets = @user.tweets.order(created_at: :desc) + end end def update @@ -26,6 +32,7 @@ def likes end private + def set_user @user = User.find(params[:id]) end diff --git a/app/models/reply.rb b/app/models/reply.rb index 223589f44..dd3679577 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,4 +1,4 @@ class Reply < ApplicationRecord belongs_to :user - belongs_to :tweet + belongs_to :tweet, counter_cache: true end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 1c44ad22f..e1a42c379 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -3,5 +3,6 @@ class Tweet < ApplicationRecord belongs_to :user has_many :replies, dependent: :destroy + has_many :replied_users, through: :replies, source: :user end diff --git a/app/models/user.rb b/app/models/user.rb index f424386c4..be84023b9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -16,6 +16,7 @@ class User < ApplicationRecord has_many :tweets, dependent: :destroy has_many :replies, dependent: :destroy + has_many :replied_tweets, through: :replies, source: :tweet def admin? self.role== "admin" diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index f31691489..38cd29968 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,7 +1,15 @@ -
    - -

    後台

    - - - <%= paginate @restaurants %> -
    \ No newline at end of file +
    +

    後台

    + +
    + + +
    + \ No newline at end of file diff --git a/app/views/kaminari/_first_page.html.erb b/app/views/kaminari/_first_page.html.erb new file mode 100644 index 000000000..2c5985bc9 --- /dev/null +++ b/app/views/kaminari/_first_page.html.erb @@ -0,0 +1,3 @@ +
  • + <%= link_to_unless current_page.first?, raw(t 'views.pagination.first'), url, remote: remote %> +
  • diff --git a/app/views/kaminari/_gap.html.erb b/app/views/kaminari/_gap.html.erb new file mode 100644 index 000000000..6d3a1490c --- /dev/null +++ b/app/views/kaminari/_gap.html.erb @@ -0,0 +1,3 @@ +
  • + <%= content_tag :a, raw(t 'views.pagination.truncate') %> +
  • diff --git a/app/views/kaminari/_last_page.html.erb b/app/views/kaminari/_last_page.html.erb new file mode 100644 index 000000000..aee252478 --- /dev/null +++ b/app/views/kaminari/_last_page.html.erb @@ -0,0 +1,3 @@ +
  • + <%= link_to_unless current_page.last?, raw(t 'views.pagination.last'), url, { remote: remote } %> +
  • diff --git a/app/views/kaminari/_next_page.html.erb b/app/views/kaminari/_next_page.html.erb new file mode 100644 index 000000000..8e9a10e6a --- /dev/null +++ b/app/views/kaminari/_next_page.html.erb @@ -0,0 +1,3 @@ +
  • + <%= link_to_unless current_page.last?, raw(t 'views.pagination.next'), url, rel: 'next', remote: remote %> +
  • diff --git a/app/views/kaminari/_page.html.erb b/app/views/kaminari/_page.html.erb new file mode 100644 index 000000000..19c420634 --- /dev/null +++ b/app/views/kaminari/_page.html.erb @@ -0,0 +1,9 @@ +<% if page.current? %> +
  • + <%= content_tag :a, page, data: { remote: remote }, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)) %> +
  • +<% else %> +
  • + <%= link_to page, url, remote: remote, rel: (page.next? ? 'next' : (page.prev? ? 'prev' : nil)) %> +
  • +<% end %> diff --git a/app/views/kaminari/_paginator.html.erb b/app/views/kaminari/_paginator.html.erb new file mode 100644 index 000000000..2c8757b4f --- /dev/null +++ b/app/views/kaminari/_paginator.html.erb @@ -0,0 +1,15 @@ +<%= paginator.render do -%> +
      + <%= first_page_tag unless current_page.first? %> + <%= prev_page_tag unless current_page.first? %> + <% each_page do |page| -%> + <% if page.left_outer? || page.right_outer? || page.inside_window? -%> + <%= page_tag page %> + <% elsif !page.was_truncated? -%> + <%= gap_tag %> + <% end -%> + <% end -%> + <%= next_page_tag unless current_page.last? %> + <%= last_page_tag unless current_page.last? %> +
    +<% end -%> diff --git a/app/views/kaminari/_prev_page.html.erb b/app/views/kaminari/_prev_page.html.erb new file mode 100644 index 000000000..f16f6b4bd --- /dev/null +++ b/app/views/kaminari/_prev_page.html.erb @@ -0,0 +1,3 @@ +
  • + <%= link_to_unless current_page.first?, raw(t 'views.pagination.previous'), url, rel: 'prev', remote: remote %> +
  • diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 90b222dc0..2a99d39c9 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1,10 +1,14 @@ -
    -
    - <%=image_tag reply.user.avatar, class:"img-responsive" %> +
    + <%= form_for [@tweet, @reply] do |f| %> +
    + <%= f.text_area :comment, class: "form-control"%> +
    +
    + <%= f.submit "Reply", class: "btn" %> + <%= f.button "Cancel", type: :reset, class: "btn" %> +
    + <% end %>
    -

    - <%="@"+reply.user.name + ", "%><%=reply.updated_at.strftime("%Y-%m-%d,%H:%M")%>

    -

    <%=reply.comment%>

    -
    -
    \ No newline at end of file +
    +
    \ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index d981d8c70..1b085ffaf 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -5,8 +5,9 @@
    <%= f.text_area :description, placeholder: "What's on your mind?", class: "form-control text-area" %>
    -
    - <%= f.submit "Tweet", class: "pull-right btn btn-primary my-btn btn-lg" %> +
    + <%= f.submit "Tweet", class: "pull-right btn btn-primary my-btn btn-lg" %> + <%= f.button "Cancel", type: :reset, class: "pull-right my-btn btn-lg" %>
    <% end %>
    @@ -22,6 +23,7 @@

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

    <%=tweet.description%>

    + <%= link_to "Reply(#{tweet.replies_count})", tweet_replies_path(tweet) %>
    <%end%> @@ -29,8 +31,8 @@
    -
    -
    +
    +

    Hot Topics

    <% @tweets.each do |tweet| %>
    @@ -41,4 +43,5 @@ <% end %>
    +
    \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index fb9f44495..aa337de47 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -2,7 +2,7 @@ <%= form_for @user do |f| %>
    - + <%= f.label :avatar, class: "none"%>
    @@ -13,8 +13,9 @@
    <%= f.text_area :introduction, placeholder: "About Me....", class: "form-control text-area" %>
    -
    - <%= f.submit "Update", :disable_with => 'Submiting...', class: "btn btn-default my-btn pull-right btn-lg" %> +
    + <%= f.submit "Update", class: "btn"%> + <%= f.button "Cancel", type: :reset, class: "btn" %>
    <% end %> diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 4d2ee3f0b..def0683fb 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -34,6 +34,7 @@ namespace :dev do end puts "reated fake tweets" + puts "now you have #{Tweet.count} users data" end end From c872987d9f3bc949b9efa1ee243b07db8cfdcfdd Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 23 Jul 2018 14:58:26 +0800 Subject: [PATCH 18/48] create user index page for admin --- app/views/admin/users/index.html.erb | 35 ++++++++++++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 36 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..21aa2f3e7 --- /dev/null +++ b/app/views/admin/users/index.html.erb @@ -0,0 +1,35 @@ +
    +

    後台

    + +
    + + + +
    + + + + + + + + + + + + <% @users.each do |u| %> + + + + + <% end %> + +
    nametweetsfollowingsfollowerslikes
    <%= u.name %>
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 82eeea7d2..b1b47c7ac 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,6 +29,7 @@ namespace :admin do resources :tweets, only: [:index, :destroy] resources :users, only:[:index] + root "tweets#index" end end From 58ac26aacdc8a396f5ca107d5612b77d592c7a46 Mon Sep 17 00:00:00 2001 From: Henry Date: Mon, 23 Jul 2018 15:15:39 +0800 Subject: [PATCH 19/48] create tweet index page for admin --- app/views/admin/tweets/index.html.erb | 29 ++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 38cd29968..d9da84187 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,8 +1,6 @@

    後台

    - -
    - +
    -
    - \ No newline at end of file +
    + + + + + + + + + + + + + + <% @tweets.each do |t| %> + + + + + <% end %> + +
    usercontenttweet timelikesrepliesdelete
    <%= t.user.name %><%= t.description %>
    +
    \ No newline at end of file From 86ea870c98d1d17b371c36952e4cafc4d1664b4d Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 24 Jul 2018 10:32:46 +0800 Subject: [PATCH 20/48] setup relation among user,tweet and like --- app/models/like.rb | 3 +++ app/models/tweet.rb | 3 +++ app/models/user.rb | 3 +++ 3 files changed, 9 insertions(+) diff --git a/app/models/like.rb b/app/models/like.rb index d99b93a32..3d87d154d 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,2 +1,5 @@ class Like < ApplicationRecord + belongs_to :user + belongs_to :tweet, counter_cache: true + end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index e1a42c379..bc2f6828c 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -5,4 +5,7 @@ class Tweet < ApplicationRecord has_many :replies, dependent: :destroy has_many :replied_users, through: :replies, source: :user + has_many :likes, dependent: :destroy + has_many :liked_users, through: :likes, source: :user + end diff --git a/app/models/user.rb b/app/models/user.rb index be84023b9..d9abbffa6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,9 @@ class User < ApplicationRecord has_many :replies, dependent: :destroy has_many :replied_tweets, through: :replies, source: :tweet + has_many :likes, dependent: :destroy + has_many :liked_tweets, through: :likes, source: :tweet + def admin? self.role== "admin" end From e93ac76cfab0fe659d985cb575bceac814ee7739 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 24 Jul 2018 11:42:00 +0800 Subject: [PATCH 21/48] add like/unlike button & add tweets counnt on user model --- app/assets/stylesheets/style.scss | 9 +++++++++ app/controllers/tweets_controller.rb | 13 +++++++++++-- app/controllers/users_controller.rb | 3 ++- app/models/reply.rb | 2 +- app/models/tweet.rb | 6 +++++- app/views/tweets/index.html.erb | 6 ++++++ .../20180724032324_add_tweets_count_on_user.rb | 5 +++++ db/schema.rb | 3 ++- 8 files changed, 41 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20180724032324_add_tweets_count_on_user.rb diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 1c992064e..b3aa9c6fb 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -5,4 +5,13 @@ color: #3b70c9; } +.like { + padding: 0 5px; + color: #f00; +} + +.unlike { + padding: 0 5px; + color: #0f1; +} diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 01d12ed9a..c7c2e7d95 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,10 +1,10 @@ class TweetsController < ApplicationController + before_action :set_tweet, only: [:like, :unlike] def index @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweet = Tweet.new @tweets = Tweet.all.order(created_at: :desc) - end def create @@ -17,14 +17,23 @@ def create end end - def like + def like + @tweet.likes.create!(user: current_user) + redirect_back fallback_location: tweets_path end def unlike + like = Like.where(tweet: @tweet, user: current_user) + like.destroy_all + redirect_back fallback_location: tweets_path end private + def set_tweet + @tweet = Tweet.find(params[:id]) + end + def tweet_params params.require(:tweet).permit(:description) end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a804ebd05..c7a506901 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -28,7 +28,8 @@ def followers end def likes - @likes # 基於測試規格,必須講定變數名稱 + @likes = @user.liked_tweets.order('likes.created_at desc') + # 基於測試規格,必須講定變數名稱 end private diff --git a/app/models/reply.rb b/app/models/reply.rb index dd3679577..1698fc959 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,4 +1,4 @@ class Reply < ApplicationRecord - belongs_to :user + belongs_to :user, counter_cache: true belongs_to :tweet, counter_cache: true end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index bc2f6828c..69677b8fd 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,6 +1,6 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140,:message => "限字140" - belongs_to :user + belongs_to :user, counter_cache: true has_many :replies, dependent: :destroy has_many :replied_users, through: :replies, source: :user @@ -8,4 +8,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 diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 1b085ffaf..37d7f1317 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -24,6 +24,12 @@

    <%=tweet.description%>

    <%= link_to "Reply(#{tweet.replies_count})", tweet_replies_path(tweet) %> + <% if tweet.is_liked?(current_user) %> + <%= link_to "Unlike(#{tweet.likes_count})", unlike_tweet_path(tweet), method: :post,class: "unlike" %> + <% else %> + <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet), method: :post, class: "like" %> + <% end %> +
    <%end%> diff --git a/db/migrate/20180724032324_add_tweets_count_on_user.rb b/db/migrate/20180724032324_add_tweets_count_on_user.rb new file mode 100644 index 000000000..6da80effa --- /dev/null +++ b/db/migrate/20180724032324_add_tweets_count_on_user.rb @@ -0,0 +1,5 @@ +class AddTweetsCountOnUser < 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..dee4dc5e6 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: 20180724032324) 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 07a77e979f0056806e0b1fcd6a08233804390791 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 24 Jul 2018 17:21:39 +0800 Subject: [PATCH 22/48] setup relation between user and followship --- app/models/followship.rb | 4 ++++ app/models/user.rb | 12 ++++++++++++ app/views/tweets/index.html.erb | 7 ++----- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/models/followship.rb b/app/models/followship.rb index 1aed01396..e5017a52c 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,4 +1,8 @@ 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 d9abbffa6..858ecb4c3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -15,16 +15,28 @@ class User < ApplicationRecord has_many :tweets, dependent: :destroy + # 關聯名稱自訂為 : replied_tweets,需要用 source 來告知 model name 位置 has_many :replies, dependent: :destroy has_many :replied_tweets, through: :replies, source: :tweet has_many :likes, dependent: :destroy has_many :liked_tweets, through: :likes, source: :tweet + # 不需要另加 source (其沒有自訂名稱),Rails 可自動推論指向 User Model + has_many :followships, dependent: :destroy + has_many :followings, through: :followships + + # 使用者追蹤設定 + has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" + has_many :followers, through: :inverse_followships, source: :user + def admin? self.role== "admin" end +def following?(user) + self.followings.include?(user) +end end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 37d7f1317..454e459a4 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -36,8 +36,7 @@
    - -
    +

    Hot Topics

    <% @tweets.each do |tweet| %> @@ -48,6 +47,4 @@
    <% end %>
    -
    - -
    \ No newline at end of file +
    \ No newline at end of file From d4437f498edea44c41941ee47c6151062d7d3c62 Mon Sep 17 00:00:00 2001 From: Henry Date: Tue, 24 Jul 2018 18:02:28 +0800 Subject: [PATCH 23/48] user can view top10 followers on ranking page --- app/controllers/tweets_controller.rb | 2 +- app/views/tweets/index.html.erb | 32 ++++++++++++++++------- config/routes.rb | 39 ++++++++++++++++++---------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index c7c2e7d95..155b902a8 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -2,7 +2,7 @@ class TweetsController < ApplicationController before_action :set_tweet, only: [:like, :unlike] def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @users = User.order(followers_count: :desc).limit(10) # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 @tweet = Tweet.new @tweets = Tweet.all.order(created_at: :desc) end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 454e459a4..22356ed7b 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -29,22 +29,36 @@ <% else %> <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet), method: :post, class: "like" %> <% end %> -
    <%end%> -
    -
    -

    Hot Topics

    - <% @tweets.each do |tweet| %> -
    -
    - <%= truncate tweet.description, length: 150 %> +
    +

    Popular

    + <% @users.each do |u| %> +
    +
    +
    + <%= image_tag u.avatar %> +
    +
    + <%= link_to "@#{u.name}", tweets_user_path(u), class: "name" %> +

    <%= u.introduction %>

    +
    + <% if u != current_user %> + <% if current_user.following?(u) %> + <%= link_to "Unfollow", followship_path(u), method: :delete, class: "btn" %> + <% else %> + <%= link_to "Follow", followships_path(following_id: u), method: :post, class: "btn" %> + <% end %> + <% end %> +
    +
    <% end %>
    -
    \ No newline at end of file +
    +
    diff --git a/config/routes.rb b/config/routes.rb index b1b47c7ac..f6115021e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,35 +1,46 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users + resources :users, only: [:edit, :update] do + + member do + + # user page + get :tweets + + # following and follower pages + get :followings + get :followers - # 請依照專案指定規格來設定路由 - root "tweets#index" + # likes page + get :likes + + end + end + # 請依照專案指定規格來設定路由 resources :tweets, only: [:index, :create] do resources :replies, only: [:index, :create] - member do + member do + + # like button post :like post :unlike - end - end - resources :users, only: [:edit, :update] do - member do - get :tweets - get :followings - get :followers - get :likes end + end - resource :followships, only: [:create, :destroy] + resources :followships, only: [:create, :destroy] + + root "tweets#index" namespace :admin do resources :tweets, only: [:index, :destroy] - resources :users, only:[:index] + resources :users, only: [:index] root "tweets#index" end -end +end \ No newline at end of file From cac48d7b7431ed87d4440096d6713d0db4518643 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 11:01:09 +0800 Subject: [PATCH 24/48] Test --- db/seeds.rb | 3 ++- lib/tasks/dev.rake | 18 ++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/db/seeds.rb b/db/seeds.rb index 19f60166b..93be138bc 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,5 +6,6 @@ # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) -User.create(email: "admin@aaa.com", password: "asdasd", role: "admin") + +User.create(name:'Mike', email:'admin@aaa.com', password:'asdasd',introduction:'This is admin',role:'admin' ) puts "Default admin created!" \ No newline at end of file diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index def0683fb..0b4d59ef9 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,26 +2,24 @@ namespace :dev do # 請先執行 rails dev:fake_user,可以產生 20 個資料完整的 User 紀錄 # 其他測試用的假資料請依需要自行撰寫 task fake_user: :environment do - - User.all.each do | user | - user.destroy unless user.admin? - end + User.destroy_all 20.times do |i| name = FFaker::Name::first_name - file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") - - user.create!( + file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") + user = User.new( name: name, - email: "#{name}@example.com", + email: "#{name}@example.co", password: "12345678", introduction: FFaker::Lorem::sentence(30), avatar: file ) - end + user.save! - puts "created fake users" + puts user.name end + end + task fake_tweet: :environment do Tweet.destroy_all From aa104c6f0feee1ee8a16a95ac8b4c5e9569883d2 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 11:30:13 +0800 Subject: [PATCH 25/48] can't write unknown attribute --- app/controllers/replies_controller.rb | 20 ++++++------- app/views/replies/index.html.erb | 41 ++++++++++++++++++++++++++- lib/tasks/dev.rake | 15 ++++++++++ 3 files changed, 63 insertions(+), 13 deletions(-) diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index ea2cf1f64..777a41a87 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,26 +1,22 @@ class RepliesController < ApplicationController - + def index @tweet = Tweet.find(params[:tweet_id]) - @replies = @tweet.replies.order(updated_at: :desc) + @replies = @tweet.replies @reply = Reply.new end def create - @tweet = Tweet.find(params[:tweet_id]) - @reply = @tweet.replies.build(reply_params) - @reply.user = current_user - if @reply.save! - redirect_to tweet_replies_path(@tweet) - else - render :index - end + tweet = Tweet.find(params[:tweet_id]) + reply = tweet.replies.create(reply_params) + reply.user = current_user + reply.save + redirect_to tweet_replies_path(tweet) end - private +private def reply_params params.require(:reply).permit(:comment) end - end \ No newline at end of file diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 2a99d39c9..c68077345 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1,4 +1,43 @@ - +
    +
    + <%= image_tag @tweet.user.avatar %> +

    <%= @tweet.user.name %>

    +

    <%= @tweet.user.introduction %>

    +
    +

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

    +

    Following<%= @tweet.user.followings.count %>

    +

    Follower<%= @tweet.user.followers_count %>

    +

    Like<%= @tweet.user.likes_count %>

    +
    +
    +
    +

    tweets

    +
    +
    +
    + <%= image_tag @tweet.user.avatar %> +
    +
    + <%= link_to "@#{@tweet.user.name},#{@tweet.created_at.to_s(:db)}", tweets_user_path(@tweet.user) %> +

    <%= @tweet.description %>

    + <%= link_to "Reply(#{@tweet.replies_count})", tweet_replies_path(@tweet) %> +
    +
    +
    +

    replies

    + <% @replies.each do |t| %> +
    +
    +
    + <%= image_tag t.user.avatar %> +
    +
    + <%= link_to "@#{t.user.name},#{t.created_at.to_s(:db)}", tweets_user_path(t.user) %> +

    <%= t.comment %>

    +
    +
    +
    + <% end %>
    <%= form_for [@tweet, @reply] do |f| %>
    diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 0b4d59ef9..e3bfcdaec 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -34,7 +34,22 @@ namespace :dev do puts "reated fake tweets" puts "now you have #{Tweet.count} users data" end +end + + task fake_reply: :environment do + Reply.destroy_all + Tweet.all.each do |r| + 3.times do |i| + r.replies.create!( + comment: FFaker::Lorem.sentence, + user: User.all.sample + ) + end + end + puts "have created fake replies" + puts "now you have #{Reply.count} replies' data" end + end From 93aea92200e2e2f229e763798af7aad54c2434e5 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 11:33:44 +0800 Subject: [PATCH 26/48] update replies index page --- app/views/replies/index.html.erb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index c68077345..a99a7fde7 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -10,6 +10,7 @@

    Like<%= @tweet.user.likes_count %>

    +

    tweets

    @@ -24,6 +25,7 @@
    +

    replies

    <% @replies.each do |t| %>
    From 76d76a8a9deaace2b98712847426f75c5b3c81b9 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 12:04:53 +0800 Subject: [PATCH 27/48] fixed reply model --- app/controllers/replies_controller.rb | 2 +- app/models/reply.rb | 2 +- db/migrate/20180724032324_add_tweets_count_on_user.rb | 5 ----- db/migrate/20180725035428_add_tweets_count_to_users.rb | 9 +++++++++ db/schema.rb | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) delete mode 100644 db/migrate/20180724032324_add_tweets_count_on_user.rb create mode 100644 db/migrate/20180725035428_add_tweets_count_to_users.rb diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index 777a41a87..05f308e9c 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -10,7 +10,7 @@ def create tweet = Tweet.find(params[:tweet_id]) reply = tweet.replies.create(reply_params) reply.user = current_user - reply.save + reply.save! redirect_to tweet_replies_path(tweet) end diff --git a/app/models/reply.rb b/app/models/reply.rb index 1698fc959..fb5638d15 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,4 +1,4 @@ class Reply < ApplicationRecord - belongs_to :user, counter_cache: true belongs_to :tweet, counter_cache: true + belongs_to :user end diff --git a/db/migrate/20180724032324_add_tweets_count_on_user.rb b/db/migrate/20180724032324_add_tweets_count_on_user.rb deleted file mode 100644 index 6da80effa..000000000 --- a/db/migrate/20180724032324_add_tweets_count_on_user.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddTweetsCountOnUser < ActiveRecord::Migration[5.1] - def change - add_column :users, :tweets_count, :integer, default: 0 - end -end diff --git a/db/migrate/20180725035428_add_tweets_count_to_users.rb b/db/migrate/20180725035428_add_tweets_count_to_users.rb new file mode 100644 index 000000000..08dac61a2 --- /dev/null +++ b/db/migrate/20180725035428_add_tweets_count_to_users.rb @@ -0,0 +1,9 @@ +class AddTweetsCountToUsers < ActiveRecord::Migration[5.1] + def change + add_column :users, :tweets_count, :integer, default: 0 + #新增tweets的counter cache + User.pluck(:id).each do |i| + User.reset_counters(i, :tweets) + end + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index dee4dc5e6..5858020da 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: 20180724032324) do +ActiveRecord::Schema.define(version: 20180725035428) do create_table "followships", force: :cascade do |t| t.integer "user_id" From 640f0f207285fa211a78be7c7365571c87691f04 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 16:57:13 +0800 Subject: [PATCH 28/48] styled tweet index page --- app/assets/stylesheets/style.scss | 33 +++++++++++++++++++++++++------ app/views/tweets/index.html.erb | 4 ++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index b3aa9c6fb..1e6fc4a68 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -1,10 +1,32 @@ -.item-info{ - font-size: 9x; - font-weight: bold; - color: #3b70c9; +.tweet-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 4px; + margin-bottom: 20px; } +.tweet-item img { + max-height: 180px; + width: 100%; +} + + +.pop-user img { + max-height: 180px; + width: 120%; +} + +.pop-user { + border: 1px solid #ddd; + border-radius: 4px; + padding: 4px; + margin-bottom: 15px; +} + + + + .like { padding: 0 5px; color: #f00; @@ -13,5 +35,4 @@ .unlike { padding: 0 5px; color: #0f1; -} - +} \ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 22356ed7b..323b9e5b8 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,4 +1,4 @@ -
    +
    <%= form_for @tweet do |f| %> @@ -41,7 +41,7 @@
    - <%= image_tag u.avatar %> + <%= image_tag(u.avatar, class: "img-rounded") %>
    <%= link_to "@#{u.name}", tweets_user_path(u), class: "name" %> From c1d80fd1b3ca095dfeb5325e8d889322f728e223 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 17:32:57 +0800 Subject: [PATCH 29/48] add create & destroy action for followship record --- app/controllers/followships_controller.rb | 16 +++++++++++++++- app/controllers/users_controller.rb | 6 +++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 05f01b552..75f821ad2 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,7 +1,21 @@ class FollowshipsController < ApplicationController + def create + @followship = current_user.followships.build(following_id: params[:following_id]) + if @followship.save + flash[:notice] = "Successfully followed" + redirect_back(fallback_location: root_path) + else + flash[:alert] = @followship.errors.full_messages.to_sentence + redirect_back(fallback_location: root_path) + end end def destroy + @followship = current_user.followships.where(following_id: params[:id]).first + @followship.destroy + flash[:alert] = "Followship destroyed" + redirect_back(fallback_location: root_path) end -end + +end \ No newline at end of file diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index c7a506901..cff4bdbb7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -19,8 +19,9 @@ def update end end - def followings - @followings # 基於測試規格,必須講定變數名稱 + def followings # 基於測試規格,必須講定變數名稱 + @followings = @user.followships.order(created_at: :desc) + end end def followers @@ -41,4 +42,3 @@ def set_user def user_params params.require(:user).permit(:name, :introduction, :avatar) end -end From aff601e9de32531f341a95b62cde8c63a1596c65 Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Jul 2018 17:57:11 +0800 Subject: [PATCH 30/48] styled user edit page and reply index --- app/assets/stylesheets/style.scss | 10 +++++++++- app/views/replies/index.html.erb | 2 +- app/views/users/edit.html.erb | 5 +++-- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 1e6fc4a68..bfa36bc20 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -11,8 +11,16 @@ width: 100%; } +.reply-msg { + border: 1px solid #ddd; + border-radius: 4px; + padding: 4px; + margin-bottom: 15px; + width: 120%; + +} -.pop-user img { +.reply-msg img { max-height: 180px; width: 120%; } diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index a99a7fde7..f0f0961d3 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -31,7 +31,7 @@
    - <%= image_tag t.user.avatar %> + <%= image_tag(t.user.avatar, class: "img-rounded") %>
    <%= link_to "@#{t.user.name},#{t.created_at.to_s(:db)}", tweets_user_path(t.user) %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index aa337de47..94d7d2aa2 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -2,8 +2,9 @@ <%= form_for @user do |f| %>
    - <%= f.label :avatar, class: "none"%> - + <%= f.label :avatar, "大頭照上傳" %> + <% image_tag @user.avatar if @user.avatar? %> + <%= f.file_field :avatar %>
    From 0f159bffc84b41079cc3bf5cdc4c241dcc4b03dc Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 26 Jul 2018 10:36:10 +0800 Subject: [PATCH 31/48] styled tweet and reply index page --- app/assets/stylesheets/style.scss | 31 +++++++++++++++++-------------- app/views/replies/index.html.erb | 6 +++--- app/views/tweets/index.html.erb | 4 ++-- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index bfa36bc20..42fb20dd1 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -1,14 +1,19 @@ - -.tweet-item { +#tweets img { border: 1px solid #ddd; border-radius: 4px; padding: 4px; - margin-bottom: 20px; + margin-bottom: 15px; + } -.tweet-item img { - max-height: 180px; - width: 100%; + + +#replies img { + width: 110%; + padding: 6px; + max-height: 200px; + + } .reply-msg { @@ -16,14 +21,10 @@ border-radius: 4px; padding: 4px; margin-bottom: 15px; - width: 120%; - } -.reply-msg img { - max-height: 180px; - width: 120%; -} + + .pop-user { border: 1px solid #ddd; @@ -32,8 +33,10 @@ margin-bottom: 15px; } - - +.pop-user img { + max-height: 180px; + width: 120%; +} .like { padding: 0 5px; diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index f0f0961d3..86c4b618d 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1,6 +1,6 @@
    -
    - <%= image_tag @tweet.user.avatar %> +
    + <%= image_tag @tweet.user.avatar, class: "img-rounded" %>

    <%= @tweet.user.name %>

    <%= @tweet.user.introduction %>

    @@ -11,7 +11,7 @@
    -
    +

    tweets

    diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 323b9e5b8..8b282ca27 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,4 +1,4 @@ -
    +
    <%= form_for @tweet do |f| %> @@ -60,5 +60,5 @@
    <% end %>
    +
    -
    From d721007ed1ab7753c22d2b9b4cf195dd0234dcec Mon Sep 17 00:00:00 2001 From: Henry Date: Thu, 26 Jul 2018 11:47:42 +0800 Subject: [PATCH 32/48] update tweet/user index page for admin --- app/controllers/admin/base_controller.rb | 13 ++++- app/controllers/admin/tweets_controller.rb | 10 ++-- app/controllers/admin/users_controller.rb | 2 +- app/views/admin/tweets/index.html.erb | 55 +++++++++++++++------- app/views/admin/users/index.html.erb | 17 +++---- 5 files changed, 63 insertions(+), 34 deletions(-) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 61c9a1463..251ee3ebd 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,2 +1,13 @@ class Admin::BaseController < ApplicationController -end + before_action :authenticate_admin + + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "NOT ALLOW!" + redirect_to root_path + end + end + +end \ No newline at end of file diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index e1739df10..114253aa8 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -3,15 +3,13 @@ class Admin::TweetsController < Admin::BaseController before_action :authenticate_admin def index - @tweets = Tweet.all + @tweets = Tweet.all.order(created_at: :desc) end - def destroy + def destroy @tweet = Tweet.find(params[:id]) @tweet.destroy flash[:alert] = "Tweet was deleted!" - redirect_to admin_root_path(t) + redirect_to admin_root_path end - - -end +end \ No newline at end of file diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e2f4e5d0a..4a2bf1ea9 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,7 +1,7 @@ class Admin::UsersController < Admin::BaseController def index - @users = User.all + @users = User.order(tweets_count: :desc) end end diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index d9da84187..cf694abde 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1,18 +1,18 @@ -
    -

    後台

    -
    - -
    +
    +

    後台

    +
    + - - +
    +
    + @@ -24,10 +24,29 @@ <% @tweets.each do |t| %> - - - - + + + + + + + + <% end %>
    user content
    <%= t.user.name %><%= t.description %>
    <%= t.user.name %><%= t.description %><%= t.created_at.strftime("%Y-%m-%d") %> + + + + <%= link_to "Delete", admin_tweet_path(t), method: :delete,data: { confirm: "Are you sure?"}, class: "btn" %> +
    diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 21aa2f3e7..ad2d6f655 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -1,8 +1,6 @@ -
    +

    後台

    -
    -