Module: CsvRowModel::Model::Children

Extended by:
ActiveSupport::Concern
Includes:
HiddenModule, InheritedClassVar
Included in:
CsvRowModel::Model
Defined in:
lib/csv_row_model/concerns/model/children.rb

Instance Method Summary collapse

Instance Method Details

#append_child(source, options = {}) ⇒ Model

Appends child to the parent and returns it

Returns:

  • (Model)

    return the child if it is valid, otherwise returns nil


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/csv_row_model/concerns/model/children.rb', line 23

def append_child(source, options={})
  return nil unless source
  self.class.has_many_relationships.each do |relation_name, child_class|
    child_row_model = child_class.new(source, options.reverse_merge(parent: self))
    if child_row_model.valid?
      public_send(relation_name) << child_row_model
      return child_row_model
    end
  end
  nil
end

#child?Boolean

Returns true, if the instance is a child

Returns:

  • (Boolean)

    returns true, if the instance is a child


16
17
18
# File 'lib/csv_row_model/concerns/model/children.rb', line 16

def child?
  !!parent
end

#children_public_send(method_name) ⇒ Array

Convenience method to return an array of calling public_send(method_name) on it's children

Returns:

  • (Array)

    results of public_send(method_name) in a flattened array


38
39
40
41
42
# File 'lib/csv_row_model/concerns/model/children.rb', line 38

def children_public_send(method_name)
  self.class.has_many_relationships.keys.map do |relation_name|
    public_send(relation_name).map(&method_name)
  end.flatten(1)
end

#deep_public_send(method_name) ⇒ Array

Convenience method to return an array of calling public_send(method_name) on itself and it's children

Returns:

  • (Array)

    results of public_send(method_name) in a flattened array


47
48
49
50
# File 'lib/csv_row_model/concerns/model/children.rb', line 47

def deep_public_send(method_name)
  result = [public_send(method_name)]
  result + children_public_send(method_name)
end