Module: Consyncful::Tree::Parent
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/consyncful/tree/concerns/parent.rb
Instance Method Summary collapse
- #lookup_child_model_ids(context: self) ⇒ Object
- #lookup_child_model_ids_for_list(child_objects) ⇒ Object
-
#with_child_class_of?(klass) ⇒ Boolean
Returns true/false if the parent has a child model with a specific class.
Instance Method Details
#lookup_child_model_ids(context: self) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/consyncful/tree/concerns/parent.rb', line 20 def lookup_child_model_ids(context: self) context.relations.map do |key, val| child_ids = context[val.foreign_key] child_ids = Array.wrap(child_ids) next if key == "child_models" || child_ids.empty? child_objects = Consyncful::Base.where(id: { "$in": child_ids }) child_ids + lookup_child_model_ids_for_list(child_objects) end.flatten end |
#lookup_child_model_ids_for_list(child_objects) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/consyncful/tree/concerns/parent.rb', line 33 def lookup_child_model_ids_for_list(child_objects) return [] if child_objects.empty? child_objects.map do |obj| obj_is_parent = classes_with_parent_concern.include?(obj.class) lookup_child_model_ids(context: obj) unless obj_is_parent end.flatten end |
#with_child_class_of?(klass) ⇒ Boolean
Returns true/false if the parent has a child model with a specific class. This is useful if you want to trace a child’s class on a specific parent.
44 45 46 47 48 49 50 |
# File 'lib/consyncful/tree/concerns/parent.rb', line 44 def with_child_class_of?(klass) return false if klass.nil? klasses = child_models.map(&:class).uniq.map(&:to_s) klasses.include?(klass.to_s) end |