docbrown/app/models/user_block.rb
Anna Buianova 01456fe029 [deploy] Finalize deleting user associations (#5624)
* Prepare factories and associations for Users::DeleteActivity specs

* Altered user associations, delete the needed associations in Users::DeleteActivity

* Moved and refined user deletion spec

* Removed useless commented user associations

* Refactored BackupData factory

* Refactored ProfilePin factory

* Refactored Reaction factory

* Fixed specs: Keep default _type in factories or specify it in tests

* Ok, notes shouldn't be deleted on user deletion

* Fixed creating reactable for specs

* Specify commentable/commentable_type explicitly when creating data for tests

* Fixed typo in the comments spec

* Removed unnneeded space

* Added aggregate_failures for testing user associations deletion

* Keep feedback_messages while deleting user activities
2020-01-23 11:29:47 -05:00

24 lines
874 B
Ruby

class UserBlock < ApplicationRecord
belongs_to :blocker, foreign_key: "blocker_id", class_name: "User", inverse_of: :blocker_blocks
belongs_to :blocked, foreign_key: "blocked_id", class_name: "User", inverse_of: :blocked_blocks
validates :blocked_id, :blocker_id, :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"
class << self
def blocking?(blocker_id, blocked_id)
exists?(blocker_id: blocker_id, blocked_id: blocked_id)
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
end