|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +RSpec.describe RuboCop::Cop::Rails::DuplicateAssociation, :config do |
| 4 | + describe 'belongs_to' do |
| 5 | + it 'registers an offense' do |
| 6 | + expect_offense(<<~RUBY) |
| 7 | + class Post < ApplicationRecord |
| 8 | + belongs_to :foo |
| 9 | + ^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations. |
| 10 | + belongs_to :bar |
| 11 | + belongs_to :foo, -> { active } |
| 12 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations. |
| 13 | + belongs_to :blah |
| 14 | + end |
| 15 | + RUBY |
| 16 | + |
| 17 | + expect_correction(<<~RUBY) |
| 18 | + class Post < ApplicationRecord |
| 19 | + belongs_to :bar |
| 20 | + belongs_to :foo, -> { active } |
| 21 | + belongs_to :blah |
| 22 | + end |
| 23 | + RUBY |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + describe 'has_many' do |
| 28 | + it 'registers an offense' do |
| 29 | + expect_offense(<<~RUBY) |
| 30 | + class Post < ApplicationRecord |
| 31 | + has_many :foos |
| 32 | + ^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 33 | + has_many :bars |
| 34 | + has_many 'foos', -> { active } |
| 35 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 36 | + has_many :blahs |
| 37 | + end |
| 38 | + RUBY |
| 39 | + |
| 40 | + expect_correction(<<~RUBY) |
| 41 | + class Post < ApplicationRecord |
| 42 | + has_many :bars |
| 43 | + has_many 'foos', -> { active } |
| 44 | + has_many :blahs |
| 45 | + end |
| 46 | + RUBY |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe 'has_one' do |
| 51 | + it 'registers an offense' do |
| 52 | + expect_offense(<<~RUBY) |
| 53 | + class Post < ApplicationRecord |
| 54 | + has_one :foo |
| 55 | + ^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations. |
| 56 | + has_one :bar |
| 57 | + has_one :foo, -> { active } |
| 58 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foo` is defined multiple times. Don't repeat associations. |
| 59 | + has_one :blah |
| 60 | + end |
| 61 | + RUBY |
| 62 | + |
| 63 | + expect_correction(<<~RUBY) |
| 64 | + class Post < ApplicationRecord |
| 65 | + has_one :bar |
| 66 | + has_one :foo, -> { active } |
| 67 | + has_one :blah |
| 68 | + end |
| 69 | + RUBY |
| 70 | + end |
| 71 | + end |
| 72 | + |
| 73 | + describe 'has_and_belongs_to_many' do |
| 74 | + it 'registers an offense' do |
| 75 | + expect_offense(<<~RUBY) |
| 76 | + class Post < ApplicationRecord |
| 77 | + has_and_belongs_to_many :foos |
| 78 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 79 | + has_and_belongs_to_many :bars |
| 80 | + has_and_belongs_to_many :foos, -> { active } |
| 81 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 82 | + has_and_belongs_to_many :blahs |
| 83 | + end |
| 84 | + RUBY |
| 85 | + |
| 86 | + expect_correction(<<~RUBY) |
| 87 | + class Post < ApplicationRecord |
| 88 | + has_and_belongs_to_many :bars |
| 89 | + has_and_belongs_to_many :foos, -> { active } |
| 90 | + has_and_belongs_to_many :blahs |
| 91 | + end |
| 92 | + RUBY |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + describe 'all associations' do |
| 97 | + it 'marks offenses for duplicate associations of differing types' do |
| 98 | + expect_offense(<<~RUBY) |
| 99 | + class Post < ApplicationRecord |
| 100 | + has_many :foos |
| 101 | + ^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 102 | + has_and_belongs_to_many :foos |
| 103 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 104 | + has_and_belongs_to_many :bars |
| 105 | + has_and_belongs_to_many :foos, -> { active } |
| 106 | + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Association `foos` is defined multiple times. Don't repeat associations. |
| 107 | + has_and_belongs_to_many :blahs |
| 108 | +
|
| 109 | + has_one :author |
| 110 | + ^^^^^^^^^^^^^^^ Association `author` is defined multiple times. Don't repeat associations. |
| 111 | + has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment' |
| 112 | + belongs_to :author |
| 113 | + ^^^^^^^^^^^^^^^^^^ Association `author` is defined multiple times. Don't repeat associations. |
| 114 | + end |
| 115 | + RUBY |
| 116 | + |
| 117 | + expect_correction(<<~RUBY) |
| 118 | + class Post < ApplicationRecord |
| 119 | + has_and_belongs_to_many :bars |
| 120 | + has_and_belongs_to_many :foos, -> { active } |
| 121 | + has_and_belongs_to_many :blahs |
| 122 | +
|
| 123 | + has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment' |
| 124 | + belongs_to :author |
| 125 | + end |
| 126 | + RUBY |
| 127 | + end |
| 128 | + |
| 129 | + it 'does not flag non-duplicate associations' do |
| 130 | + expect_no_offenses(<<-RUBY) |
| 131 | + class Post < ApplicationRecord |
| 132 | + belongs_to :user |
| 133 | +
|
| 134 | + has_many :comments |
| 135 | + has_many :commenters, through: :comments |
| 136 | +
|
| 137 | + has_many :active_comments, -> { active }, class_name: 'Comment' |
| 138 | +
|
| 139 | + has_one :top_comment, -> { order(likes: :desc) }, class_name: 'Comment' |
| 140 | +
|
| 141 | + has_and_belongs_to_many :related_posts, class_name: 'Post' |
| 142 | + end |
| 143 | + RUBY |
| 144 | + end |
| 145 | + end |
| 146 | +end |
0 commit comments