From 253abbbb8377c9b87cd3f3a4233111926ae44173 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Tue, 17 Jul 2018 22:08:55 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=E5=89=8D=E5=BE=8C?= =?UTF-8?q?=E5=8F=B0=E9=A6=96=E9=A0=81=EF=BC=8C=E5=BB=BA=E7=AB=8B=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/stylesheets/admin/tweets.scss | 2 +- app/controllers/tweets_controller.rb | 2 +- app/controllers/users_controller.rb | 25 +++++++++----- app/models/user.rb | 3 ++ app/views/admin/tweets/index.html.erb | 1 + app/views/layouts/application.html.erb | 2 +- app/views/tweets/index.html.erb | 1 + app/views/users/edit.html.erb | 34 +++++++++++++++++++ config/routes.rb | 19 +++++++++++ db/seeds.rb | 3 ++ .../admin/tweets_controller_spec.rb | 5 +++ spec/controllers/users_controller_spec.rb | 5 +++ spec/helpers/admin/tweets_helper_spec.rb | 15 ++++++++ spec/helpers/users_helper_spec.rb | 15 ++++++++ 14 files changed, 120 insertions(+), 12 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/users/edit.html.erb create mode 100644 spec/controllers/admin/tweets_controller_spec.rb create mode 100644 spec/controllers/users_controller_spec.rb create mode 100644 spec/helpers/admin/tweets_helper_spec.rb create mode 100644 spec/helpers/users_helper_spec.rb diff --git a/app/assets/stylesheets/admin/tweets.scss b/app/assets/stylesheets/admin/tweets.scss index 9ec2a3732..b27805dc4 100644 --- a/app/assets/stylesheets/admin/tweets.scss +++ b/app/assets/stylesheets/admin/tweets.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the admin/tweets controller here. +// Place all the styles related to the admin::tweets controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..58670b0c6 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,7 +1,7 @@ class TweetsController < ApplicationController def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + # @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end def create diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 750e3c6b5..a315023bc 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,24 +1,31 @@ class UsersController < ApplicationController + before_action :set_user, only: [:show, :edit, :update] + def index + @users = User.all + end - def tweets + def show end def edit + unless @user == current_user + redirect_to user_path(@user) + end end def update + @user.update(user_params) + redirect_to user_path(@user) end - def followings - @followings # 基於測試規格,必須講定變數名稱 - end + private - def followers - @followers # 基於測試規格,必須講定變數名稱 + def set_user + @user = User.find(params[:id]) end - def likes - @likes # 基於測試規格,必須講定變數名稱 + def user_params + params.require(:user).permit(:name, :intro, :avatar) end - end + diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..b6600e478 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,5 +10,8 @@ class User < ApplicationRecord # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + def admin? + self.role == "admin" + end end diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb new file mode 100644 index 000000000..1199558b7 --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1 @@ +

後台

\ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 952cb7a1b..4017a3f05 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@ <% if current_user %> <% if current_user&.admin? %> -
  • <%= link_to 'Admin Panel', admin_restaurants_path %>
  • +
  • <%= link_to 'Admin Panel', admin_tweets_path %>
  • <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb new file mode 100644 index 000000000..9182193fc --- /dev/null +++ b/app/views/tweets/index.html.erb @@ -0,0 +1 @@ +

    首頁

    \ No newline at end of file diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb new file mode 100644 index 000000000..7dd38ed9f --- /dev/null +++ b/app/views/users/edit.html.erb @@ -0,0 +1,34 @@ +
    +
    + <%= form_for @user do |f| %> +
    + <%= f.label :avatar, "大頭照" %> + <%= image_tag @user.avatar if @user.avatar? %> + <%= f.file_field :avatar %> +
    + <% end %> +
    +
    +
    + +

    Edit Profile

    + + <%= form_for @user do |f| %> +
    + <%= f.label :name, "Name" %> + <%= f.text_field :name, class: "form-control" %> +
    +
    + <%= f.label :intro, "自我介紹" %> + <%= f.text_area :intro, class: "form-control" %> +
    +
    + <%= f.submit "Update", 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 90856d4fe..8d60bd464 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,25 @@ Rails.application.routes.draw do + root "tweets#index" # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users + + resources :tweets do + # 瀏覽所有推特最新動態 + collection do + get :feeds + end + member do + get :dashboard + # 瀏覽個別餐廳 Dashboard,帶入餐廳id + post :like + post :unlike + end + end + resources :users, only: [:index ,:show, :edit, :update] + namespace :admin do + resources :tweets + root "tweets#index" + end # 請依照專案指定規格來設定路由 diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2acc..4eda3b3a5 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: "root@example.com", password: "12345678", role: "admin", name: "root") +puts "Default admin created!" \ No newline at end of file diff --git a/spec/controllers/admin/tweets_controller_spec.rb b/spec/controllers/admin/tweets_controller_spec.rb new file mode 100644 index 000000000..56d5404b7 --- /dev/null +++ b/spec/controllers/admin/tweets_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Admin::TweetsController, type: :controller do + +end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb new file mode 100644 index 000000000..e2c3d3b5d --- /dev/null +++ b/spec/controllers/users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UsersController, type: :controller do + +end diff --git a/spec/helpers/admin/tweets_helper_spec.rb b/spec/helpers/admin/tweets_helper_spec.rb new file mode 100644 index 000000000..0c792ac78 --- /dev/null +++ b/spec/helpers/admin/tweets_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Admin::TweetsHelper. For example: +# +# describe Admin::TweetsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Admin::TweetsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/users_helper_spec.rb b/spec/helpers/users_helper_spec.rb new file mode 100644 index 000000000..b2e34440e --- /dev/null +++ b/spec/helpers/users_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the UsersHelper. For example: +# +# describe UsersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe UsersHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From 64735f4e09e56f936cc3f1647418abea0ba2aba7 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Sun, 22 Jul 2018 11:57:51 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E9=99=A4=E4=BA=86=E8=A8=BB=E5=86=8A?= =?UTF-8?q?=E5=92=8C=E7=99=BB=E5=85=A5=E9=A0=81=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=80=85=E4=B8=80=E5=AE=9A=E8=A6=81=E7=99=BB=E5=85=A5=E6=89=8D?= =?UTF-8?q?=E8=83=BD=E4=BD=BF=E7=94=A8=E7=B6=B2=E7=AB=99=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=80=85=E8=83=BD=E5=89=B5=E5=BB=BA=E5=B8=B3=E8=99=9F?= =?UTF-8?q?=E3=80=81=E7=99=BB=E5=85=A5=E3=80=81=E7=99=BB=E5=87=BA=20?= =?UTF-8?q?=E9=99=A4=E4=BA=86=E4=BF=A1=E7=AE=B1=E5=92=8C=E5=AF=86=E7=A2=BC?= =?UTF-8?q?=EF=BC=8C=E4=BD=BF=E7=94=A8=E8=80=85=E5=9C=A8=E8=A8=BB=E5=86=8A?= =?UTF-8?q?=E6=99=82=E9=82=84=E8=83=BD=E8=A8=AD=E5=AE=9A=E8=87=AA=E5=B7=B1?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E7=A8=B1=20=E4=BD=BF=E7=94=A8=E8=80=85?= =?UTF-8?q?=E7=9A=84=E5=90=8D=E7=A8=B1=E4=B8=8D=E8=83=BD=E9=87=8D=E8=A6=86?= =?UTF-8?q?=EF=BC=8C=E8=8B=A5=E6=9C=89=E9=87=8D=E8=A6=86=E6=9C=83=E8=B7=B3?= =?UTF-8?q?=E5=87=BA=E9=8C=AF=E8=AA=A4=20=E4=BD=BF=E7=94=A8=E8=80=85?= =?UTF-8?q?=E8=83=BD=E7=B7=A8=E8=BC=AF=E8=87=AA=E5=B7=B1=E7=9A=84=E5=90=8D?= =?UTF-8?q?=E7=A8=B1=E3=80=81=E4=BB=8B=E7=B4=B9=E5=92=8C=E5=A4=A7=E9=A0=AD?= =?UTF-8?q?=E7=85=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 +- Gemfile.lock | 15 ++++++- app/assets/stylesheets/admin/users.scss | 2 +- app/controllers/admin/tweets_controller.rb | 41 ++++++++++++++++++- app/controllers/admin/users_controller.rb | 8 ++-- app/controllers/application_controller.rb | 9 ++++ app/controllers/users_controller.rb | 3 +- app/models/tweet.rb | 1 + app/models/user.rb | 2 + app/views/admin/tweets/_form.html.erb | 18 ++++++++ app/views/admin/tweets/edit.html.erb | 1 + app/views/admin/tweets/index.html.erb | 3 +- app/views/admin/tweets/new.html.erb | 1 + app/views/admin/users/index.html.erb | 30 ++++++++++++++ app/views/devise/registrations/new.html.erb | 4 ++ app/views/layouts/application.html.erb | 1 + app/views/users/edit.html.erb | 7 +++- app/views/users/index.html.erb | 13 ++++++ app/views/users/show.html.erb | 27 ++++++++++++ config/routes.rb | 3 +- .../admin/users_controller_spec.rb | 5 +++ spec/helpers/admin/users_helper_spec.rb | 15 +++++++ 22 files changed, 199 insertions(+), 12 deletions(-) create mode 100644 app/views/admin/tweets/_form.html.erb create mode 100644 app/views/admin/tweets/edit.html.erb create mode 100644 app/views/admin/tweets/new.html.erb create mode 100644 app/views/admin/users/index.html.erb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/show.html.erb create mode 100644 spec/controllers/admin/users_controller_spec.rb create mode 100644 spec/helpers/admin/users_helper_spec.rb diff --git a/Gemfile b/Gemfile index c1f1ee3e2..b5995d72e 100644 --- a/Gemfile +++ b/Gemfile @@ -11,7 +11,7 @@ gem 'devise' gem 'carrierwave' # resize image size # gem 'mini_magick' - +gem 'kaminari' gem 'ffaker' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' diff --git a/Gemfile.lock b/Gemfile.lock index e55e9522f..3e98f70e4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -97,6 +97,18 @@ GEM jbuilder (2.7.0) activesupport (>= 4.2.0) multi_json (>= 1.2) + 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) @@ -259,6 +271,7 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + kaminari listen (>= 3.0.5, < 3.2) puma (~> 3.7) rails (~> 5.1.4) @@ -276,4 +289,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.16.1 + 1.16.2 diff --git a/app/assets/stylesheets/admin/users.scss b/app/assets/stylesheets/admin/users.scss index 925e31223..2cabf7bd7 100644 --- a/app/assets/stylesheets/admin/users.scss +++ b/app/assets/stylesheets/admin/users.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the admin/users controller here. +// Place all the styles related to the admin::users controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..058fb66a8 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,7 +1,46 @@ class Admin::TweetsController < Admin::BaseController + before_action :set_tweet, only: [:show, :edit, :update, :destroy] def index + @tweets = Tweet.page(params[:page]).per(10) + end + def new + @tweet = Tweet.new + end + def create + @tweet = Tweet.new(tweet_params) + if @restaurant.save + flash[:notice] = "tweet is successfully create" + redirect_to admin_tweets_path + else + flash.now[:alert] = "tweet is failed to create" + render :new + end + end + def show + + end + def edit + + end + def update + if @tweet.update(tweet_params) + flash[:notice] = "tweet is successfully updated" + redirect_to admin_tweets_path(@tweet) + else + flash.now[:alert] = "tweet was failed to update" + end end - def destroy + @tweet.destroy + redirect_to admin_tweets_path + flash[:alert] = "tweet was deleted" + end + + private + def tweet_params + params.require(:tweet).permit(:name, :opening_hours, :tel ,:address, :description, :image, :category_id, :remote_image_url) + end + def set_tweet + @tweet = tweet.find(params[:id]) end end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 3ba9f0a36..e3ca4b8f4 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,4 +1,6 @@ -class Admin::UsersController < Admin::BaseController - def index - end +class Admin::UsersController < ApplicationController + def index + @users = User.all + end + end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..1632f33e5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,16 @@ 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/controllers/users_controller.rb b/app/controllers/users_controller.rb index a315023bc..9ba08fe32 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -5,6 +5,7 @@ def index end def show + @user = User.find(params[:id]) end def edit @@ -25,7 +26,7 @@ def set_user end def user_params - params.require(:user).permit(:name, :intro, :avatar) + 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 b6600e478..e1d0e70b3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,11 +5,13 @@ class User < ApplicationRecord :recoverable, :rememberable, :trackable, :validatable mount_uploader :avatar, AvatarUploader + has_many :tweets # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + validates_uniqueness_of :name def admin? self.role == "admin" end diff --git a/app/views/admin/tweets/_form.html.erb b/app/views/admin/tweets/_form.html.erb new file mode 100644 index 000000000..420e7b49a --- /dev/null +++ b/app/views/admin/tweets/_form.html.erb @@ -0,0 +1,18 @@ +<% if @tweet.errors.any? %> +

    We have some errors here

    + +<% end %> + + + + +<%= form_for [:admin, @tweet] do |f| %> + <%= f.label :description, "Description" %> + <%= f.text_area :description %> + + <%=f.submit%> +<% end %> \ No newline at end of file diff --git a/app/views/admin/tweets/edit.html.erb b/app/views/admin/tweets/edit.html.erb new file mode 100644 index 000000000..48f0730ef --- /dev/null +++ b/app/views/admin/tweets/edit.html.erb @@ -0,0 +1 @@ +<%= render partial:"form" %> \ No newline at end of file diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb index 1199558b7..10c2c818a 100644 --- a/app/views/admin/tweets/index.html.erb +++ b/app/views/admin/tweets/index.html.erb @@ -1 +1,2 @@ -

    後台

    \ No newline at end of file +

    後台

    +<%= link_to 'New Tweet', new_admin_tweet_path, class: "btn btn-primary" %> diff --git a/app/views/admin/tweets/new.html.erb b/app/views/admin/tweets/new.html.erb new file mode 100644 index 000000000..48f0730ef --- /dev/null +++ b/app/views/admin/tweets/new.html.erb @@ -0,0 +1 @@ +<%= render partial:"form" %> \ No newline at end of file diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb new file mode 100644 index 000000000..c037f1206 --- /dev/null +++ b/app/views/admin/users/index.html.erb @@ -0,0 +1,30 @@ +
    + +

    使用者

    + + +
    + +
    + + + + + + + + + + <% @users.each do |user| %> + + + + + + <% end %> + +
    #NamePassword
    <%= user.id %><%= user.name %><%= user.password%>
    +
    + +
    +
    \ No newline at end of file diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 602803cff..69bee41ff 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -7,6 +7,10 @@ <%= f.label :email %>
    <%= f.email_field :email, autofocus: true, autocomplete: "email" %> +
    + <%= f.label :name %>
    + <%= f.text_field :name, autocomplete: "off" %> +
    <%= f.label :password %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4017a3f05..160d9aa31 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,6 +15,7 @@ <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • +
  • <%= link_to 'Profile', user_path(current_user) %>
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %> diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 7dd38ed9f..4b6abeb6b 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -6,6 +6,9 @@ <%= image_tag @user.avatar if @user.avatar? %> <%= f.file_field :avatar %>
    +
    + <%= f.submit "更新照片", class: "btn btn-primary" %> +
    <% end %>
    @@ -19,8 +22,8 @@ <%= f.text_field :name, class: "form-control" %>
    - <%= f.label :intro, "自我介紹" %> - <%= f.text_area :intro, class: "form-control" %> + <%= f.label :introduction, "自我介紹" %> + <%= f.text_area :introduction, class: "form-control" %>
    <%= f.submit "Update", class: "btn btn-primary" %> diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 000000000..9283fc45f --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,13 @@ +<% @users.each do |user| %> +
    +
    + +
    +

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

    + <% if user != current_user %> + <%= render partial: "shared/following", locals: {user: user} %> + <%end%> +
    +
    +
    +<% end %> \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 000000000..32bd54a0a --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,27 @@ +
    +
    +
    + + + +
    + +
    + +
    diff --git a/config/routes.rb b/config/routes.rb index 8d60bd464..13088c0af 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,8 @@ resources :users, only: [:index ,:show, :edit, :update] namespace :admin do resources :tweets - root "tweets#index" + resources :users + root "users#index" end # 請依照專案指定規格來設定路由 diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb new file mode 100644 index 000000000..520a18f6f --- /dev/null +++ b/spec/controllers/admin/users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Admin::UsersController, type: :controller do + +end diff --git a/spec/helpers/admin/users_helper_spec.rb b/spec/helpers/admin/users_helper_spec.rb new file mode 100644 index 000000000..f26854ec3 --- /dev/null +++ b/spec/helpers/admin/users_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Admin::UsersHelper. For example: +# +# describe Admin::UsersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Admin::UsersHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From c6275aef5a170f2d1e59acfdbb71bfcc17974259 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Sun, 22 Jul 2018 21:10:21 +0800 Subject: [PATCH 03/17] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/routes.rb | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 13088c0af..c44cb04f8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,23 +3,23 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html devise_for :users - resources :tweets do - # 瀏覽所有推特最新動態 - collection do - get :feeds - end + resources :tweets, only: [:index, :create] do + resources :replies, only:[:index, :create] member do - get :dashboard - # 瀏覽個別餐廳 Dashboard,帶入餐廳id post :like post :unlike end end - resources :users, only: [:index ,:show, :edit, :update] + resources :users, only: :edit do + member do + get :followings + get :followers + end + end + resources :followship, only: [:create, :destroy] namespace :admin do - resources :tweets - resources :users - root "users#index" + resources :tweets, only: [:index ,:destroy] + resources :users, only: :index end # 請依照專案指定規格來設定路由 From f035c8dc628244dec91961209dc3082bb3be2c62 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Sun, 29 Jul 2018 14:03:42 +0800 Subject: [PATCH 04/17] =?UTF-8?q?=E9=BB=9E=E6=93=8A=E5=85=B6=E4=BB=96?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=80=85=E7=9A=84=E5=90=8D=E7=A8=B1=E6=99=82?= =?UTF-8?q?=EF=BC=8C=E8=83=BD=E7=80=8F=E8=A6=BD=E8=A9=B2=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=80=85=E7=9A=84=E5=80=8B=E4=BA=BA=E8=B3=87=E6=96=99=E5=8F=8A?= =?UTF-8?q?=E6=8E=A8=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/tweets_controller.rb | 25 +++++------ app/controllers/application_controller.rb | 1 + app/controllers/tweets_controller.rb | 24 +++++++++-- app/controllers/users_controller.rb | 7 +++- app/models/tweet.rb | 2 + app/models/user.rb | 1 + app/views/admin/tweets/_form.html.erb | 7 ++-- app/views/layouts/application.html.erb | 1 - app/views/tweets/index.html.erb | 34 ++++++++++++++- app/views/users/tweets.html.erb | 41 +++++++++++++++++++ config/routes.rb | 1 + db/migrate/20180727165332_change_user_role.rb | 6 +++ db/migrate/20180727174429_change_user_role.rb | 6 +++ db/schema.rb | 4 +- db/seeds.rb | 3 +- lib/tasks/dev.rake | 15 ++++++- 16 files changed, 149 insertions(+), 29 deletions(-) create mode 100644 app/views/users/tweets.html.erb create mode 100644 db/migrate/20180727165332_change_user_role.rb create mode 100644 db/migrate/20180727174429_change_user_role.rb diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 058fb66a8..0d67a911a 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -3,18 +3,11 @@ class Admin::TweetsController < Admin::BaseController def index @tweets = Tweet.page(params[:page]).per(10) end - def new - @tweet = Tweet.new - end def create - @tweet = Tweet.new(tweet_params) - if @restaurant.save - flash[:notice] = "tweet is successfully create" - redirect_to admin_tweets_path - else - flash.now[:alert] = "tweet is failed to create" - render :new - end + @user= current_user + @tweet= @user.tweets.build(tweet_params) + @tweet.save! + redirect_to tweet_path(@tweet) end def show @@ -31,16 +24,18 @@ def update end end def destroy - @tweet.destroy - redirect_to admin_tweets_path + if current_user.admin? + @tweet.destroy + redirect_to tweets_path + end flash[:alert] = "tweet was deleted" end private def tweet_params - params.require(:tweet).permit(:name, :opening_hours, :tel ,:address, :description, :image, :category_id, :remote_image_url) + params.require(:tweet).permit( :description) end def set_tweet - @tweet = tweet.find(params[:id]) + @tweet = Tweet.find(params[:id]) end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1632f33e5..a76beb782 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,5 @@ class ApplicationController < ActionController::Base + before_action :authenticate_user! protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index 58670b0c6..e1f3cf67f 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,16 +1,34 @@ class TweetsController < ApplicationController - + before_action :set_tweet, only: [:show, :edit, :update, :destroy] def index + @users = User.all + @tweet = Tweet.new + @tweets = Tweet.all + @rank_tweets = Tweet.order(followers_count: :desc).limit(10) + # 新增一組空白資料給表單,首頁為index,@tweet傳入表單的實例變數 # @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end - + def create + @user = current_user + @tweet= @user.tweets.build(tweet_params) + @tweet.save! + if @tweet.save + flash[:notice] = "tweet is successfully create" + redirect_to tweets_path + else + flash.now[:alert] = "tweet is failed to create" + render :new + end end - def like end def unlike end + private + def tweet_params + params.require(:tweet).permit(:description) + end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9ba08fe32..ef4f7ff9c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,11 +1,14 @@ class UsersController < ApplicationController - before_action :set_user, only: [:show, :edit, :update] + before_action :set_user, only: [:show, :edit, :update, :tweets] def index @users = User.all end + def tweets + @user = User.find(params[:id]) + end def show - @user = User.find(params[:id]) + end def edit diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 66a52974e..003288b07 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -2,4 +2,6 @@ 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 e1d0e70b3..714403e9f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,6 +12,7 @@ class User < ApplicationRecord validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) validates_uniqueness_of :name + def admin? self.role == "admin" end diff --git a/app/views/admin/tweets/_form.html.erb b/app/views/admin/tweets/_form.html.erb index 420e7b49a..229829996 100644 --- a/app/views/admin/tweets/_form.html.erb +++ b/app/views/admin/tweets/_form.html.erb @@ -11,8 +11,9 @@ <%= form_for [:admin, @tweet] do |f| %> - <%= f.label :description, "Description" %> - <%= f.text_area :description %> - +
    + <%= f.label :description, "Description" %> + <%= f.text_area :description %> +
    <%=f.submit%> <% end %> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 160d9aa31..4017a3f05 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,7 +15,6 @@ <% end %>
  • <%= link_to('登出', destroy_user_session_path, method: :delete) %>
  • -
  • <%= link_to 'Profile', user_path(current_user) %>
  • <%= link_to('修改個人資料', edit_user_path(current_user)) %>
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • <% else %> diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 9182193fc..5b278f360 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1 +1,33 @@ -

    首頁

    \ No newline at end of file +

    首頁

    + + +
    +
    + <%= form_for @tweet do |f| %> +
    + <%= f.text_area :description, placeholder: "留個言吧", class: "form-control" %> +
    +
    + <%= f.submit class: "btn btn-primary" %> + <%= f.button "Cancel", type: :reset, class: "btn btn-default" %> +
    + <% end %> + + <% @tweets.each do |tweet| %> +
    +

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

    +

    <%= simple_format tweet.description %>

    +

    + <%= time_ago_in_words(tweet.created_at) %> +

    +
    + <% if current_user.admin? %> + <%= link_to "Delete", admin_tweet_path(tweet), method: :delete %> + <%end%> + +
    + <% end %> + + +
    +
    \ No newline at end of file diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb new file mode 100644 index 000000000..d29d04e30 --- /dev/null +++ b/app/views/users/tweets.html.erb @@ -0,0 +1,41 @@ +
    +
    +
    + + + +
    +
    +
    \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c44cb04f8..5768f2520 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ member do get :followings get :followers + get :tweets end end resources :followship, only: [:create, :destroy] diff --git a/db/migrate/20180727165332_change_user_role.rb b/db/migrate/20180727165332_change_user_role.rb new file mode 100644 index 000000000..7974c026b --- /dev/null +++ b/db/migrate/20180727165332_change_user_role.rb @@ -0,0 +1,6 @@ +class ChangeUserRole < ActiveRecord::Migration[5.1] + def change + remove_column :users, :role, :string, default: 'normal' + add_column :users,:role, :string + end +end diff --git a/db/migrate/20180727174429_change_user_role.rb b/db/migrate/20180727174429_change_user_role.rb new file mode 100644 index 000000000..f0b72cf0e --- /dev/null +++ b/db/migrate/20180727174429_change_user_role.rb @@ -0,0 +1,6 @@ +class ChangeUserRole < ActiveRecord::Migration[5.1] + def change + remove_column :users, :role, :string + add_column :users, :role, :string, default: 'normal' + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a6842c86..08f896256 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: 20180727174429) do create_table "followships", force: :cascade do |t| t.integer "user_id" @@ -64,8 +64,8 @@ t.string "avatar" t.text "introduction" t.integer "likes_count", default: 0 - t.string "role", default: "normal" t.integer "followers_count", default: 0 + t.string "role", default: "normal" 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 diff --git a/db/seeds.rb b/db/seeds.rb index 4eda3b3a5..2dfdb1239 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,6 +5,7 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) - +Tweet.destroy_all +User.destroy_all User.create(email: "root@example.com", password: "12345678", role: "admin", name: "root") puts "Default admin created!" \ No newline at end of file diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 9b1e87ae4..d9bdae8ee 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,7 +2,7 @@ namespace :dev do # 請先執行 rails dev:fake_user,可以產生 20 個資料完整的 User 紀錄 # 其他測試用的假資料請依需要自行撰寫 task fake_user: :environment do - User.destroy_all + 20.times do |i| name = FFaker::Name::first_name file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") @@ -19,5 +19,18 @@ namespace :dev do puts user.name end end + task fake_tweet: :environment do + Tweet.destroy_all + User.all.each do |user| + 3.times do |i| + user.tweets.create!( + description: FFaker::Lorem.sentence, + user: User.all.sample + ) + end + end + puts "create tweets" + puts "#{Tweet.count} tweet data" + end end From c393fa1c7d2f1edbb9e3a07102e4c8454bb6cced Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Sun, 29 Jul 2018 16:09:23 +0800 Subject: [PATCH 05/17] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=80=85=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=BF=BD=E8=B9=A4/=E5=8F=96=E6=B6=88=E8=BF=BD?= =?UTF-8?q?=E8=B9=A4=E5=85=B6=E4=BB=96=E4=BD=BF=E7=94=A8=E8=80=85=EF=BC=88?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E8=BF=BD=E8=B9=A4=E8=87=AA=E5=B7=B1=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/followships_controller.rb | 20 +++++++++++++++++++- app/models/followship.rb | 3 ++- app/models/user.rb | 10 +++++++++- app/views/tweets/index.html.erb | 9 ++++++++- app/views/users/tweets.html.erb | 7 +++++++ config/routes.rb | 2 +- 6 files changed, 46 insertions(+), 5 deletions(-) diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 05f01b552..bdcd03212 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,7 +1,25 @@ class FollowshipsController < ApplicationController def create - end + # 需要設定前端的 link_to,在發出請求時送進 following_id + @followship = current_user.followships.build(following_id: params[:following_id]) + if @followship.save + flash[:notice] = "Successfully followed" + redirect_back(fallback_location: root_path) + else + # 驗證失敗時,Model 將錯誤訊息放在 errors 裡回傳 + # 使用 errors.full_messages 取出完成訊息集合(Array),串接 to_sentence 將 Array 組合成 String + 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) + #複數寫法 + # @followship = current_user.followships.where(following_id: params[:id]) + # @followship.destroy_all end end diff --git a/app/models/followship.rb b/app/models/followship.rb index 1aed01396..102fb5e81 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 714403e9f..94e4816e4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -12,9 +12,17 @@ class User < ApplicationRecord validates_presence_of :name # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) validates_uniqueness_of :name + #建立追蹤關係 + has_many :followships, dependent: :destroy + has_many :followings, through: :followships + #被誰追蹤 + has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" #主鍵指向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 5b278f360..80ef135e7 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -5,7 +5,7 @@
    <%= form_for @tweet do |f| %>
    - <%= f.text_area :description, placeholder: "留個言吧", class: "form-control" %> + <%= f.text_area :description, placeholder: "在想些什麼呢?", class: "form-control" %>
    <%= f.submit class: "btn btn-primary" %> @@ -16,6 +16,13 @@ <% @tweets.each do |tweet| %>

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

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

    <%= simple_format tweet.description %>

    <%= time_ago_in_words(tweet.created_at) %> diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index d29d04e30..0e77202c5 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -17,7 +17,14 @@

    <%= simple_format @user.introduction %>
    <% if @user.email == current_user.email %> <%= link_to 'Edit Profile', edit_user_path(@user), class: "btn btn-primary" %> + <% else %> + <% if current_user.following?(@user) %> + <%= link_to 'Unfollow', followship_path(@user), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: @user), method: :post,class: "btn btn-info" %> + <% end %> <% end %> +
    diff --git a/config/routes.rb b/config/routes.rb index 5768f2520..9ce90d10b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,7 +17,7 @@ get :tweets end end - resources :followship, only: [:create, :destroy] + resources :followships, only: [:create, :destroy] namespace :admin do resources :tweets, only: [:index ,:destroy] resources :users, only: :index From 5b63db50dc1d7bb4c3850263759d6e2714e8f5e3 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Sun, 29 Jul 2018 22:00:17 +0800 Subject: [PATCH 06/17] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=80=85=E8=83=BD?= =?UTF-8?q?=E5=9C=A8=E9=A6=96=E9=A0=81=E7=9C=8B=E8=A6=8B=E8=B7=9F=E9=9A=A8?= =?UTF-8?q?=E8=80=85=20(followers)=20=E6=95=B8=E9=87=8F=E6=8E=92=E5=88=97?= =?UTF-8?q?=E5=89=8D=2010=20=E7=9A=84=E4=BD=BF=E7=94=A8=E8=80=85=E6=8E=A8?= =?UTF-8?q?=E8=96=A6=E5=90=8D=E5=96=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/tweets_controller.rb | 2 +- app/models/followship.rb | 4 +- app/views/tweets/index.html.erb | 58 ++++++++++++++++++---------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index e1f3cf67f..46be43dc1 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -4,7 +4,7 @@ def index @users = User.all @tweet = Tweet.new @tweets = Tweet.all - @rank_tweets = Tweet.order(followers_count: :desc).limit(10) + @rank_users = User.order(followers_count: :desc).limit(10) # 新增一組空白資料給表單,首頁為index,@tweet傳入表單的實例變數 # @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end diff --git a/app/models/followship.rb b/app/models/followship.rb index 102fb5e81..a54695fcb 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,5 +1,5 @@ class Followship < ApplicationRecord validates :following_id, uniqueness: { scope: :user_id } - belongs_to :user - belongs_to :following, class_name:"User" + belongs_to :user + belongs_to :following,counter_cache: :followers_count, class_name:"User" end diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 80ef135e7..0b1d297fe 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -13,28 +13,44 @@
    <% end %> - <% @tweets.each do |tweet| %> +
    -

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

    - <% if tweet.user != current_user %> - <% if current_user.following?(tweet.user) %> - <%= link_to 'Unfollow', followship_path(tweet.user), method: :delete,class: "btn btn-primary" %> - <% else %> - <%= link_to 'Follow', followships_path(following_id: tweet.user), method: :post,class: "btn btn-info" %> - <% end %> - <%end%> -

    <%= simple_format tweet.description %>

    -

    - <%= time_ago_in_words(tweet.created_at) %> -

    + <% @tweets.each do |tweet| %> +

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

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

    <%= simple_format tweet.description %>

    +

    + <%= time_ago_in_words(tweet.created_at) %> +

    + <% if current_user.admin? %> + <%= link_to "Delete", admin_tweet_path(tweet), method: :delete %> + <%end%> +
    + <% end %> +
    +
    +

    Top Ten

    +
    +
    +
    +

    風雲榜

    +
    +
    + <% @rank_users.each do |user| %> +

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

    +

    <%= user.introduction %>

    +

    追蹤數:<%= user.followers_count %>

    +
    + <% end %> +
    +
    +
    - <% if current_user.admin? %> - <%= link_to "Delete", admin_tweet_path(tweet), method: :delete %> - <%end%> - -
    - <% end %> - -
    \ No newline at end of file From 8d8f81ac0f6637681b1400f1b330e90b7c63d589 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Mon, 30 Jul 2018 21:07:34 +0800 Subject: [PATCH 07/17] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=80=85=E8=83=BD?= =?UTF-8?q?=E5=9B=9E=E8=A6=86=E5=88=A5=E4=BA=BA=E7=9A=84=E6=8E=A8=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/replies_controller.rb | 28 +++++++++++++++++++---- app/models/reply.rb | 2 ++ app/models/tweet.rb | 2 +- app/models/user.rb | 3 +++ app/views/replies/index.html.erb | 32 +++++++++++++++++++++++++++ app/views/tweets/index.html.erb | 7 ------ app/views/tweets/reply.html.erb | 0 app/views/users/tweets.html.erb | 7 ++++-- 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 app/views/replies/index.html.erb create mode 100644 app/views/tweets/reply.html.erb diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index a9b6a315b..3d973620e 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,9 +1,29 @@ class RepliesController < ApplicationController + def index + @replies = Reply.all + @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= current_user + @reply.save! + redirect_to tweet_replies_path(@tweet) + end - def index - end + def destroy + @tweet= Tweet.find(params[:tweet_id]) + @reply = Reply.find(params[:id]) - def create - end + if current_user.admin? + @reply.destroy + redirect_to tweets_user_path(user) + end + end + private + def reply_params + params.require(:reply).permit(:comment) + end end diff --git a/app/models/reply.rb b/app/models/reply.rb index bae6f9463..5c225a8ef 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 003288b07..273d860ab 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,7 +1,7 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 belongs_to :user - + has_many :replies end diff --git a/app/models/user.rb b/app/models/user.rb index 94e4816e4..f82529d0c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -18,6 +18,9 @@ class User < ApplicationRecord #被誰追蹤 has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" #主鍵指向following_id has_many :followers, through: :inverse_followships, source: :user + # 建立多對多的關係,回應 + has_many :replies, dependent: :restrict_with_error + # has_many :restaurants, through: :comments =>>已經擁有tweets def admin? self.role == "admin" diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb new file mode 100644 index 000000000..e6eacac90 --- /dev/null +++ b/app/views/replies/index.html.erb @@ -0,0 +1,32 @@ +

    回覆

    + +
    +

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

    +

    <%= simple_format @tweet.description %>

    +

    + <%= time_ago_in_words(@tweet.created_at) %> +

    + <% if current_user.admin? %> + <%= link_to "Delete", admin_tweet_path(@tweet), method: :delete %> + <%end%> +
    +
    +<%= form_for [@tweet, @reply] do |f| %> +
    + <%= f.text_area :comment, placeholder: "回應", class: "form-control" %> +
    +
    + <%= f.submit class:"btn btn-primary" %> + <%= f.button "Cancel", type: :reset, class:"btn-primary" %> +
    +<%end%> +<% @tweet.replies.each do |reply| %> +
    +

    <%= reply.user.name %>

    +

    <%= simple_format reply.comment %>

    +

    + <%= time_ago_in_words(reply.created_at) %> +

    +
    +
    +<%end%> \ No newline at end of file diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 0b1d297fe..5c0838319 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -17,13 +17,6 @@
    <% @tweets.each do |tweet| %>

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

    - <% if tweet.user != current_user %> - <% if current_user.following?(tweet.user) %> - <%= link_to 'Unfollow', followship_path(tweet.user), method: :delete,class: "btn btn-primary" %> - <% else %> - <%= link_to 'Follow', followships_path(following_id: tweet.user), method: :post,class: "btn btn-info" %> - <% end %> - <%end%>

    <%= simple_format tweet.description %>

    <%= time_ago_in_words(tweet.created_at) %> diff --git a/app/views/tweets/reply.html.erb b/app/views/tweets/reply.html.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 0e77202c5..fc14df869 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -4,7 +4,6 @@

    diff --git a/config/routes.rb b/config/routes.rb index 58acb30a1..3fc1727c0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,16 +10,19 @@ post :unlike end end - resources :users, only: :edit do + resources :users, only: [:edit, :update] do member do get :followings get :followers get :tweets + get :like end end resources :followships, only: [:create, :destroy] namespace :admin do - resources :tweets, only: [:index ,:destroy] + resources :tweets, only: [:index ,:destroy] do + resources :replies,only:[:index,:destroy] + end resources :users, only: :index root "tweets#index" end diff --git a/spec/controllers/admin/replies_controller_spec.rb b/spec/controllers/admin/replies_controller_spec.rb new file mode 100644 index 000000000..aeefcb6f9 --- /dev/null +++ b/spec/controllers/admin/replies_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Admin::RepliesController, type: :controller do + +end diff --git a/spec/helpers/admin/replies_helper_spec.rb b/spec/helpers/admin/replies_helper_spec.rb new file mode 100644 index 000000000..9821d25d7 --- /dev/null +++ b/spec/helpers/admin/replies_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the Admin::RepliesHelper. For example: +# +# describe Admin::RepliesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe Admin::RepliesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end From da298d722c6c3eb469097a6db7b9c81dce1eaa6e Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Tue, 31 Jul 2018 20:51:17 +0800 Subject: [PATCH 10/17] =?UTF-8?q?=E9=83=A8=E5=B1=AC=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 6 +++++- config/database.yml | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Gemfile b/Gemfile index b5995d72e..952181445 100644 --- a/Gemfile +++ b/Gemfile @@ -17,7 +17,7 @@ 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 @@ -60,6 +60,10 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' + gem 'sqlite3' +end +group :production do + gem 'pg', '~> 0.20' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem diff --git a/config/database.yml b/config/database.yml index 0d02f2498..1c8d40f2a 100644 --- a/config/database.yml +++ b/config/database.yml @@ -21,5 +21,5 @@ test: database: db/test.sqlite3 production: - <<: *default - database: db/production.sqlite3 + adapter: postgresql + encoding: Unicode From ca016327aca8827b0b4f9dfa29d80bb142936e94 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Tue, 31 Jul 2018 20:54:25 +0800 Subject: [PATCH 11/17] bundle install --- Gemfile.lock | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Gemfile.lock b/Gemfile.lock index 3e98f70e4..8d68c89af 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -136,6 +136,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) @@ -273,6 +276,7 @@ DEPENDENCIES jbuilder (~> 2.5) kaminari listen (>= 3.0.5, < 3.2) + pg (~> 0.20) puma (~> 3.7) rails (~> 5.1.4) rails-controller-testing From 8a8b952280716745a3fc08f82fa8af682002aec9 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Wed, 1 Aug 2018 23:28:18 +0800 Subject: [PATCH 12/17] =?UTF-8?q?=E5=88=AA=E9=99=A4=E9=87=8D=E8=A4=87migra?= =?UTF-8?q?tion?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/base_controller.rb | 4 +++- app/controllers/admin/replies_controller.rb | 1 + app/controllers/admin/tweets_controller.rb | 3 ++- app/controllers/admin/users_controller.rb | 2 ++ app/models/tweet.rb | 2 +- app/views/admin/users/index.html.erb | 10 ++++++++-- db/migrate/20180727174429_change_user_role.rb | 6 ------ 7 files changed, 17 insertions(+), 11 deletions(-) delete mode 100644 db/migrate/20180727174429_change_user_role.rb diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 4a89583f5..70865a28e 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,3 +1,5 @@ class Admin::BaseController < ApplicationController - + if current_user.admin? == false + redirect_to root_path + end end diff --git a/app/controllers/admin/replies_controller.rb b/app/controllers/admin/replies_controller.rb index dc7b140e5..cf599c878 100644 --- a/app/controllers/admin/replies_controller.rb +++ b/app/controllers/admin/replies_controller.rb @@ -2,6 +2,7 @@ class Admin::RepliesController < ApplicationController def index @replies = Reply.order(created_at: :desc) @tweet = Tweet.find(params[:tweet_id]) + end diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index d047d2a84..dc77f3927 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,5 +1,6 @@ class Admin::TweetsController < Admin::BaseController def index + @tweets = Tweet.order(created_at: :desc) end @@ -7,7 +8,7 @@ def destroy @tweet = Tweet.find(params[:id]) if current_user.admin? @tweet.destroy - redirect_to tweets_path + redirect_to admin_root_path end flash[:alert] = "tweet was deleted" end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index e3ca4b8f4..330fbfd95 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,8 @@ class Admin::UsersController < ApplicationController def index @users = User.all + + end end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6d25fac69..241d5cd5a 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,6 +1,6 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 - belongs_to :user + belongs_to :user, counter_cache: true has_many :replies # 喜歡關係 has_many :likes, dependent: :destroy diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index c037f1206..5f4037564 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -11,7 +11,10 @@ # Name - Password + 推文數 + 關注人數 + 追蹤者人數 + 喜歡的推特 @@ -19,7 +22,10 @@ <%= user.id %> <%= user.name %> - <%= user.password%> + <%= user.tweets.count%> + <%= user.followings.count%> + <%= user.followers.count%> + <%= user.likes.count%> <% end %> diff --git a/db/migrate/20180727174429_change_user_role.rb b/db/migrate/20180727174429_change_user_role.rb deleted file mode 100644 index f0b72cf0e..000000000 --- a/db/migrate/20180727174429_change_user_role.rb +++ /dev/null @@ -1,6 +0,0 @@ -class ChangeUserRole < ActiveRecord::Migration[5.1] - def change - remove_column :users, :role, :string - add_column :users, :role, :string, default: 'normal' - end -end From 683e5e67c24dc8acb2b38e9b120cf4bad509efcf Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Thu, 2 Aug 2018 00:13:50 +0800 Subject: [PATCH 13/17] =?UTF-8?q?=E4=BD=BF=E6=99=AE=E9=80=9A=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=80=85=E9=80=B2=E4=B8=8D=E5=8E=BB=E5=BE=8C=E5=8F=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/admin/base_controller.rb | 5 ++--- app/controllers/admin/replies_controller.rb | 3 +++ app/controllers/admin/tweets_controller.rb | 4 +++- app/controllers/admin/users_controller.rb | 4 +++- app/controllers/application_controller.rb | 1 + 5 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 70865a28e..69bb7fe52 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,5 +1,4 @@ class Admin::BaseController < ApplicationController - if current_user.admin? == false - redirect_to root_path - end + + end diff --git a/app/controllers/admin/replies_controller.rb b/app/controllers/admin/replies_controller.rb index cf599c878..1aad9009c 100644 --- a/app/controllers/admin/replies_controller.rb +++ b/app/controllers/admin/replies_controller.rb @@ -2,6 +2,9 @@ class Admin::RepliesController < ApplicationController def index @replies = Reply.order(created_at: :desc) @tweet = Tweet.find(params[:tweet_id]) + if current_user.admin? == false + redirect_to root_path + end end diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index dc77f3927..b2eeb7e8c 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,6 +1,8 @@ class Admin::TweetsController < Admin::BaseController def index - + if current_user.admin? == false + redirect_to root_path + end @tweets = Tweet.order(created_at: :desc) end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 330fbfd95..388b9311e 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,7 +1,9 @@ class Admin::UsersController < ApplicationController def index @users = User.all - + if current_user.admin? == false + redirect_to root_path + end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index a76beb782..5b0dc34f5 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base before_action :authenticate_user! protect_from_forgery with: :exception before_action :configure_permitted_parameters, if: :devise_controller? + # 請參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 From 9655bac4ea4ed46608712624884b88854dae8672 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Thu, 2 Aug 2018 00:22:27 +0800 Subject: [PATCH 14/17] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=80=85=E6=AC=84=E4=BD=8D=EF=BC=8Ctweets=5Fcount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/migrate/20180801162039_add_tweets_count_to_user.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180801162039_add_tweets_count_to_user.rb diff --git a/db/migrate/20180801162039_add_tweets_count_to_user.rb b/db/migrate/20180801162039_add_tweets_count_to_user.rb new file mode 100644 index 000000000..74f21a0c4 --- /dev/null +++ b/db/migrate/20180801162039_add_tweets_count_to_user.rb @@ -0,0 +1,5 @@ +class AddTweetsCountToUser < 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 08f896256..3dd49220b 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: 20180727174429) do +ActiveRecord::Schema.define(version: 20180801162039) do create_table "followships", force: :cascade do |t| t.integer "user_id" @@ -66,6 +66,7 @@ t.integer "likes_count", default: 0 t.integer "followers_count", default: 0 t.string "role", default: "normal" + 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 8bcf7b4d70e87caeaf42ee4f1af4bb339bde0746 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Wed, 8 Aug 2018 00:13:49 +0800 Subject: [PATCH 15/17] =?UTF-8?q?=E8=A7=A3=E6=B1=BA=E8=87=AA=E5=8B=95?= =?UTF-8?q?=E6=B8=AC=E8=A9=A6=E9=8C=AF=E8=AA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 2 + Gemfile.lock | 13 +++- app/assets/javascripts/application.js | 2 + .../{application.css => application.scss} | 6 +- app/assets/stylesheets/style.scss | 77 +++++++++++++++++++ app/controllers/admin/users_controller.rb | 2 +- app/controllers/followships_controller.rb | 21 ++--- app/controllers/tweets_controller.rb | 9 ++- app/controllers/users_controller.rb | 26 ++++++- app/models/followship.rb | 2 +- app/models/like.rb | 2 +- app/models/tweet.rb | 2 +- app/models/user.rb | 9 ++- app/views/admin/users/index.html.erb | 2 + app/views/tweets/index.html.erb | 2 +- app/views/users/followers.html.erb | 16 ++++ app/views/users/followings.html.erb | 16 ++++ app/views/users/likes.html.erb | 15 ++++ app/views/users/tweets.html.erb | 61 +++------------ config/routes.rb | 2 +- lib/tasks/dev.rake | 6 +- 21 files changed, 212 insertions(+), 81 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (90%) create mode 100644 app/assets/stylesheets/style.scss create mode 100644 app/views/users/followers.html.erb create mode 100644 app/views/users/followings.html.erb create mode 100644 app/views/users/likes.html.erb diff --git a/Gemfile b/Gemfile index 952181445..0b31580d8 100644 --- a/Gemfile +++ b/Gemfile @@ -13,6 +13,8 @@ gem 'carrierwave' # gem 'mini_magick' gem 'kaminari' gem 'ffaker' +gem 'jquery-rails' +gem 'bootstrap-sass', '~> 3.3.7' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '~> 5.1.4' diff --git a/Gemfile.lock b/Gemfile.lock index 8d68c89af..31fef2236 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.2) + 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) kaminari (1.1.1) activesupport (>= 4.1.0) kaminari-actionview (= 1.1.1) @@ -266,6 +275,7 @@ PLATFORMS x86-mswin32 DEPENDENCIES + bootstrap-sass (~> 3.3.7) byebug capybara (~> 2.13) carrierwave @@ -274,6 +284,7 @@ DEPENDENCIES factory_bot_rails ffaker jbuilder (~> 2.5) + jquery-rails kaminari listen (>= 3.0.5, < 3.2) pg (~> 0.20) @@ -293,4 +304,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 1.16.2 + 1.16.3 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..38d5802a2 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,3 +13,5 @@ //= require rails-ujs //= require turbolinks //= require_tree . +//= require jquery +//= require bootstrap-sprockets diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 90% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index d05ea0f51..1f6d01a71 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,8 @@ * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * - *= require_tree . - *= require_self */ +@import "bootstrap-sprockets"; +@import "bootstrap"; +@import "style"; + diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss new file mode 100644 index 000000000..6284a0bfa --- /dev/null +++ b/app/assets/stylesheets/style.scss @@ -0,0 +1,77 @@ +.nav-pills-center { + text-align: center; +} + +.nav-pills-center > li { + float: none; + display: inline-block; +} + +.restaurant-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 4px; + margin-bottom: 20px; +} + +.restaurant-item img { + max-height: 180px; + width: 100%; +} + +.restaurant-item .caption { + padding: 10px; +} + +.edit_user img { + width: 50px; + margin: 10px; +} + +.my-restaurant { + display: inline-block; + text-align: center; + padding: 7px 7px 0 7px; +} + +.my-restaurant > div { + height: 60px; +} + +.my-restaurant img { + height: 59px; + width: 100px; + +} + +.panel a:hover .my-restaurant { + background-color: #eee; +} +.panel-body{ + text-align: center; +} + +.avatar{ + height: 300px; + float: left; +} +.avatar img{ + height: 200px; + width: 150px; + display: block; +} + +.nav-tabs{ + margin-bottom: 20px; +} +.restaurant_feed{ + width: 48%; + float: left; + margin-right: 2%; +} +.panel-default { + padding: 5px; +} +.user-item { + margin-bottom: 20px; +} \ No newline at end of file diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 388b9311e..dd7eb4881 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,6 +1,6 @@ class Admin::UsersController < ApplicationController def index - @users = User.all + @users = User.order(tweets_count: :desc) if current_user.admin? == false redirect_to root_path end diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index bdcd03212..f90c1d100 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,16 +1,19 @@ class FollowshipsController < ApplicationController def create - # 需要設定前端的 link_to,在發出請求時送進 following_id - @followship = current_user.followships.build(following_id: params[:following_id]) - - if @followship.save - flash[:notice] = "Successfully followed" + @user = User.find(params[:following_id]) + if @user == current_user + flash[:notice] = "Can't follow self!" redirect_back(fallback_location: root_path) else - # 驗證失敗時,Model 將錯誤訊息放在 errors 裡回傳 - # 使用 errors.full_messages 取出完成訊息集合(Array),串接 to_sentence 將 Array 組合成 String - flash[:alert] = @followship.errors.full_messages.to_sentence - redirect_back(fallback_location: root_path) + @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 end def destroy diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index b3ea2d5b4..7ffaefc33 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,9 +1,8 @@ class TweetsController < ApplicationController def index - @users = User.all + @users = User.order(followers_count: :desc).includes(:followers).limit(10) @tweet = Tweet.new @tweets = Tweet.order(created_at: :desc) - @rank_users = User.order(followers_count: :desc).limit(10) # 新增一組空白資料給表單,首頁為index,@tweet傳入表單的實例變數 # @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 end @@ -23,14 +22,16 @@ def create def like @tweet = Tweet.find(params[:id]) @tweet.likes.create!(user: current_user) - redirect_back(fallback_location: root_path) + redirect_to tweets_path + end def unlike @tweet = Tweet.find(params[:id]) likes = Like.where(tweet: @tweet, user: current_user) likes.destroy_all - redirect_back(fallback_location: root_path) + redirect_to tweets_path + end private def tweet_params diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d3c71572e..d8cabf27d 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,15 +1,37 @@ class UsersController < ApplicationController - before_action :set_user, only: [ :edit, :update, :tweets] + before_action :set_user, only: [ :edit, :update, :tweets,:followings,:followers,:likes] def tweets @followings = @user.followings.order(created_at: :desc) @followers = @user.followers.order(created_at: :desc) @likes = @user.likes.order(created_at: :desc) + @tweets = @user.tweets + @order_user_tweets = @user.tweets.order(created_at: :desc) + end + def followings + @followings = @user.followings.order('followships.created_at DESC') + @followers = @user.followers + @likes = @user.likes.order(created_at: :desc) + @tweets = @user.tweets + end + def followers + @followers = @user.followers.order('followships.created_at DESC') + @followings = @user.followings + @likes = @user.likes.order(created_at: :desc) + @tweets = @user.tweets + end + + def likes + @followings = @user.followings + @followers = @user.followers + @likes = @user.liked_tweets.order('likes.created_at DESC') + @tweets = @user.tweets end def edit unless @user == current_user - redirect_to tweets_user_path(current_user), alert: "Can't edit other's profile" + redirect_to tweets_user_path(@user), alert: "Can't edit other's profile" end end + def update if @user == current_user diff --git a/app/models/followship.rb b/app/models/followship.rb index a54695fcb..ccee25467 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,5 +1,5 @@ class Followship < ApplicationRecord validates :following_id, uniqueness: { scope: :user_id } - belongs_to :user + belongs_to :user belongs_to :following,counter_cache: :followers_count, class_name:"User" end diff --git a/app/models/like.rb b/app/models/like.rb index 386e4d978..1fcb9b445 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,4 +1,4 @@ class Like < ApplicationRecord belongs_to :tweet,counter_cache: true - belongs_to :user, counter_cache: true + belongs_to :user end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 241d5cd5a..382f2b3f4 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,7 +1,7 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 belongs_to :user, counter_cache: true - has_many :replies + has_many :replies,dependent: :destroy # 喜歡關係 has_many :likes, dependent: :destroy has_many :liked_users, through: :likes, source: :user diff --git a/app/models/user.rb b/app/models/user.rb index 5fd1c9287..e6799c18c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,7 +5,7 @@ class User < ApplicationRecord :recoverable, :rememberable, :trackable, :validatable mount_uploader :avatar, AvatarUploader - has_many :tweets + has_many :tweets,dependent: :destroy # 需要 app/views/devise 裡找到樣板,加上 name 屬性 # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 @@ -16,14 +16,14 @@ class User < ApplicationRecord has_many :followships, dependent: :destroy has_many :followings, through: :followships #被誰追蹤 - has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" #主鍵指向following_id + has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id",dependent: :destroy #主鍵指向following_id has_many :followers, through: :inverse_followships, source: :user # 建立多對多的關係,回應 has_many :replies, dependent: :restrict_with_error - # has_many :restaurants, through: :comments =>>已經擁有tweets + # 建立喜歡 has_many :likes, dependent: :destroy - has_many :liked_tweets, through: :likes , source: :likes + has_many :liked_tweets, through: :likes, source: :tweet def admin? self.role == "admin" @@ -31,4 +31,5 @@ def admin? def following?(user) self.followings.include?(user) end + end diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 5f4037564..0e0ed3cfc 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -15,6 +15,7 @@ 關注人數 追蹤者人數 喜歡的推特 + # @@ -26,6 +27,7 @@ <%= user.followings.count%> <%= user.followers.count%> <%= user.likes.count%> + <%= user.followships.count%> <% end %> diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 5edf2efc7..8d7e867fb 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -34,7 +34,7 @@

    風雲榜

    - <% @rank_users.each do |user| %> + <% @users.each do |user| %>

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

    <%= user.introduction %>

    追蹤數:<%= user.followers_count %>

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

    <%= @followers.count %> followers

    +
    + +
      + <% @followers.each do |follower| %> +
    • + <%= link_to user_path(follower) do %> + <%= image_tag(follower.avatar, height: 30, width: 30, class: "img-circle") %> + <%= follower.name %> + <% end %> +
    • + <% end %> +
    +
    \ No newline at end of file diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb new file mode 100644 index 000000000..5ef104868 --- /dev/null +++ b/app/views/users/followings.html.erb @@ -0,0 +1,16 @@ +
    +
    +

    <%= @followings.count %> followings

    +
    + +
      + <% @followings.each do |following| %> +
    • + <%= link_to user_path(following) do %> + <%= image_tag(following.avatar, height: 30, width: 30, class: "img-circle") %> + <%= following.name %> + <% end %> +
    • + <% end %> +
    +
    \ No newline at end of file diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb new file mode 100644 index 000000000..a808af7bb --- /dev/null +++ b/app/views/users/likes.html.erb @@ -0,0 +1,15 @@ +
    +
    +

    <%= @likes.count %> likes

    +
    + +
      + <% @likes.each do |like| %> +
    • + <%= link_to tweets_path(like) do %> + <%= like.description %> + <% end %> +
    • + <% end %> +
    +
    \ No newline at end of file diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 5656fb98e..634628938 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -14,21 +14,24 @@

    <%= @user.name %>

    <%= @user.email %>

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

    Tweet from <%= @user.name %>

    - <% @user.tweets.each do |tweet| %> + <% @order_user_tweets.each do |tweet| %>

    <%= link_to tweet.user.name, tweets_user_path(tweet.user) %> @@ -47,54 +50,10 @@ <%end%>
    -
    -
    -

    <%= @followings.count %> followings

    -
    - -
      - <% @followings.each do |following| %> -
    • - <%= link_to user_path(following) do %> - <%= image_tag(following.avatar, height: 30, width: 30, class: "img-circle") %> - <%= following.name %> - <% end %> -
    • - <% end %> -
    -
    + -
    -
    -

    <%= @followers.count %> followers

    -
    - -
      - <% @followers.each do |follower| %> -
    • - <%= link_to user_path(follower) do %> - <%= image_tag(follower.avatar, height: 30, width: 30, class: "img-circle") %> - <%= follower.name %> - <% end %> -
    • - <% end %> -
    -
    -
    -
    -

    <%= @likes.count %> likes

    -
    - -
      - <% @likes.each do |like| %> -
    • - <%= link_to tweets_path(like) do %> - <%= like.tweet.description %> - <% end %> -
    • - <% end %> -
    -
    + +

    diff --git a/config/routes.rb b/config/routes.rb index 3fc1727c0..614c2f2ab 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,7 +15,7 @@ get :followings get :followers get :tweets - get :like + get :likes end end resources :followships, only: [:create, :destroy] diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index d9bdae8ee..2643aed7d 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -2,7 +2,9 @@ namespace :dev do # 請先執行 rails dev:fake_user,可以產生 20 個資料完整的 User 紀錄 # 其他測試用的假資料請依需要自行撰寫 task fake_user: :environment do - + User.destroy_all + User.create(email: "root@example.com", password: "12345678", role: "admin", name: "root") + puts "Default admin created!" 20.times do |i| name = FFaker::Name::first_name file = File.open("#{Rails.root}/public/avatar/user#{i+1}.jpg") @@ -20,7 +22,6 @@ namespace :dev do end end task fake_tweet: :environment do - Tweet.destroy_all User.all.each do |user| 3.times do |i| user.tweets.create!( @@ -33,4 +34,5 @@ namespace :dev do puts "#{Tweet.count} tweet data" end + end From 875e1cb65673a3ed814ad6be3a8b9b0ffb1d92ce Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Tue, 4 Sep 2018 22:56:01 +0800 Subject: [PATCH 16/17] =?UTF-8?q?=E9=80=B2=E8=A1=8C=E4=BB=8B=E9=9D=A2?= =?UTF-8?q?=E5=84=AA=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/assets/config/manifest.js | 3 - app/assets/stylesheets/style.scss | 79 +++++++++++----------- app/views/layouts/application.html.erb | 34 +++++----- app/views/replies/index.html.erb | 72 +++++++++++++-------- app/views/shared/_like.html.erb | 2 +- app/views/tweets/index.html.erb | 81 +++++++++++++---------- app/views/users/edit.html.erb | 38 +++++------ app/views/users/followers.html.erb | 46 +++++++++---- app/views/users/followings.html.erb | 46 +++++++++---- app/views/users/likes.html.erb | 54 ++++++++++++---- app/views/users/tweets.html.erb | 90 ++++++++++---------------- 11 files changed, 311 insertions(+), 234 deletions(-) delete mode 100644 app/assets/config/manifest.js diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js deleted file mode 100644 index b16e53d6d..000000000 --- a/app/assets/config/manifest.js +++ /dev/null @@ -1,3 +0,0 @@ -//= link_tree ../images -//= link_directory ../javascripts .js -//= link_directory ../stylesheets .css diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss index 6284a0bfa..fbc45bf61 100644 --- a/app/assets/stylesheets/style.scss +++ b/app/assets/stylesheets/style.scss @@ -7,46 +7,7 @@ display: inline-block; } -.restaurant-item { - border: 1px solid #ddd; - border-radius: 4px; - padding: 4px; - margin-bottom: 20px; -} - -.restaurant-item img { - max-height: 180px; - width: 100%; -} - -.restaurant-item .caption { - padding: 10px; -} - -.edit_user img { - width: 50px; - margin: 10px; -} - -.my-restaurant { - display: inline-block; - text-align: center; - padding: 7px 7px 0 7px; -} - -.my-restaurant > div { - height: 60px; -} - -.my-restaurant img { - height: 59px; - width: 100px; - -} -.panel a:hover .my-restaurant { - background-color: #eee; -} .panel-body{ text-align: center; } @@ -54,6 +15,12 @@ .avatar{ height: 300px; float: left; + width: 28%; + margin-right: 20px; +} +.edit_form{ + float: left; + width: 68%; } .avatar img{ height: 200px; @@ -74,4 +41,36 @@ } .user-item { margin-bottom: 20px; -} \ No newline at end of file +} +.tweet { + border: 1px solid; + padding:10px; + margin-top: 30px; + margin-bottom: 30px; +} +.information { + width: 68%; + float: left; + margin-right: 20px; +} +.popular{ + width: 28%; + float: left; +} +.tweet-avatar{ + float: left; + margin: 0.5%; + margin-right: 15px; + margin-bottom: 15px; +} +.form_group{ + margin-top: 5px; +} +.about{ + width: 28%; + float: left; +} +.content{ + width: 68%; + float: left; +} diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4017a3f05..7f24ff232 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,20 +9,24 @@ - <% 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_path(current_user)) %>
  • -
  • <%= link_to('修改密碼', edit_user_registration_path) %>
  • - <% else %> -
  • <%= link_to('註冊', new_user_registration_path) %>
  • -
  • <%= link_to('登入', new_user_session_path) %>
  • - <% end %> -

    <%= notice %>

    -

    <%= alert %>

    - <%= yield %> + diff --git a/app/views/replies/index.html.erb b/app/views/replies/index.html.erb index 2d7b50cd6..cb34dad28 100644 --- a/app/views/replies/index.html.erb +++ b/app/views/replies/index.html.erb @@ -1,29 +1,45 @@ -

    回覆

    - -
    -

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

    -

    <%= simple_format @tweet.description %>

    -

    - <%= time_ago_in_words(@tweet.created_at) %> -

    -
    -
    -<%= form_for [@tweet, @reply] do |f| %> -
    - <%= f.text_area :comment, placeholder: "回應", class: "form-control" %> +
    +
    +
    + +

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

    +

    <%= @tweet.user.introduction %>

    +

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

    +

    Follower <%= @tweet.user.followships.count %>

    +

    Tweet <%= @tweet.user.tweets_count %>

    +

    Like <%= @tweet.user.likes.count %>

    +
    +
    +
    + +
    +
    +

    <%= link_to "@#{@tweet.user.name}, #{@tweet.created_at.strftime("%B %e, %Y at %I:%M %p")}", tweets_user_path(@tweet.user) %>

    +

    <%= simple_format @tweet.description %>

    +

    + <%= time_ago_in_words(@tweet.created_at) %> +

    + <%= render partial: "shared/like", locals: {tweet: @tweet} %> +
    + <%= form_for [@tweet, @reply] do |f| %> +
    + <%= f.text_area :comment, placeholder: "回應", class: "form-control" %> +
    +
    + <%= f.submit class:"btn btn-primary" %> + <%= f.button "Cancel", type: :reset, class:"btn btn-info" %> +
    + <%end%> + <% @tweet.replies.each do |reply| %> +
    +

    <%= reply.user.name %>

    +

    <%= simple_format reply.comment %>

    +

    + <%= time_ago_in_words(reply.created_at) %> +

    +
    +
    + <%end%> +
    -
    - <%= f.submit class:"btn btn-primary" %> - <%= f.button "Cancel", type: :reset, class:"btn-primary" %> -
    -<%end%> -<% @tweet.replies.each do |reply| %> -
    -

    <%= reply.user.name %>

    -

    <%= simple_format reply.comment %>

    -

    - <%= time_ago_in_words(reply.created_at) %> -

    -
    -
    -<%end%> \ No newline at end of file +
    \ No newline at end of file diff --git a/app/views/shared/_like.html.erb b/app/views/shared/_like.html.erb index 79e9cc960..46ddf6236 100644 --- a/app/views/shared/_like.html.erb +++ b/app/views/shared/_like.html.erb @@ -1,5 +1,5 @@ <% if tweet.is_liked?(current_user) %> - <%= link_to 'Unlike', unlike_tweet_path(tweet), method: :post, class: "btn btn-info" %> + <%= link_to "Unlike, #{tweet.likes_count} Likes", unlike_tweet_path(tweet), method: :post, class: "btn btn-info" %> <% else %> <%= link_to "Like(#{tweet.likes_count})", like_tweet_path(tweet), method: :post, class: "btn btn-primary" %> <% end %> diff --git a/app/views/tweets/index.html.erb b/app/views/tweets/index.html.erb index 8d7e867fb..a4ba0089e 100644 --- a/app/views/tweets/index.html.erb +++ b/app/views/tweets/index.html.erb @@ -1,46 +1,57 @@ -

    首頁

    +
    - <%= form_for @tweet do |f| %> -
    - <%= f.text_area :description, placeholder: "在想些什麼呢?", class: "form-control" %> -
    -
    - <%= f.submit class: "btn btn-primary" %> - <%= f.button "Cancel", type: :reset, class: "btn btn-default" %> -
    - <% end %> - - -
    +
    + <%= form_for @tweet do |f| %> +
    + <%= f.text_area :description, placeholder: "在想些什麼呢?", class: "form-control" %> +
    +
    + <%= f.submit class: "btn btn-primary" %> + <%= f.button "Cancel", type: :reset, class: "btn btn-default" %> +
    + <% end %> + <% @tweets.each do |tweet| %> -

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

    -

    <%= simple_format tweet.description %>

    -

    - <%= time_ago_in_words(tweet.created_at) %> -

    - <%= render partial: "shared/like", locals: {tweet: tweet} %> - <%= link_to "Reply(#{tweet.replies_count})" , tweet_replies_path(tweet)%> -
    +
    +
    + +
    +
    +

    <%= link_to "@#{tweet.user.name}, #{tweet.created_at.strftime("%B %e, %Y at %I:%M %p")}", tweets_user_path(tweet.user) %>

    +

    <%= simple_format tweet.description %>

    +

    + <%= time_ago_in_words(tweet.created_at) %> +

    +
    + <%= render partial: "shared/like", locals: {tweet: tweet} %> + <%= link_to "Reply(#{tweet.replies_count})" , tweet_replies_path(tweet),class:"btn btn-info"%> +
    <% end %>
    -
    -

    Top Ten

    -
    -
    -
    -

    風雲榜

    + diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 4b6abeb6b..5d03ea5f2 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb @@ -11,27 +11,21 @@
    <% end %>
    -
    -
    - -

    Edit Profile

    - - <%= form_for @user do |f| %> -
    - <%= f.label :name, "Name" %> - <%= f.text_field :name, class: "form-control" %> -
    -
    - <%= f.label :introduction, "自我介紹" %> - <%= f.text_area :introduction, class: "form-control" %> -
    -
    - <%= f.submit "Update", class: "btn btn-primary" %> - <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> -
    - <% end %> - -
    - +
    +

    Edit Profile

    + <%= form_for @user do |f| %> +
    + <%= f.label :name, "Name" %> + <%= f.text_field :name, class: "form-control" %> +
    +
    + <%= f.label :introduction, "自我介紹" %> + <%= f.text_area :introduction, class: "form-control" %> +
    +
    + <%= f.submit "Update", class: "btn btn-primary" %> + <%= link_to "Cancel", user_path(@user), class: "btn btn-default" %> +
    + <% end %>
    \ No newline at end of file diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb index e9362e741..146eb57ed 100644 --- a/app/views/users/followers.html.erb +++ b/app/views/users/followers.html.erb @@ -1,16 +1,40 @@ -
    -
    -

    <%= @followers.count %> followers

    +
    +
    + +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +

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

    +

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

    +

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

    +

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

    + <% if @user == current_user %> + <%= link_to 'Edit Profile', edit_user_path(@user), class: "btn btn-primary" %> + <%else%> + <% if current_user.following?(@user) %> + <%= link_to 'Unfollow', followship_path(@user), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: @user), method: :post,class: "btn btn-info" %> + <% end %> + <%end%>
    -
      - <% @followers.each do |follower| %> -
    • - <%= link_to user_path(follower) do %> - <%= image_tag(follower.avatar, height: 30, width: 30, class: "img-circle") %> - <%= follower.name %> +
      +

      Followers

      +
      + <% @followers.each do |follower| %> + <%= link_to user_path(follower) do %> +

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

      +

      <%= simple_format follower.introduction %>

      + <% if follower != current_user %> + <% if current_user.following?(follower) %> + <%= link_to 'Unfollow', followship_path(follower), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: follower), method: :post,class: "btn btn-info" %> + <% end %> + <%end%> + <% end %> <% end %> -
    • - <% end %> +
      +
    \ No newline at end of file diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb index 5ef104868..57119de25 100644 --- a/app/views/users/followings.html.erb +++ b/app/views/users/followings.html.erb @@ -1,16 +1,40 @@ -
    -
    -

    <%= @followings.count %> followings

    +
    +
    + +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +

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

    +

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

    +

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

    +

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

    + <% if @user == current_user %> + <%= link_to 'Edit Profile', edit_user_path(@user), class: "btn btn-primary" %> + <%else%> + <% if current_user.following?(@user) %> + <%= link_to 'Unfollow', followship_path(@user), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: @user), method: :post,class: "btn btn-info" %> + <% end %> + <%end%>
    -
      - <% @followings.each do |following| %> -
    • - <%= link_to user_path(following) do %> - <%= image_tag(following.avatar, height: 30, width: 30, class: "img-circle") %> - <%= following.name %> +
      +

      Followings

      +
      + <% @followings.each do |following| %> + <%= link_to user_path(following) do %> +

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

      +

      <%= simple_format following.introduction %>

      + <% if follower != current_user %> + <% if current_user.following?(follower) %> + <%= link_to 'Unfollow', followship_path(follower), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: follower), method: :post,class: "btn btn-info" %> + <% end %> + <%end%> + <% end %> <% end %> -
    • - <% end %> +
      +
    \ No newline at end of file diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb index a808af7bb..71b02934e 100644 --- a/app/views/users/likes.html.erb +++ b/app/views/users/likes.html.erb @@ -1,15 +1,43 @@ -
    -
    -

    <%= @likes.count %> likes

    -
    +
    +
    +
    + +

    <%= @user.name %>

    +

    <%= @user.introduction %>

    +

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

    +

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

    +

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

    +

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

    + <% if @user == current_user %> + <%= link_to 'Edit Profile', edit_user_path(@user), class: "btn btn-primary" %> + <%else%> + <% if current_user.following?(@user) %> + <%= link_to 'Unfollow', followship_path(@user), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: @user), method: :post,class: "btn btn-info" %> + <% end %> + <%end%> +
    -
      - <% @likes.each do |like| %> -
    • - <%= link_to tweets_path(like) do %> - <%= like.description %> - <% end %> -
    • - <% end %> -
    +
    +

    Like

    + <% @tweets.each do |tweet| %> +
    +

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

    +

    <%= simple_format tweet.description %>

    +

    + <%= time_ago_in_words(tweet.created_at) %> +

    + <%= link_to "Reply" , tweet_replies_path(tweet)%> + <% if current_user.admin? %> + <%= link_to "Delete", admin_tweet_path(tweet), method: :delete %> + <%end%> +
    +
    + <%end%> +
    +
    \ No newline at end of file diff --git a/app/views/users/tweets.html.erb b/app/views/users/tweets.html.erb index 634628938..41a47cc1f 100644 --- a/app/views/users/tweets.html.erb +++ b/app/views/users/tweets.html.erb @@ -1,61 +1,41 @@
    -
    -
    - - -
    \ No newline at end of file From 5270bd7319ed164f28c04f653e3cdfa490190e78 Mon Sep 17 00:00:00 2001 From: "wenho.hu" Date: Tue, 4 Sep 2018 23:28:04 +0800 Subject: [PATCH 17/17] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E8=B7=AF=E5=BE=91?= =?UTF-8?q?=E8=88=87=E9=83=A8=E5=88=86=E4=BB=8B=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/views/users/followers.html.erb | 25 ++++++++++++++++--------- app/views/users/followings.html.erb | 27 +++++++++++++++++---------- app/views/users/likes.html.erb | 29 +++++++++++++++-------------- 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/app/views/users/followers.html.erb b/app/views/users/followers.html.erb index 146eb57ed..a96ed9a53 100644 --- a/app/views/users/followers.html.erb +++ b/app/views/users/followers.html.erb @@ -23,15 +23,22 @@
    <% @followers.each do |follower| %> <%= link_to user_path(follower) do %> -

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

    -

    <%= simple_format follower.introduction %>

    - <% if follower != current_user %> - <% if current_user.following?(follower) %> - <%= link_to 'Unfollow', followship_path(follower), method: :delete,class: "btn btn-primary" %> - <% else %> - <%= link_to 'Follow', followships_path(following_id: follower), method: :post,class: "btn btn-info" %> - <% end %> - <%end%> +
    +
    + +
    +
    +

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

    +

    <%= simple_format follower.introduction %>

    + <% if follower != current_user %> + <% if current_user.following?(follower) %> + <%= link_to 'Unfollow', followship_path(follower), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: follower), method: :post,class: "btn btn-info" %> + <% end %> + <%end%> +
    +
    <% end %> <% end %>
    diff --git a/app/views/users/followings.html.erb b/app/views/users/followings.html.erb index 57119de25..478141d73 100644 --- a/app/views/users/followings.html.erb +++ b/app/views/users/followings.html.erb @@ -23,18 +23,25 @@
    <% @followings.each do |following| %> <%= link_to user_path(following) do %> -

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

    -

    <%= simple_format following.introduction %>

    - <% if follower != current_user %> - <% if current_user.following?(follower) %> - <%= link_to 'Unfollow', followship_path(follower), method: :delete,class: "btn btn-primary" %> - <% else %> - <%= link_to 'Follow', followships_path(following_id: follower), method: :post,class: "btn btn-info" %> - <% end %> - <%end%> +
    +
    + +
    +
    +

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

    +

    <%= simple_format following.introduction %>

    + <% if following != current_user %> + <% if current_user.following?(following) %> + <%= link_to 'Unfollow', followship_path(following), method: :delete,class: "btn btn-primary" %> + <% else %> + <%= link_to 'Follow', followships_path(following_id: following), method: :post,class: "btn btn-info" %> + <% end %> + <%end%> +
    +
    <% end %> <% end %> -
    +
    \ No newline at end of file diff --git a/app/views/users/likes.html.erb b/app/views/users/likes.html.erb index 71b02934e..26f4f9de8 100644 --- a/app/views/users/likes.html.erb +++ b/app/views/users/likes.html.erb @@ -22,21 +22,22 @@

    Like

    <% @tweets.each do |tweet| %> -
    -

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

    -

    <%= simple_format tweet.description %>

    -

    - <%= time_ago_in_words(tweet.created_at) %> -

    - <%= link_to "Reply" , tweet_replies_path(tweet)%> - <% if current_user.admin? %> - <%= link_to "Delete", admin_tweet_path(tweet), method: :delete %> - <%end%> +
    +
    + +
    +
    +

    <%= link_to "@#{tweet.user.name}, #{tweet.created_at.strftime("%B %e, %Y at %I:%M %p")}", tweets_user_path(tweet.user) %>

    +

    <%= simple_format tweet.description %>

    +

    + <%= time_ago_in_words(tweet.created_at) %> +

    + <%= link_to "Reply" , tweet_replies_path(tweet),class: "btn btn-primary"%> + <% if current_user.admin? %> + <%= link_to "Delete", admin_tweet_path(tweet), method: :delete,class: "btn btn-info" %> + <%end%> +
    -
    <%end%>