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

「7. 言及している日報を表示する」の差分 / 「8. Railsでテストを書く」のスタート地点 #5

Open
wants to merge 20 commits into
base: 07-report_mention
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ gem 'carrierwave'
gem 'devise'
gem 'devise-i18n'
gem 'kaminari'
gem 'rails_autolink'
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ GEM
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails_autolink (1.1.8)
actionview (> 3.1)
activesupport (> 3.1)
railties (> 3.1)
railties (7.0.6)
actionpack (= 7.0.6)
activesupport (= 7.0.6)
Expand Down Expand Up @@ -328,6 +332,7 @@ DEPENDENCIES
letter_opener_web
puma
rails (~> 7.0.6)
rails_autolink
rubocop (~> 1.45.1)
rubocop-fjord
rubocop-rails
Expand Down
40 changes: 40 additions & 0 deletions app/assets/stylesheets/mentions.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/****************
mentions-container
****************/

.mentions-container {
margin-top: 32px;
max-width: var(--single-column-w);
}

.mentions-container p {
font-size: var(--global-fz-md);
line-height: 1.8;
}

.mentions-container ul {
margin: 12px 0 0;
padding-inline-start: 0;
list-style: none;
}

.mentions-container li {
padding-bottom: 12px;
font-size: var(--global-fz-md);
line-height: 1.8;
}

.mentions-container li:not(:first-of-type) {
border-top: solid var(--global-bw) var(--global-bc-muted);
padding-top: 12px;
}

.mentions-container li:last-of-type {
padding-bottom: 0;
}

.mentions-container li small {
display: block;
font-size: 12px;
margin-top: 8px;
}
18 changes: 18 additions & 0 deletions app/models/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,32 @@ class Report < ApplicationRecord
belongs_to :user
has_many :comments, as: :commentable, dependent: :destroy

has_many :active_mentions, class_name: 'ReportMention', foreign_key: :mentioning_id, dependent: :destroy, inverse_of: :mentioning
has_many :mentioning_reports, through: :active_mentions, source: :mentioned

has_many :passive_mentions, class_name: 'ReportMention', foreign_key: :mentioned_id, dependent: :destroy, inverse_of: :mentioned
has_many :mentioned_reports, through: :passive_mentions, source: :mentioning

validates :title, presence: true
validates :content, presence: true

after_save :save_mentions

def editable?(target_user)
user == target_user
end

def created_on
created_at.to_date
end

private

MENTION_REGEXP = %r{http://localhost:3000/reports/(\d+)}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(別解)後読みを使うと31行目のflattenをなくせます。

Suggested change
MENTION_REGEXP = %r{http://localhost:3000/reports/(\d+)}
MENTION_REGEXP = %r{(?<=http://localhost:3000/reports/)\d+}

def save_mentions
active_mentions.destroy_all
ids = content.to_s.scan(MENTION_REGEXP).flatten.uniq
reports = Report.where(id: ids).where.not(id:)
self.mentioning_reports += reports
end
end
8 changes: 8 additions & 0 deletions app/models/report_mention.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class ReportMention < ApplicationRecord
belongs_to :mentioning, class_name: 'Report'
belongs_to :mentioned, class_name: 'Report'

validates :mentioned_id, uniqueness: { scope: :mentioning_id }
end
17 changes: 17 additions & 0 deletions app/views/reports/_mentions.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<div class="mentions-container">
<strong><%= t('.title') %>:</strong>
<% if mentioned_reports = report.mentioned_reports.includes(mentioned_reports: :user).order(id: :desc).presence %>
<ul>
<% mentioned_reports.each do |mentioned_report| %>
<li>
<%= link_to mentioned_report.title, mentioned_report %>
<small>
(<%= link_to mentioned_report.user.name_or_email, mentioned_report.user %> - <%= l mentioned_report.created_on %>)
</small>
</li>
<% end %>
</ul>
<% else %>
<p>(<%= t('.no_mentions') %>)</p>
<% end %>
</div>
2 changes: 1 addition & 1 deletion app/views/reports/_report.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<p>
<strong><%= Report.human_attribute_name(:content) %>:</strong>
<%= format_content report.content %>
<%= auto_link(format_content(report.content)) %>
</p>

<p>
Expand Down
2 changes: 2 additions & 0 deletions app/views/reports/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<%= render @report %>
</div>

<%= render 'reports/mentions', report: @report %>

<%= render 'shared/comments', commentable: @report %>

<nav class="page-nav">
Expand Down
4 changes: 4 additions & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,10 @@ ja:
create: コメントする
delete: 削除
no_comments: コメントはまだありません
reports:
mentions:
title: この日報に言及している日報
no_mentions: この日報に言及している日報はまだありません
# devise-i18n-viewsをオーバーライド
devise:
registrations:
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20230102085641_create_report_mentions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateReportMentions < ActiveRecord::Migration[7.0]
def change
create_table :report_mentions do |t|
t.references :mention_to, null: false, foreign_key: { to_table: 'reports' }, index: false
t.references :mentioned_by, null: false, foreign_key: { to_table: 'reports' }

t.timestamps
end
add_index :report_mentions, %i[mention_to_id mentioned_by_id], unique: true
end
end
6 changes: 6 additions & 0 deletions db/migrate/20240505022253_rename_report_mentions_columns.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class RenameReportMentionsColumns < ActiveRecord::Migration[7.0]
def change
rename_column :report_mentions, :mention_to_id, :mentioning_id
rename_column :report_mentions, :mentioned_by_id, :mentioned_id
end
end
13 changes: 12 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,25 @@ def add_comments_to(commentable, contents)
end
end

mention_texts = <<~TEXT.lines(chomp: true)
この日報を参考にさせてもらいました。
この日報が役に立ちました。
昨日こちらの日報を読みました。
この日報に書かれている内容が素敵だなと思いました。
以下の日報に手順が書いてありました。
TEXT
Report.transaction do
# 先頭の20件には何らかのメンションをランダムに付ける
Report.order(id: :desc).limit(20).each do |report|
mention_count = rand(0..5)
mentioning_reports = Report.where('id > ?', report.id).to_a.sample(mention_count)
mentioning_reports.each do |mentioning_report|
mentioning_report.update!(content: <<~CONTENT)
#{mention_texts.sample}
http://localhost:3000/reports/#{report.id}
CONTENT
end
end
end

puts '初期データの投入が完了しました。' # rubocop:disable Rails/Output
11 changes: 11 additions & 0 deletions test/fixtures/report_mentions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
9 changes: 9 additions & 0 deletions test/models/report_mention_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

require 'test_helper'

class ReportMentionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end