Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ユーザープロフィールページの作成 #8

Merged
merged 20 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ gem 'slim-rails'
# ページネーション
gem 'kaminari'

group :production do
# AWSのS3用
gem 'aws-sdk-s3', require: false
gem 'fog-aws'
end

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
Expand Down
40 changes: 40 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ GEM
addressable (2.8.4)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
aws-eventstream (1.2.0)
aws-partitions (1.816.0)
aws-sdk-core (3.181.0)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
aws-sigv4 (~> 1.5)
jmespath (~> 1, >= 1.6.1)
aws-sdk-kms (1.71.0)
aws-sdk-core (~> 3, >= 3.177.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.134.0)
aws-sdk-core (~> 3, >= 3.181.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.6)
aws-sigv4 (1.6.0)
aws-eventstream (~> 1, >= 1.0.2)
base64 (0.1.1)
bcrypt (3.1.19)
bindex (0.8.1)
Expand Down Expand Up @@ -116,6 +132,7 @@ GEM
railties (>= 3.2)
erubi (1.12.0)
erubis (2.7.0)
excon (0.102.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
Expand All @@ -126,6 +143,22 @@ GEM
faraday-net_http (>= 2.0, < 3.1)
ruby2_keywords (>= 0.0.4)
faraday-net_http (3.0.2)
fog-aws (3.19.0)
fog-core (~> 2.1)
fog-json (~> 1.1)
fog-xml (~> 0.1)
fog-core (2.3.0)
builder
excon (~> 0.71)
formatador (>= 0.2, < 2.0)
mime-types
fog-json (1.2.0)
fog-core
multi_json (~> 1.10)
fog-xml (0.1.4)
fog-core
nokogiri (>= 1.5.11, < 2.0.0)
formatador (1.1.0)
globalid (1.1.0)
activesupport (>= 5.0)
hashie (5.0.0)
Expand All @@ -141,6 +174,7 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
jmespath (1.6.2)
jsbundling-rails (1.1.2)
railties (>= 6.0.0)
json (2.6.3)
Expand Down Expand Up @@ -178,9 +212,13 @@ GEM
marcel (1.0.2)
matrix (0.4.2)
method_source (1.0.0)
mime-types (3.5.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2023.0808)
mini_mime (1.1.2)
minitest (5.18.1)
msgpack (1.7.2)
multi_json (1.15.0)
multi_xml (0.6.0)
net-imap (0.3.6)
date
Expand Down Expand Up @@ -382,6 +420,7 @@ PLATFORMS
x86_64-linux

DEPENDENCIES
aws-sdk-s3
bootsnap
capybara
cssbundling-rails
Expand All @@ -390,6 +429,7 @@ DEPENDENCIES
devise-i18n
dotenv-rails
factory_bot_rails
fog-aws
html2slim
htmlbeautifier
jbuilder
Expand Down
Binary file added app/assets/images/dummy_header_photo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/dummy_photo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@ $btn-border-radius: 1rem;

.pagination {
justify-content: center;
}

.profile-images {
position: relative;
}

.upper {
display: block;
width: 100%;
height: auto;
max-height: 200px;
}

.profile{
display: block;
position: absolute;
left: 3%;
top: 70%;
width: 15%;
height: auto;
}

.profile-content {
display: block;
}
5 changes: 2 additions & 3 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class PostsController < ApplicationController
def index
@user = user_signed_in? ? current_user : User.new
@posts = Post.includes(:user).latest.page(params[:posts_page]).per(10)
# 自分がフォローしている人
@followees = @user.followees
followee_ids = @followees.map(&:id)
# 自分がフォローしている投稿
followee_ids = @user.followees.map(&:id)
@followee_posts = Post.includes(:user).followee_posts(followee_ids:).page(params[:followee_page]).per(10)
end
end
11 changes: 11 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class UsersController < ApplicationController
def show

Check notice on line 4 in app/controllers/users_controller.rb

View workflow job for this annotation

GitHub Actions / rails_best_practices

[rails_best_practices] app/controllers/users_controller.rb#L4

move model logic into model (@user use_count > 4)
Raw output
/home/runner/work/twitter_clone/twitter_clone/app/controllers/users_controller.rb:4 - move model logic into model (@user use_count > 4)
@user = User.find(params[:id])
@posts = @user.posts.includes(:user).latest.page(params[:posts_page]).per(10)
@liking_posts = @user.liking_posts.includes(:user).latest.page(params[:like_page]).per(10)
@reposting_posts = @user.reposting_posts.includes(:user).latest.page(params[:repost_page]).per(10)
@commenting_posts = @user.commenting_posts.includes(:user).latest.page(params[:comment_page]).per(10)
end
end
8 changes: 8 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Comment < ApplicationRecord
belongs_to :user
belongs_to :post

validates :content, presence: true
end
6 changes: 6 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Like < ApplicationRecord
belongs_to :user
belongs_to :post
end
9 changes: 9 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
class Post < ApplicationRecord
belongs_to :user

has_many :likes, dependent: :destroy
has_many :liked_users, through: :likes, source: :user

has_many :reposts, dependent: :destroy
has_many :reposted_users, through: :reposts, source: :user

has_many :comments, dependent: :destroy
has_many :commented_users, through: :comments, source: :user

validates :content, presence: true, length: { maximum: 140 }

scope :latest, -> { order(created_at: :desc) }
Expand Down
6 changes: 6 additions & 0 deletions app/models/repost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Repost < ApplicationRecord
belongs_to :user
belongs_to :post
end
27 changes: 27 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class User < ApplicationRecord
before_save :attach_dummy_photo

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
Expand All @@ -9,6 +11,8 @@ class User < ApplicationRecord
:omniauthable, omniauth_providers: %i[github]

has_many :posts, dependent: :destroy
has_one_attached :photo
has_one_attached :header_photo

# 自分がフォローしているユーザたち
has_many :following_status, class_name: 'Follow', foreign_key: :follower_id, dependent: :destroy,
Expand All @@ -20,6 +24,15 @@ class User < ApplicationRecord
inverse_of: 'followee'
has_many :followers, through: :followed_status, source: :follower

has_many :likes, dependent: :destroy
has_many :liking_posts, through: :likes, source: :post

has_many :reposts, dependent: :destroy
has_many :reposting_posts, through: :reposts, source: :post

has_many :comments, dependent: :destroy
has_many :commenting_posts, through: :comments, source: :post

with_options presence: true do
validates :name
validates :user_name, uniqueness: true
Expand All @@ -36,4 +49,18 @@ def self.from_omniauth(auth)
u.user_name = auth.info.nickname
end
end

private

def attach_dummy_photo
unless photo.attached?
photo.attach(io: File.open(Rails.root.join('app/assets/images/dummy_photo.jpg')),
filename: 'dummy_photo.jpg')
end

return if header_photo.attached?

header_photo.attach(io: File.open(Rails.root.join('app/assets/images/dummy_header_photo.jpg')),
filename: 'dummy_header_photo.jpg')
end
end
25 changes: 16 additions & 9 deletions app/views/posts/_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
= form_with url: posts_path, model: Post.new , locale: true do |f|
.card.mb-3
.card-body
= f.text_area :content, class: 'form-control', placeholder: 'いまどうしてる?', rows: 5
.d-flex.justify-content-between.mt-4
a.text-primary[href="#"]
i.fs-3.bi-image
p
= link_to "ツイートする", posts_path, data: { turbo: false }, class: "btn btn-primary px-4 text-white fw-bold"
- if user_signed_in?
= form_with url: posts_path, model: Post.new , locale: true do |f|
.card.mb-3
.card-body.container-grid
.row
.col-1
- if user.photo.attached?
= link_to user_path(current_user), data: { turbo: false }
= image_tag user.photo, class: "rounded-circle img-fluid", style: " max-width: 150%; height: auto;"
.col
= f.text_area :content, class: 'form-control', placeholder: 'いまどうしてる?', rows: 5
.d-flex.justify-content-between.mt-4
a.text-primary[href="#"]
i.fs-3.bi-image
p
= link_to "ツイートする", posts_path, data: { turbo: false }, class: "btn btn-primary px-4 text-white fw-bold"
27 changes: 16 additions & 11 deletions app/views/posts/_post.html.slim
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
.card
.card-body
.d-flex.flex-row
p.card-title.fw-bold
= post.user_name
p.card-subtitle.text-muted.d-flex.align-items-center.mx-2
| @
= post.user_user_name
| ・
= l post.created_at
p.card-text
= post.content.html_safe
.card-body.container-grid
.row
.col-1
- if post.user.photo.attached?
= image_tag post.user.photo, class: "rounded-circle img-fluid", style: " max-width: 150%; height: auto;"
.col
.d-flex.flex-row
p.card-title.fw-bold
= post.user_name
p.card-subtitle.text-muted.d-flex.align-items-center.mx-2
| @
= post.user_user_name
| ・
= l post.created_at
p.card-text
= post.content.html_safe
2 changes: 1 addition & 1 deletion app/views/posts/index.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
button#pills-profile-tab.fw-bold.nav-link[data-bs-toggle="pill" data-bs-target="#pills-profile" type="button" role="tab" aria-controls="pills-profile" aria-selected="false"]
| フォロー中
#pills-tabContent.tab-content
= render 'form'
= render 'form', :user => @user
#pills-home.tab-pane.fade.show.active
= paginate @posts, :param_name => 'posts_page'
- @posts.each do |post|
Expand Down
Loading
Loading