Skip to content

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

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

Open
wants to merge 23 commits into
base: 07-report_mention
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
35b8225
日報のメンション機能を実装
JunichiIto Jan 2, 2023
d110f7b
同じレポートへの言及はメンション対象外とする
JunichiIto Jan 3, 2023
2638604
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 3, 2023
b0a817f
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 3, 2023
398d331
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 4, 2023
ad354b9
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 4, 2023
8fb87c0
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 24, 2023
e0fab4b
Refactor Report#save_mentions
JunichiIto Jan 24, 2023
49c9140
Merge branch '07-report_mention' into 08-testing
JunichiIto Jan 24, 2023
e35545f
Merge branch '07-report_mention' into 08-testing
JunichiIto Feb 11, 2023
cbbbcde
Add mentions.css
JunichiIto Feb 11, 2023
28b2197
Refactor save_mentions
JunichiIto Feb 11, 2023
088baaa
Merge branch '07-report_mention' into 08-testing
JunichiIto Feb 11, 2023
e39a702
Merge branch '07-report_mention' into 08-testing
JunichiIto Feb 11, 2023
fb3d277
bundle install
JunichiIto Feb 11, 2023
c2809ac
Merge branch '07-report_mention' into 08-testing
JunichiIto Feb 19, 2023
bf2b4e2
関連の定義を修正
JunichiIto Apr 22, 2023
0e60331
Merge branch '07-report_mention' into 08-testing
JunichiIto Aug 5, 2023
492ef02
Install missing gems
JunichiIto Aug 5, 2023
8e04440
関連の名称をわかりやすい名前に改善
JunichiIto May 5, 2024
b7fc2f8
テスト安定化に向けた修正
JunichiIto Jun 1, 2025
12a38ed
Merge pull request #46 from fjordllc/08-testing-fix-flaky-tests
komagata Jun 2, 2025
4f4dfd4
concurrent-rubyのバージョンを1.3.4に固定
JunichiIto Jun 16, 2025
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 @@ -84,3 +84,4 @@ gem 'carrierwave'
gem 'devise'
gem 'devise-i18n'
gem 'kaminari'
gem 'rails_autolink'
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,8 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.5.0)
loofah (~> 2.19, >= 2.19.1)
rails_autolink (1.1.7)
rails (> 3.1)
railties (7.0.4.2)
actionpack (= 7.0.4.2)
activesupport (= 7.0.4.2)
Expand Down Expand Up @@ -323,6 +325,7 @@ DEPENDENCIES
letter_opener_web
puma (~> 5.0)
rails (~> 7.0.4)
rails_autolink
rubocop-fjord
rubocop-rails
selenium-webdriver
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: :mentioned_by_id, dependent: :destroy, inverse_of: :mentioned_by
has_many :mentioning_reports, through: :active_mentions, source: :mention_to

has_many :passive_mentions, class_name: 'ReportMention', foreign_key: :mention_to_id, dependent: :destroy, inverse_of: :mention_to
has_many :mentioned_reports, through: :passive_mentions, source: :mentioned_by

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 :mention_to, class_name: 'Report'
belongs_to :mentioned_by, class_name: 'Report'

validates :mentioned_by_id, uniqueness: { scope: :mention_to_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
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