diff --git a/Gemfile b/Gemfile index c1f1ee3e2..6f0abe0d3 100644 --- a/Gemfile +++ b/Gemfile @@ -5,19 +5,10 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end -# user authentication -gem 'devise' -# file uploader -gem 'carrierwave' -# resize image size -# gem 'mini_magick' - -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' +gem 'rails', '~> 5.1.6' + # Use Puma as the app server gem 'puma', '~> 3.7' # Use SCSS for stylesheets @@ -34,13 +25,28 @@ gem 'turbolinks', '~> 5' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.5' # Use Redis adapter to run Action Cable in production -# gem 'redis', '~> 3.0' +# gem 'redis', '~> 4.0' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development +#for User +gem 'devise' +#for image +gem 'carrierwave' +#for fake data +gem 'ffaker' +# Use uinames +gem 'rest-client' + +gem 'kaminari' +gem 'bootstrap-sass', '~> 3.3.7' +gem 'jquery-rails' + +gem 'fog-aws' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] @@ -51,6 +57,7 @@ group :development, :test do gem 'factory_bot_rails' gem 'shoulda-matchers', '~> 3.1' gem 'rails-controller-testing' + gem 'sqlite3' end group :development do @@ -62,5 +69,9 @@ group :development do gem 'spring-watcher-listen', '~> 2.0.0' end +group :production do + gem 'pg', '~> 0.20' +end + # Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/Gemfile.lock b/Gemfile.lock index 3b212e9b7..9546e43e6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -41,11 +41,13 @@ GEM addressable (2.5.2) public_suffix (>= 2.0.2, < 4.0) arel (8.0.0) + autoprefixer-rails (8.5.0) + execjs bcrypt (3.1.12) - bcrypt (3.1.12-java) - bcrypt (3.1.12-x64-mingw32) - bcrypt (3.1.12-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.2) capybara (2.18.0) @@ -69,7 +71,6 @@ GEM execjs coffee-script-source (1.12.2) concurrent-ruby (1.0.5) - concurrent-ruby (1.0.5-java) crass (1.0.4) devise (4.4.3) bcrypt (~> 3.0) @@ -78,7 +79,10 @@ GEM responders warden (~> 1.2.3) diff-lcs (1.3) + domain_name (0.5.20180417) + unf (>= 0.0.5, < 1.0.0) erubi (1.7.1) + excon (0.62.0) execjs (2.7.0) factory_bot (4.10.0) activesupport (>= 3.0.0) @@ -87,16 +91,49 @@ GEM railties (>= 3.0.0) ffaker (2.10.0) ffi (1.9.25) - ffi (1.9.25-java) - ffi (1.9.25-x64-mingw32) - ffi (1.9.25-x86-mingw32) + fog-aws (3.0.0) + fog-core (~> 2.1) + fog-json (~> 1.1) + fog-xml (~> 0.1) + ipaddress (~> 0.8) + fog-core (2.1.0) + builder + excon (~> 0.58) + formatador (~> 0.2) + mime-types + fog-json (1.2.0) + fog-core + multi_json (~> 1.10) + fog-xml (0.1.3) + fog-core + nokogiri (>= 1.5.11, < 2.0.0) + formatador (0.2.5) globalid (0.4.1) activesupport (>= 4.2.0) + http-cookie (1.0.3) + domain_name (~> 0.5) i18n (1.0.1) concurrent-ruby (~> 1.0) + ipaddress (0.8.3) 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) + 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) @@ -114,19 +151,14 @@ GEM mini_portile2 (2.3.0) minitest (5.11.3) multi_json (1.13.1) + netrc (0.11.0) nio4r (2.3.1) - nio4r (2.3.1-java) nokogiri (1.8.4) mini_portile2 (~> 2.3.0) - nokogiri (1.8.4-java) - nokogiri (1.8.4-x64-mingw32) - mini_portile2 (~> 2.3.0) - nokogiri (1.8.4-x86-mingw32) - mini_portile2 (~> 2.3.0) orm_adapter (0.5.0) + pg (0.21.0) public_suffix (3.0.2) puma (3.12.0) - puma (3.12.0-java) rack (2.0.5) rack-test (1.1.0) rack (>= 1.0, < 3) @@ -164,6 +196,10 @@ GEM responders (2.4.0) actionpack (>= 4.2.0, < 5.3) railties (>= 4.2.0, < 5.3) + rest-client (2.0.2) + http-cookie (>= 1.0.2, < 2.0) + mime-types (>= 1.16, < 4.0) + netrc (~> 0.8) rspec-core (3.7.1) rspec-support (~> 3.7.0) rspec-expectations (3.7.0) @@ -212,22 +248,19 @@ GEM activesupport (>= 4.0) sprockets (>= 3.0.0) sqlite3 (1.3.13) - sqlite3 (1.3.13-x64-mingw32) - sqlite3 (1.3.13-x86-mingw32) - sqlite3 (1.3.13-x86-mswin32-60) thor (0.20.0) thread_safe (0.3.6) - thread_safe (0.3.6-java) tilt (2.0.8) turbolinks (5.1.1) turbolinks-source (~> 5.1) turbolinks-source (5.1.0) tzinfo (1.2.5) thread_safe (~> 0.1) - tzinfo-data (1.2018.5) - tzinfo (>= 1.0.0) uglifier (4.1.17) execjs (>= 0.3.0, < 3) + unf (0.1.4) + unf_ext + unf_ext (0.0.7.5) warden (1.2.7) rack (>= 1.0) web-console (3.6.2) @@ -237,20 +270,15 @@ GEM railties (>= 5.0) websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-driver (0.6.5-java) - websocket-extensions (>= 0.1.0) websocket-extensions (0.1.3) xpath (3.1.0) nokogiri (~> 1.8) PLATFORMS - java ruby - x64-mingw32 - x86-mingw32 - x86-mswin32 DEPENDENCIES + bootstrap-sass (~> 3.3.7) byebug capybara (~> 2.13) carrierwave @@ -258,11 +286,16 @@ DEPENDENCIES devise factory_bot_rails ffaker + fog-aws jbuilder (~> 2.5) + jquery-rails + kaminari listen (>= 3.0.5, < 3.2) + pg (~> 0.20) puma (~> 3.7) - rails (~> 5.1.4) + rails (~> 5.1.6) rails-controller-testing + rest-client rspec-rails (~> 3.7) sass-rails (~> 5.0) selenium-webdriver diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index 46b20359f..8e7956cfb 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -13,3 +13,6 @@ //= require rails-ujs //= require turbolinks //= require_tree . + +//= require jquery +//= require bootstrap-sprockets diff --git a/app/assets/stylesheets/admin/tweets.scss b/app/assets/stylesheets/admin/tweets.scss index 9ec2a3732..90bc7343b 100644 --- a/app/assets/stylesheets/admin/tweets.scss +++ b/app/assets/stylesheets/admin/tweets.scss @@ -1,3 +1,7 @@ +<<<<<<< HEAD // Place all the styles related to the admin/tweets controller here. +======= +// Place all the styles related to the admin::tweets controller here. +>>>>>>> be397184eefc7ef5727e1b450016174fa9246195 // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/admin/users.scss b/app/assets/stylesheets/admin/users.scss index 925e31223..9d905c1bd 100644 --- a/app/assets/stylesheets/admin/users.scss +++ b/app/assets/stylesheets/admin/users.scss @@ -1,3 +1,7 @@ +<<<<<<< HEAD // Place all the styles related to the admin/users controller here. +======= +// Place all the styles related to the admin::users controller here. +>>>>>>> be397184eefc7ef5727e1b450016174fa9246195 // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ 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..9134ab518 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -10,6 +10,9 @@ * 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"; \ No newline at end of file diff --git a/app/assets/stylesheets/style.scss b/app/assets/stylesheets/style.scss new file mode 100644 index 000000000..251469c55 --- /dev/null +++ b/app/assets/stylesheets/style.scss @@ -0,0 +1,30 @@ +body { + margin-top: 70px; +} + +.tweet-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 10px; + margin-bottom: 20px; +} + +.popu-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 10px; + margin-bottom: 20px; +} + +.user-item { + border: 1px solid #ddd; + border-radius: 4px; + padding: 10px; + margin: 20px ; + height: 250px; +} + +.follow-button { + margin-top: 10px; + text-align: right; +} \ No newline at end of file diff --git a/app/controllers/admin/base_controller.rb b/app/controllers/admin/base_controller.rb index 4a89583f5..741ebd19b 100644 --- a/app/controllers/admin/base_controller.rb +++ b/app/controllers/admin/base_controller.rb @@ -1,3 +1,15 @@ class Admin::BaseController < ApplicationController + before_action :authenticate_admin + + private + + def authenticate_admin + unless current_user.admin? + flash[:alert] = "非工作人員請勿進入!" + redirect_to root_path + end + end end + + diff --git a/app/controllers/admin/tweets_controller.rb b/app/controllers/admin/tweets_controller.rb index 24a57566c..5952f0f6e 100644 --- a/app/controllers/admin/tweets_controller.rb +++ b/app/controllers/admin/tweets_controller.rb @@ -1,7 +1,20 @@ class Admin::TweetsController < Admin::BaseController + def index + @tweets = Tweet.order(created_at: :desc).page(params[:page]).per(10) end def destroy + @tweet = Tweet.find(params[:id]) + + if current_user.admin? + @tweet.destroy + redirect_to admin_root_path + flash[:alert] = "推文已刪除" + else + redirect_to admin_root_path + flash[:alert] = "非禮勿刪" + end end + end diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 3ba9f0a36..e29ec1db2 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,4 +1,8 @@ class Admin::UsersController < Admin::BaseController + before_action :authenticate_admin + def index + @users = User.order(tweets_count: :desc).page(params[:page]).per(10) end + end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0da627f1a..3524a0388 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,7 +1,17 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception + before_action :configure_permitted_parameters, if: :devise_controller? + before_action :authenticate_user! # 請參考 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(:acount_update, keys: [:name]) + end + end diff --git a/app/controllers/followships_controller.rb b/app/controllers/followships_controller.rb index 05f01b552..0e052e389 100644 --- a/app/controllers/followships_controller.rb +++ b/app/controllers/followships_controller.rb @@ -1,7 +1,22 @@ class FollowshipsController < ApplicationController + def create + @followship = current_user.followships.build(following_id: params[:following_id]) + @user = User.find(params[:following_id]) + if @user == current_user + flash[:alert] = @followship.errors.full_messages.to_sentence + redirect_back(fallback_location: root_path) + else + @followship.save + flash[:notice] = "Successfully followed" + redirect_back(fallback_location: root_path) + end end def destroy + @followship = current_user.followships.where(following_id: params[:id]).first + @followship.destroy + flash[:alert] = "Followship destroyed" + redirect_back(fallback_location: root_path) end end diff --git a/app/controllers/replies_controller.rb b/app/controllers/replies_controller.rb index a9b6a315b..5f2fdead6 100644 --- a/app/controllers/replies_controller.rb +++ b/app/controllers/replies_controller.rb @@ -1,9 +1,23 @@ class RepliesController < ApplicationController def index + @tweet = Tweet.find(params[:tweet_id]) + @replies = @tweet.replies.order('created_at desc') + @user = current_user + @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 + private + + def reply_params + params.require(:reply).permit(:comment) + end end diff --git a/app/controllers/tweets_controller.rb b/app/controllers/tweets_controller.rb index ad14115c1..8c41f4a67 100644 --- a/app/controllers/tweets_controller.rb +++ b/app/controllers/tweets_controller.rb @@ -1,16 +1,39 @@ class TweetsController < ApplicationController def index - @users # 基於測試規格,必須講定變數名稱,請用此變數中存放關注人數 Top 10 的使用者資料 + @tweets = Tweet.all.order('created_at desc') + @users = User.all.order(followers_count: :desc).limit(10) + @tweet = Tweet.new end def create + @user = current_user + @tweet = @user.tweets.build(tweet_params) + if @tweet.save + flash[:notice] = "Successfully tweet" + redirect_to tweets_path + elsif @tweet.errors.any? + flash[:alert] = @tweet.errors.full_messages.to_sentence + redirect_to tweets_path + end end def like + @tweet = Tweet.find(params[:id]) + @tweet.likes.create!(user: current_user) + redirect_to tweets_path end def unlike + @tweet = Tweet.find(params[:id]) + like = Like.where(tweet: @tweet, user: current_user) + like.destroy_all + redirect_to tweets_path 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 750e3c6b5..1cdb6ca54 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,24 +1,46 @@ class UsersController < ApplicationController + before_action :set_user, only:[:tweets, :edit, :update, :followings, :followers, :likes] def tweets + @tweets = @user.tweets.all.order(created_at: :desc) end def edit + unless @user == current_user + redirect_to tweets_user_path(@user) + end end def update + if @user.update(user_params) + flash[:notice] = "Profile was successfully update" + redirect_to tweets_user_path(@user) + else + flash[:alert] = "Profile was failed to update" + render :edit + end end def followings - @followings # 基於測試規格,必須講定變數名稱 + @followings = @user.followings.all end def followers - @followers # 基於測試規格,必須講定變數名稱 + @followers = @user.followers.all end def likes - @likes # 基於測試規格,必須講定變數名稱 + @likes = @user.liked_tweets + end + + private + + def user_params + params.require(:user).permit(:name, :introduction, :avatar) + end + + def set_user + @user = User.find(params[:id]) end end diff --git a/app/models/followship.rb b/app/models/followship.rb index 1aed01396..df693227e 100644 --- a/app/models/followship.rb +++ b/app/models/followship.rb @@ -1,4 +1,12 @@ class Followship < ApplicationRecord - validates :following_id, uniqueness: { scope: :user_id } + validates :following_id, uniqueness: {scope: :user_id } + after_save :update_to_user + belongs_to :user + belongs_to :following, class_name: "User" + + def update_to_user + self.following.followers_count! + end + end diff --git a/app/models/like.rb b/app/models/like.rb index d99b93a32..0d1910145 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -1,2 +1,4 @@ class Like < ApplicationRecord + belongs_to :user, counter_cache: true + belongs_to :tweet, counter_cache: true end diff --git a/app/models/reply.rb b/app/models/reply.rb index bae6f9463..83ce4bff9 100644 --- a/app/models/reply.rb +++ b/app/models/reply.rb @@ -1,2 +1,5 @@ class Reply < ApplicationRecord + belongs_to :user + belongs_to :tweet, counter_cache: true + end diff --git a/app/models/tweet.rb b/app/models/tweet.rb index 6715fada2..49b27d9e4 100644 --- a/app/models/tweet.rb +++ b/app/models/tweet.rb @@ -1,4 +1,15 @@ class Tweet < ApplicationRecord validates_length_of :description, maximum: 140 + belongs_to :user, counter_cache: true + + has_many :replies, dependent: :destroy + + has_many :likes, dependent: :destroy + has_many :liked_users, through: :likes, source: :user + + def is_liked?(user) + self.liked_users.include?(user) + end + end diff --git a/app/models/user.rb b/app/models/user.rb index 6b05b8c21..5740bcefb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,14 +1,38 @@ class User < ApplicationRecord + validates_uniqueness_of :name + validates_presence_of :name + mount_uploader :avatar, AvatarUploader + + has_many :tweets, dependent: :destroy + + has_many :replies, dependent: :restrict_with_error + + has_many :followships, dependent: :destroy + has_many :followings, through: :followships + + has_many :inverse_followships, class_name: "Followship", foreign_key: "following_id" + has_many :followers, through: :inverse_followships, source: :user + + has_many :likes, dependent: :destroy + has_many :liked_tweets, through: :likes, source: :tweet + # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - mount_uploader :avatar, AvatarUploader + + def admin? + self.role == "admin" + end - # 需要 app/views/devise 裡找到樣板,加上 name 屬性 - # 並參考 Devise 文件自訂表單後通過 Strong Parameters 的方法 - validates_presence_of :name - # 加上驗證 name 不能重覆 (關鍵字提示: uniqueness) + def following?(user) + self.followings.include?(user) + end + def followers_count! + self.update(followers_count: + self.followers.count) + end + end diff --git a/app/uploaders/avatar_uploader.rb b/app/uploaders/avatar_uploader.rb index a4a41560e..aa0898aac 100644 --- a/app/uploaders/avatar_uploader.rb +++ b/app/uploaders/avatar_uploader.rb @@ -4,7 +4,11 @@ class AvatarUploader < CarrierWave::Uploader::Base # include CarrierWave::MiniMagick # Choose what kind of storage to use for this uploader: - storage :file + if Rails.env.production? + storage :fog + else + storage :file + end # storage :fog # Override the directory where uploaded files will be stored. @@ -15,18 +19,10 @@ def store_dir # Provide a default URL as a default if there hasn't been a file uploaded: # def default_url(*args) - # # # For Rails 3.1+ asset pipeline compatibility: - # ActionController::Base.helpers.asset_path("fallback/" +["avatar.png"].compact.join('_')) - # # - # # "/images/fallback/" + [version_name, "default.png"].compact.join('_') - # end - # - # version :thumb do - # process resize_to_fit: [50, 50] - # end + # # For Rails 3.1+ asset pipeline compatibility: + # # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_')) # - # version :medium do - # process resize_to_fit: [150, 150] + # "/images/fallback/" + [version_name, "default.png"].compact.join('_') # end # Process files as they are uploaded: diff --git a/app/views/admin/tweets/index.html.erb b/app/views/admin/tweets/index.html.erb new file mode 100644 index 000000000..d43a7bfd3 --- /dev/null +++ b/app/views/admin/tweets/index.html.erb @@ -0,0 +1,52 @@ +
# | +Tweet | +# | +
---|---|---|
<%= t.id %> | +
+
+ <%= link_to "@#{t.user.name}", tweets_user_path(t.user) %>,
+ <%= t.created_at.strftime("%Y-%m-%d %H:%M")%>
+
+
+ <%= t.description %>
+
+ + <%= link_to "Reply(#{t.replies.count})", tweet_replies_path(t) %> + | + <%= link_to "Delete", admin_tweet_path(t), method: :delete, data: {confirm:"Are you sure?"} %> + | +
# | +User Name | +
---|---|
<%= u.id %> | +
+
+ <%= link_to "@"+u.name, tweets_user_path(u) %>,
+ <%= u.created_at.strftime("%Y-%m-%d %H:%M")%>
+
+ + <%= link_to " #{u.tweets_count}Tweets ", tweets_user_path(u) %> | + <%= link_to " #{u.followings.count}followings ", followings_user_path(u) %>| + <%= link_to " #{u.followers.count} followers ", followers_user_path(u) %> | + <%= link_to " #{u.likes_count} liked ", likes_user_path(u) %> + |
+