docbrown/app/models/user_block.rb
Andy Zhao 73caa9a864 New Feature: Block Users (#4411)
* Prevent need for eager loading

* Add initial implementation of user block

* Remove debugger oops

* Add index and foreign keys for user_block table

* Use private method instead of exists

* Move channel handling logic to service object

* Update styling a bit

* Use profileDropdown file for future proofing

* Remove commented out code

* Render json: { result } for all endpoints

* Add statuses for sad paths

* Add better sad path handling in the JS

* Remove accidentally committed spec file

* Don't wait for DOM to load block button

* Use equality for accuracy in slug query

* Add status code for unauthorized requests

* Add missing comma sigh
2019-10-23 17:14:28 -04:00

21 lines
856 B
Ruby

class UserBlock < ApplicationRecord
belongs_to :blocker, foreign_key: "blocker_id", class_name: "User", inverse_of: :user_blocks
belongs_to :blocked, foreign_key: "blocked_id", class_name: "User", inverse_of: :user_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
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