Module: Crudable::Rails::Nestable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/crudable/rails/nestable.rb
Overview
Nestable Module
Instance Method Summary collapse
- #find_parent ⇒ Object
- #parent_class ⇒ Object
-
#parent_friendly_finders? ⇒ Boolean
By default, if friendly finders are enabled for the resource, we will also attempt to use friendly finders for the parent (when available).
- #parent_id_param ⇒ Object
- #parent_id_param_keys ⇒ Object
- #parent_present? ⇒ Boolean
- #parent_resource_association ⇒ Object
- #parent_var_name ⇒ Object
- #request_path_parameters_present? ⇒ Boolean
- #use_parent_as_scope? ⇒ Boolean
Instance Method Details
#find_parent ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/crudable/rails/nestable.rb', line 13 def find_parent return unless parent_present? parent_scope = parent_class parent_scope = parent_class.friendly if parent_friendly_finders? instance_variable_set("@#{parent_var_name}", parent_scope.find(params[parent_id_param])) self.class.send(:decorates_assigned, parent_var_name.to_sym) if defined?(Draper) rescue ActiveRecord::RecordNotFound => e # Only catch and render 404 if we're in a request context # Check if we have a request object (indicates we're in a request context) raise e unless respond_to?(:request, true) && !request.nil? render_not_found nil # Explicitly stop execution to prevent further action execution end |
#parent_class ⇒ Object
41 42 43 |
# File 'lib/crudable/rails/nestable.rb', line 41 def parent_class @parent_class ||= parent_var_name.classify.constantize end |
#parent_friendly_finders? ⇒ Boolean
By default, if friendly finders are enabled for the resource, we will also attempt to use friendly finders for the parent (when available).
Override this in your controller if you want to force parent lookup to use (or not use) FriendlyId, independently of the resource.
73 74 75 76 77 78 |
# File 'lib/crudable/rails/nestable.rb', line 73 def parent_friendly_finders? return false unless defined?(FriendlyId) return false unless parent_class.respond_to?(:friendly) friendly_finders? end |
#parent_id_param ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/crudable/rails/nestable.rb', line 29 def parent_id_param return nil unless use_parent_as_scope? # Prefer route/path params (ordered by the route definition) over merged params # (which may include query params). # If multiple *_id params exist (multi-level nesting), default to the deepest/last one. @parent_id_param ||= begin keys = parent_id_param_keys keys.map(&:to_s).grep(/(.+)_id$/).last end end |
#parent_id_param_keys ⇒ Object
84 85 86 87 88 |
# File 'lib/crudable/rails/nestable.rb', line 84 def parent_id_param_keys return request.path_parameters.keys if request_path_parameters_present? params&.keys || [] end |
#parent_present? ⇒ Boolean
49 50 51 |
# File 'lib/crudable/rails/nestable.rb', line 49 def parent_present? !parent_id_param.nil? end |
#parent_resource_association ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/crudable/rails/nestable.rb', line 53 def parent_resource_association return unless parent_present? # Handle case where resource_class is a CollectionProxy (association proxy) # Extract the actual class for comparison actual_resource_class = if resource_class.is_a?(ActiveRecord::Associations::CollectionProxy) resource_class.klass else resource_class end association = parent_class.reflect_on_all_associations.find { |assoc| assoc.klass == actual_resource_class } association&.name end |
#parent_var_name ⇒ Object
45 46 47 |
# File 'lib/crudable/rails/nestable.rb', line 45 def parent_var_name @parent_var_name ||= parent_id_param.sub(/_id$/, '').underscore end |
#request_path_parameters_present? ⇒ Boolean
90 91 92 |
# File 'lib/crudable/rails/nestable.rb', line 90 def request_path_parameters_present? request.respond_to?(:path_parameters) && request.path_parameters.present? end |
#use_parent_as_scope? ⇒ Boolean
80 81 82 |
# File 'lib/crudable/rails/nestable.rb', line 80 def use_parent_as_scope? true end |