Module: NestedSelect::Relation
- Defined in:
- lib/nested_select/relation.rb
Instance Attribute Summary collapse
-
#nested_select_values ⇒ Object
Returns the value of attribute nested_select_values.
Instance Method Summary collapse
-
#preload_associations(records) ⇒ Object
this is copy of the original ActiveRecord::Relation#preload_associations method the only patching I' doing -- streaming down nested_select_values over the preloading trees.
- #select(*fields) ⇒ Object
Instance Attribute Details
#nested_select_values ⇒ Object
Returns the value of attribute nested_select_values.
8 9 10 |
# File 'lib/nested_select/relation.rb', line 8 def nested_select_values @nested_select_values end |
Instance Method Details
#preload_associations(records) ⇒ Object
this is copy of the original ActiveRecord::Relation#preload_associations method the only patching I' doing -- streaming down nested_select_values over the preloading trees
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/nested_select/relation.rb', line 30 def preload_associations(records) # :nodoc: preload = preload_values preload += includes_values unless eager_loading? scope = strict_loading_value ? StrictLoadingScope : nil preload.each do |associations| ActiveRecord::Associations::Preloader.new(records:, associations:, scope:) .tap do# <-- Patching code goes here # at this moment nested_select_values will have a structure of an array of one hash element, # with multiple keys matching those in associations, so we need to separate them per branch if nested_select_values.present? # associations will have a structure of either tree like hash or str/sym # Ex: includes(:relation1, relation2: :nested_relation) # associations will be either :relation1 or { relation2: :nested_relation } root_association_name = (associations.try(:keys) || associations.try(:to_sym) || associations) # nested_select_values, after deep_combine_elements # will have structure of an array of single element at this moment: # [{relation1: [columns..], relation2: [columns, nested_relation: [columns]] }] # so we need to populate nested_select_values over associations branch _1.apply_nested_select_values([nested_select_values.first.slice(*root_association_name)]) end end.call end end |
#select(*fields) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/nested_select/relation.rb', line 9 def select(*fields) # {user_profile: [:zip_code]} + {user_profile: [:bio]} -> { user_profile: [:zip_code, :bio] } @nested_select_values = [*@nested_select_values, *fields.grep(Hash)].deep_combine_elements # returning self means -- there was only nesting selection, # and we should not interfere with default selection fields.grep_v(Hash).present? ? super(*fields.grep_v(Hash)) : self end |