Module: PunditExtra::ResourceAutoload

Extended by:
ActiveSupport::Concern
Defined in:
lib/pundit_extra/resource_autoload.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#authorize_resourceObject



44
45
46
47
# File 'lib/pundit_extra/resource_autoload.rb', line 44

def authorize_resource
  resource = resource_instance || resource_class
  authorize resource
end

#load_resourceObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pundit_extra/resource_autoload.rb', line 26

def load_resource
  scope = resource_class
  action = params[:action]
  varname = resource_name
  if action == 'index'
    varname = controller_name
    resource = policy_scope resource_class
  elsif ['new', 'create'].include? action
    resource = scope.new
    resource.attributes = send("#{resource_name}_params") if action == 'create'
  elsif params[:id]
    resource = scope.find params[:id]
  else
    resource = nil
  end
  instance_variable_set "@#{varname}", resource
end

#resource_classObject



63
64
65
# File 'lib/pundit_extra/resource_autoload.rb', line 63

def resource_class
  resource_name.classify.constantize
end

#resource_instanceObject



67
68
69
# File 'lib/pundit_extra/resource_autoload.rb', line 67

def resource_instance
  instance_variable_get "@#{resource_name}"
end

#resource_nameObject



59
60
61
# File 'lib/pundit_extra/resource_autoload.rb', line 59

def resource_name
  controller_name.singularize
end

#skip_authorization_and_scopeObject



49
50
51
52
53
54
55
56
57
# File 'lib/pundit_extra/resource_autoload.rb', line 49

def skip_authorization_and_scope
  action = params[:action]
  if action == 'index'
    skip_authorization
    skip_policy_scope
  else
    skip_authorization
  end
end