Module: ActiveRpc::Rpc::Concerns::Includable
- Extended by:
- ActiveSupport::Concern
- Included in:
- QueryBuilder
- Defined in:
- lib/active_rpc/rpc/concerns/includable.rb
Overview
The Includable concern provides methods for applying includes (eager loading) to ActiveRecord queries in gRPC controllers.
Instance Method Summary collapse
-
#apply_includes(query, params) ⇒ Object
Apply includes to a query.
-
#get_query_config(model_class) ⇒ Object
Get query configuration for a model.
Instance Method Details
#apply_includes(query, params) ⇒ Object
Apply includes to a query
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/active_rpc/rpc/concerns/includable.rb', line 19 def apply_includes(query, params) return query unless params.respond_to?(:include) && params.include.present? begin # Convert the array to symbols includes = params.include.map(&:to_sym) # Get query configuration query_config = get_query_config(query.model) includable_assocs = query_config[:includable] || [] # Filter out non-includable associations if we have defined includable associations filtered_includes = if includable_assocs.empty? includes else includes.select do |assoc| includable_assocs.include?(assoc) end end # Apply includes query.includes(filtered_includes) rescue StandardError => e Rails.logger.error("Error applying includes: #{e.}") # Return original query if includes fails query end end |
#get_query_config(model_class) ⇒ Object
Get query configuration for a model
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_rpc/rpc/concerns/includable.rb', line 49 def get_query_config(model_class) if model_class.respond_to?(:active_rpc_config) && model_class.active_rpc_config resource = model_class.active_rpc_config[:resource].to_s.underscore query_config_method = "#{resource}_query_config" return model_class.send(query_config_method) if model_class.respond_to?(query_config_method) end # Default configuration if none is defined { searchable: model_class.column_names, filterable: model_class.column_names, sortable: model_class.column_names, includable: model_class.reflect_on_all_associations.map(&:name) } end |