Class: Faalis::ApplicationPolicy

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

Overview

Main class for all the Faalis Policy classes. It’s totally a minimume Policy.

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.



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

def initialize(user, record)
  @user = user
  @record = record
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block_given) ⇒ Object



28
29
30
31
# File 'app/policies/faalis/application_policy.rb', line 28

def method_missing(m, *args, &block_given)
  return authorize? m.to_s[0..-2] if m.to_s =~ /.*\?$/
  super
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



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

def record
  @record
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#authorize?(action) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/policies/faalis/application_policy.rb', line 11

def authorize?(action)
  return false if @user.nil?
  return true if @user.admin?

  # Check for ownership of the reocrd
  record_class = @record.class

  unless [Class, String, Symbol].include?(record_class)
    if @user.has_ownership?(@record)
      return false if !@user.owned? @record
    end
    @record = @record.class
  end

  user.can? action, @record.to_s
end

#scopeObject



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

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