Module: Crudable::Rails::Resourceable

Extended by:
ActiveSupport::Concern
Defined in:
lib/crudable/rails/resourceable.rb

Overview

Resourceable

Provides a number of resource helpers for controllers supporting Crudable

Instance Method Summary collapse

Instance Method Details

#authorizable_resourceObject



71
72
73
# File 'lib/crudable/rails/resourceable.rb', line 71

def authorizable_resource
  instance_variable_get("@#{resource_var_name}")
end

#authorize_class_action(method = action_name) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/crudable/rails/resourceable.rb', line 45

def authorize_class_action(method = action_name)
  return unless defined?(Pundit)
  # Check if Pundit::Authorization is included (check class, ancestors, and respond_to)
  return unless pundit_authorization_available?

  authorize([*resource_namespace, resource_class], :"#{method}?")
end

#authorize_resource(method = action_name) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/crudable/rails/resourceable.rb', line 37

def authorize_resource(method = action_name)
  return unless defined?(Pundit)
  # Check if Pundit::Authorization is included (check class, ancestors, and respond_to)
  return unless pundit_authorization_available?

  authorize resource_namespace + [authorizable_resource], :"#{method}?"
end

#plural_resource_var_nameObject



27
28
29
# File 'lib/crudable/rails/resourceable.rb', line 27

def plural_resource_var_name
  resource_name.pluralize.underscore
end

#pundit_authorization_available?Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/crudable/rails/resourceable.rb', line 53

def pundit_authorization_available?
  return false unless defined?(Pundit)
  return false unless defined?(Pundit::Authorization)
  # Check if included in this class
  return true if self.class.included_modules.include?(Pundit::Authorization)

  # Check if included in any ancestor (Class or Module)
  # This handles cases where Pundit::Authorization is included in ApplicationController
  ancestors_to_check = self.class.ancestors.select { |a| a.is_a?(Class) || a.is_a?(Module) }
  return true if ancestors_to_check.any? { |ancestor| ancestor.included_modules.include?(Pundit::Authorization) }
  # Fallback: check if authorize method is available (works in most cases)
  # Use method_defined? for more reliable check than respond_to?
  return true if self.class.method_defined?(:authorize) || self.class.private_method_defined?(:authorize)

  # Last resort: respond_to check
  respond_to?(:authorize, true)
end

#resource_classObject



19
20
21
# File 'lib/crudable/rails/resourceable.rb', line 19

def resource_class
  resource_name.constantize
end

#resource_human_nameObject



15
16
17
# File 'lib/crudable/rails/resourceable.rb', line 15

def resource_human_name
  resource_name.humanize
end

#resource_nameObject



11
12
13
# File 'lib/crudable/rails/resourceable.rb', line 11

def resource_name
  self.class.name.split('::').last.gsub('Controller', '').singularize
end

#resource_namespaceObject



31
32
33
34
35
# File 'lib/crudable/rails/resourceable.rb', line 31

def resource_namespace
  namespaces = self.class.name.split('::')
  namespaces.pop
  namespaces.map { |n| n.underscore.to_sym }
end

#resource_var_nameObject



23
24
25
# File 'lib/crudable/rails/resourceable.rb', line 23

def resource_var_name
  resource_name.underscore
end