Class: ApplicationPolicy

Inherits:
Object
  • Object
show all
Defined in:
app/policies/application_policy.rb

Overview

Base access policy

Direct Known Subclasses

PostPolicy

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record) ⇒ ApplicationPolicy

Returns a new instance of ApplicationPolicy.


5
6
7
8
# File 'app/policies/application_policy.rb', line 5

def initialize(user, record)
  @user = user || User.new
  @record = record
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record


3
4
5
# File 'app/policies/application_policy.rb', line 3

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user


3
4
5
# File 'app/policies/application_policy.rb', line 3

def user
  @user
end

Instance Method Details

#create?Boolean

Returns:

  • (Boolean)

18
19
20
# File 'app/policies/application_policy.rb', line 18

def create?
  user.admin?
end

#destroy?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'app/policies/application_policy.rb', line 34

def destroy?
  user.admin?
end

#edit?Boolean

Returns:

  • (Boolean)

30
31
32
# File 'app/policies/application_policy.rb', line 30

def edit?
  update?
end

#index?Boolean

Returns:

  • (Boolean)

10
11
12
# File 'app/policies/application_policy.rb', line 10

def index?
  user.admin?
end

#new?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'app/policies/application_policy.rb', line 22

def new?
  create?
end

#scopeObject


38
39
40
# File 'app/policies/application_policy.rb', line 38

def scope
  Pundit.policy_scope!(user, record.class)
end

#show?Boolean

Returns:

  • (Boolean)

14
15
16
# File 'app/policies/application_policy.rb', line 14

def show?
  scope.where(id: record.id).exists?
end

#update?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'app/policies/application_policy.rb', line 26

def update?
  user.admin?
end