docbrown/app/models/profile_pin.rb
Jeremy Friesen b115b2d17e
Appeasing Rubocop as it sneaks some changes in (#16085)
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
2022-01-13 07:48:01 -05:00

23 lines
1,001 B
Ruby

# @note When we destroy the related article (via pinnable), it's using
# dependent: :delete for the relationship. That means no
# before/after destroy callbacks will be called on this object.
class ProfilePin < ApplicationRecord
belongs_to :pinnable, polymorphic: true
belongs_to :profile, polymorphic: true
validates :profile_type, inclusion: { in: %w[User] } # Future could be organization, tag, etc.
validates :pinnable_id, uniqueness: { scope: %i[profile_id profile_type pinnable_type] }
validates :pinnable_type, inclusion: { in: %w[Article] } # Future could be comments, etc.
validate :only_five_pins_per_profile, on: :create
validate :pinnable_belongs_to_profile
private
def only_five_pins_per_profile
errors.add(:base, "cannot have more than five total pinned posts") if profile.profile_pins.size > 4
end
def pinnable_belongs_to_profile
errors.add(:pinnable_id, "must have proper permissions for pin") if pinnable.user_id != profile_id
end
end