docbrown/app/models/organization_membership.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

26 lines
1 KiB
Ruby

# @note When we destroy the related user, it's using dependent:
# :delete for the relationship. That means no before/after
# destroy callbacks will be called on this object.
class OrganizationMembership < ApplicationRecord
belongs_to :user
belongs_to :organization
USER_TYPES = %w[admin member guest].freeze
validates :type_of_user, presence: true
validates :user_id, uniqueness: { scope: :organization_id }
validates :type_of_user, inclusion: { in: USER_TYPES }
after_create :update_user_organization_info_updated_at
after_destroy :update_user_organization_info_updated_at
scope :admin, -> { where(type_of_user: "admin") }
scope :member, -> { where(type_of_user: %w[admin member]) }
# @note In the case where we delete the user, we don't need to worry
# about updating the user. Hence the the `user has_many
# :organization_memberships dependent: :delete_all`
def update_user_organization_info_updated_at
user.touch(:organization_info_updated_at)
end
end