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
- #authorizable_resource ⇒ Object
- #authorize_class_action(method = action_name) ⇒ Object
- #authorize_resource(method = action_name) ⇒ Object
- #plural_resource_var_name ⇒ Object
- #pundit_authorization_available? ⇒ Boolean
- #resource_class ⇒ Object
- #resource_human_name ⇒ Object
- #resource_name ⇒ Object
- #resource_namespace ⇒ Object
- #resource_var_name ⇒ Object
Instance Method Details
#authorizable_resource ⇒ Object
71 72 73 |
# File 'lib/crudable/rails/resourceable.rb', line 71 def 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 (method = action_name) return unless defined?(Pundit) # Check if Pundit::Authorization is included (check class, ancestors, and respond_to) return unless ([*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 (method = action_name) return unless defined?(Pundit) # Check if Pundit::Authorization is included (check class, ancestors, and respond_to) return unless resource_namespace + [], :"#{method}?" end |
#plural_resource_var_name ⇒ Object
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
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 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_class ⇒ Object
19 20 21 |
# File 'lib/crudable/rails/resourceable.rb', line 19 def resource_class resource_name.constantize end |
#resource_human_name ⇒ Object
15 16 17 |
# File 'lib/crudable/rails/resourceable.rb', line 15 def resource_human_name resource_name.humanize end |
#resource_name ⇒ Object
11 12 13 |
# File 'lib/crudable/rails/resourceable.rb', line 11 def resource_name self.class.name.split('::').last.gsub('Controller', '').singularize end |
#resource_namespace ⇒ Object
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_name ⇒ Object
23 24 25 |
# File 'lib/crudable/rails/resourceable.rb', line 23 def resource_var_name resource_name.underscore end |