Class: Guts::ApplicationPolicy Abstract

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

Overview

This class is abstract.

Base application policies for Pundit

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record) ⇒ ApplicationPolicy

Initilization for a policy

Parameters:

  • user (Object|nil)

    the user’s object

  • record (Object|nil)

    the record from the database

Raises:

  • (Pundit::NotAuthorizedError)

    if user is not logged in



15
16
17
18
19
20
# File 'app/policies/guts/application_policy.rb', line 15

def initialize(user, record)
  raise Pundit::NotAuthorizedError unless user

  @user = user
  @record = record
end

Instance Attribute Details

#recordObject|nil (readonly)

Returns the record from the database.

Returns:

  • (Object|nil)

    the record from the database



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

def record
  @record
end

#userObject|nil (readonly)

Returns the user’s object.

Returns:

  • (Object|nil)

    the user’s object



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

def user
  @user
end

Instance Method Details

#create?Boolean

Create method policy

Returns:

  • (Boolean)

    allowed or denied



36
37
38
# File 'app/policies/guts/application_policy.rb', line 36

def create?
  standard_check :create
end

#destroy?Boolean

Destroy method policy

Returns:

  • (Boolean)

    allowed or denied



60
61
62
# File 'app/policies/guts/application_policy.rb', line 60

def destroy?
  standard_check :destory
end

#edit?Boolean

Edit method policy

Returns:

  • (Boolean)

    allowed or denied



54
55
56
# File 'app/policies/guts/application_policy.rb', line 54

def edit?
  update?
end

#index?Boolean

Index method policy

Returns:

  • (Boolean)

    allowed or denied



24
25
26
# File 'app/policies/guts/application_policy.rb', line 24

def index?
  standard_check :index
end

#new?Boolean

New method policy

Returns:

  • (Boolean)

    allowed or denied



42
43
44
# File 'app/policies/guts/application_policy.rb', line 42

def new?
  create?
end

#scopeObject

Scope for policy

Returns:

  • (Object)

    policy scope



66
67
68
# File 'app/policies/guts/application_policy.rb', line 66

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

#show?Boolean

Show method policy

Returns:

  • (Boolean)

    allowed or denied



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

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

#update?Boolean

Update method policy

Returns:

  • (Boolean)

    allowed or denied



48
49
50
# File 'app/policies/guts/application_policy.rb', line 48

def update?
  standard_check :update
end