Module: Royce::Methods

Defined in:
lib/royce/methods.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(includer) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/royce/methods.rb', line 4

def self.included includer


  includer.instance_eval do
    # Add instance methods like user? admin?
    available_role_names.each do |name|
      define_method("#{name}?") do
        has_role? name
      end
    end

  end
end

Instance Method Details

#add_role(name) ⇒ Object



18
19
20
21
22
23
# File 'lib/royce/methods.rb', line 18

def add_role name
  if allowed_role? name
    role = Role.find_or_create_by(name: name.to_s)
    roles << role
  end
end

#allowed_role?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/royce/methods.rb', line 36

def allowed_role? name
  self.class.available_role_names.include? name.to_s
end

#has_role?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/royce/methods.rb', line 32

def has_role? name
  roles.map(&:name).include?(name.to_s)
end

#remove_role(name) ⇒ Object



25
26
27
28
29
30
# File 'lib/royce/methods.rb', line 25

def remove_role name
  if allowed_role? name
    role = Role.find_by(name: name.to_s)
    roles.delete role
  end
end