Welcome to mirror list, hosted at ThFree Co, Russian Federation.

role.rb « models « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cbe5644ef1d7f4f743b4beb958fb9ea3793d2f1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# NOTE add the person object you want to attach role to...

class Role < ActiveRecord::Base
  belongs_to :person

  validates :person, presence: true
  validates :name, uniqueness: {scope: :person_id}
  validates :name, inclusion: {in: %w(admin moderator spotlight)}

  scope :admins, -> { where(name: "admin") }
  scope :moderators, -> { where(name: %w(moderator admin)) }

  def self.is_admin?(person)
    exists?(person_id: person.id, name: "admin")
  end

  def self.add_admin(person)
    find_or_create_by(person_id: person.id, name: "admin")
  end

  def self.moderator?(person)
    moderators.exists?(person_id: person.id)
  end

  def self.add_moderator(person)
    find_or_create_by(person_id: person.id, name: "moderator")
  end

  def self.add_spotlight(person)
    find_or_create_by(person_id: person.id, name: "spotlight")
  end
end