Class: NaturalResource::Policy

Inherits:
Object
  • Object
show all
Includes:
PolicyPreconditions
Defined in:
lib/natural_resource/policy.rb

Direct Known Subclasses

ApplicationPolicy

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, record) ⇒ Policy

Returns a new instance of Policy.



10
11
12
13
14
15
# File 'lib/natural_resource/policy.rb', line 10

def initialize(user, record)
  @user = user
  @record = record
  @errors = Hash.new { |h,k| h[k]= Set.new([]) }
  @failure_status = nil
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'lib/natural_resource/policy.rb', line 8

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'lib/natural_resource/policy.rb', line 8

def user
  @user
end

Instance Method Details

#anon?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/natural_resource/policy.rb', line 61

def anon?
  user.nil?
end

#create?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/natural_resource/policy.rb', line 37

def create?
  false
end

#destroy?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/natural_resource/policy.rb', line 53

def destroy?
  false
end

#edit?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/natural_resource/policy.rb', line 49

def edit?
  update?
end

#errorsObject



21
22
23
24
25
26
27
# File 'lib/natural_resource/policy.rb', line 21

def errors
  if @errors.keys.empty?
    {user: ['does not have permission to perform this action']}
  else
    @errors
  end
end

#failure_statusObject



17
18
19
# File 'lib/natural_resource/policy.rb', line 17

def failure_status
  @failure_status.presence || :unauthorized
end

#index?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/natural_resource/policy.rb', line 29

def index?
  false
end

#new?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/natural_resource/policy.rb', line 41

def new?
  create?
end

#own_record?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/natural_resource/policy.rb', line 57

def own_record?
  user && record.user == user
end

#scopeObject



65
66
67
# File 'lib/natural_resource/policy.rb', line 65

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

#show?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/natural_resource/policy.rb', line 33

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

#update?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/natural_resource/policy.rb', line 45

def update?
  false
end