I was working on another branch and as part of my commit, Rubocop
removed a validation (but not the spec that asserted the validation).
Below is the "non-updating" rubocop offense on the other branch.
```shell
❯ rubocop ./app/models/notification_subscription.rb
Inspecting 1 file
C
Offenses:
app/models/notification_subscription.rb:13:29: C: [Correctable]
Rails/RedundantPresenceValidationOnBelongsTo: Remove explicit presence
validation for notifiable_id.
validates :notifiable_id, presence: true
^^^^^^^^^^^^^^
1 file inspected, 1 offense detected, 1 offense auto-correctable
```
To remediate, I ran:
```shell
> rubocop --only "Rails/RedundantPresenceValidationOnBelongsTo" \
--auto-correct
```
This resolved the `app/models`. Then did some regex magic and removed
the assertions from `spec/models`.
For Forem folks, I wrote a [forem.team post][1] discuss if this is how
we want to proceed.
[1]:https://forem.team/jeremy/rubocop-auto-updating-mayhem-33a6
39 lines
1.2 KiB
Ruby
39 lines
1.2 KiB
Ruby
class UserBlock < ApplicationRecord
|
|
belongs_to :blocker, class_name: "User", inverse_of: :blocker_blocks
|
|
belongs_to :blocked, class_name: "User", inverse_of: :blocked_blocks
|
|
|
|
validates :config, presence: true
|
|
validates :blocked_id, uniqueness: { scope: %i[blocker_id] }
|
|
validates :config, inclusion: { in: %w[default] }
|
|
validate :blocker_cannot_be_same_as_blocked
|
|
|
|
counter_culture :blocker, column_name: "blocking_others_count"
|
|
counter_culture :blocked, column_name: "blocked_by_count"
|
|
|
|
after_create :bust_blocker_cache
|
|
before_destroy :bust_blocker_cache
|
|
|
|
BLOCKED_IDS_CACHE_KEY = "blocked_ids_for_blocker/".freeze
|
|
|
|
class << self
|
|
def blocking?(blocker_id, blocked_id)
|
|
exists?(blocker_id: blocker_id, blocked_id: blocked_id)
|
|
end
|
|
|
|
def cached_blocked_ids_for_blocker(blocker_id)
|
|
Rails.cache.fetch("#{BLOCKED_IDS_CACHE_KEY}#{blocker_id}", expires_in: 48.hours) do
|
|
where(blocker_id: blocker_id).pluck(:blocked_id)
|
|
end
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def blocker_cannot_be_same_as_blocked
|
|
errors.add(:blocker_id, "can't be the same as the blocked_id") if blocker_id == blocked_id
|
|
end
|
|
|
|
def bust_blocker_cache
|
|
Rails.cache.delete("#{BLOCKED_IDS_CACHE_KEY}#{blocker_id}")
|
|
end
|
|
end
|