Class: EnjuQuestion::Ability

Inherits:
Object
  • Object
show all
Includes:
CanCan::Ability
Defined in:
app/models/enju_question/ability.rb

Instance Method Summary collapse

Constructor Details

#initialize(user, ip_address = nil) ⇒ Ability

Returns a new instance of Ability.



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
33
34
35
36
37
38
39
40
41
42
# File 'app/models/enju_question/ability.rb', line 5

def initialize(user, ip_address = nil)
  case user.try(:role).try(:name)
  when 'Administrator'
    can :manage, Answer
    can :manage, Question
  when 'Librarian'
    can :manage, Answer
    can :manage, Question
  when 'User'
    can [:index, :create], Answer
    can :show, Answer do |answer|
      if answer.user == user
        true
      elsif answer.question.shared
        true
      end
    end
    can [:update, :destroy, :delete], Answer do |answer|
      answer.user == user
    end
    can [:index, :create], Question
    can [:update, :destroy, :delete], Question do |question|
      question.user == user
    end
    can :show, Question do |question|
      question.user == user or question.shared
    end
  else
    can :index, Answer
    can :show, Answer do |answer|
      answer.question.shared
    end
    can :index, Question
    can :show, Question do |question|
      question.shared
    end
  end
end