Last active
October 25, 2020 04:29
-
-
Save mh-mobile/01ea7a4e14011b9a59605ffa3de36cab to your computer and use it in GitHub Desktop.
コメントの関連付けテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
git_source(:github) { |repo| "https://github.com/#{repo}.git" } | |
# Activate the gem you are reporting the issue against. | |
gem "activerecord", "6.0.0" | |
gem "sqlite3" | |
gem "byebug" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
# This connection will do for database-independent bug reports. | |
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
module Commentable | |
extend ActiveSupport::Concern | |
included do | |
has_many :comments, as: :commentable, dependent: :delete_all | |
end | |
end | |
create_table :users, force: true do |t| | |
t.string :name, null: false | |
t.timestamps | |
end | |
create_table :books, force: true do |t| | |
t.string :user_id | |
t.string :name, null: false | |
t.timestamps | |
end | |
create_table :comments, force: true do |t| | |
t.string :user_id, null: false | |
t.belongs_to :commentable, polymorphic: true | |
t.string :content | |
t.timestamps | |
end | |
create_table :reports, force: true do |t| | |
t.string :user_id | |
t.string :name, null: false | |
t.timestamps | |
end | |
end | |
class ApplicationRecord < ActiveRecord::Base | |
self.abstract_class = true | |
end | |
class User < ApplicationRecord | |
has_many :books, dependent: :destroy | |
has_many :reports, dependent: :destroy | |
has_many :comments | |
has_many :report_comments, through: :reports, source: :comments | |
has_many :book_comments, through: :books, source: :comments | |
end | |
class Comment < ApplicationRecord | |
belongs_to :commentable, polymorphic: true | |
belongs_to :user | |
end | |
class Report < ApplicationRecord | |
include Commentable | |
belongs_to :user | |
end | |
class Book < ApplicationRecord | |
belongs_to :user | |
include Commentable | |
end | |
class CommentsTest < Minitest::Test | |
def test_association | |
mh = User.create!(name: "mh") | |
ruby_book = Book.new(name: "ruby") | |
mh.books << ruby_book | |
mh_book_comment_1 = Comment.new(content: "楽しい言語", user_id: mh.id) | |
mh_book_comment_2 = Comment.new(content: "スクリプト言語", user_id: mh.id) | |
ruby_book.comments << [mh_book_comment_1, mh_book_comment_2] | |
today_report = Report.new(name: "日報") | |
mh.reports << today_report | |
mh_report_comment_1 = Comment.new(content: "がんばった", user_id: mh.id) | |
mh_report_comment_2 = Comment.new(content: "いいね", user_id: mh.id) | |
mh_report_comment_3 = Comment.new(content: "すごい!", user_id: mh.id) | |
today_report.comments << [mh_report_comment_1, mh_report_comment_2, mh_report_comment_3] | |
assert_equal true, mh.comments.include?(mh_book_comment_1) | |
assert_equal true, mh.comments.include?(mh_book_comment_2) | |
assert_equal true, mh.comments.include?(mh_report_comment_1) | |
assert_equal true, mh.comments.include?(mh_report_comment_2) | |
assert_equal 5, mh.comments.count | |
assert_equal true, mh.book_comments.include?(mh_book_comment_1) | |
assert_equal true, mh.book_comments.include?(mh_book_comment_2) | |
assert_equal 2, mh.book_comments.count | |
assert_equal true, mh.report_comments.include?(mh_report_comment_1) | |
assert_equal true, mh.report_comments.include?(mh_report_comment_2) | |
assert_equal true, mh.report_comments.include?(mh_report_comment_3) | |
assert_equal 3, mh.report_comments.count | |
mh.destroy! | |
assert_equal 0, Comment.all.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment