Skip to content

Commit

Permalink
missing_presence_validation: ignore counter cache columns
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Sep 5, 2024
1 parent 778178e commit 4bc0eef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def detect
each_attribute(model, except: config(:ignore_attributes)) do |column|
next unless validator_needed?(model, column)
next if validator_present?(model, column)
next if counter_cache_column?(model, column)

problem!(column: column.name, model: model.name)
end
Expand Down Expand Up @@ -94,6 +95,12 @@ def presence_validator_present?(model, column)
def inclusion_validator_items(validator)
validator.options[:in] || validator.options[:within] || []
end

def counter_cache_column?(model, column)
model.reflect_on_all_associations(:has_many).any? do |reflection|
reflection.has_cached_counter? && reflection.counter_cache_column == column.name
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,38 @@ def test_timestamps_are_not_reported
refute_problems
end

def test_counter_caches_are_not_reported
Context.create_table(:companies) do |t|
t.integer :users_count, default: 0, null: false
end.define_model do
has_many :users
end

Context.create_table(:users) do |t|
t.integer :company_id
end.define_model do
belongs_to :company, counter_cache: true
end

refute_problems
end

def test_counter_caches_with_custom_names_are_not_reported
Context.create_table(:companies) do |t|
t.integer :custom_users_count, default: 0, null: false
end.define_model do
has_many :users, counter_cache: :custom_users_count
end

Context.create_table(:users) do |t|
t.integer :company_id
end.define_model do
belongs_to :company, counter_cache: :custom_users_count
end

refute_problems
end

def test_models_with_non_existent_tables_are_skipped
Context.define_model(:User)

Expand Down

0 comments on commit 4bc0eef

Please sign in to comment.