Module: PolyBelongsTo::Pbt

Defined in:
lib/poly_belongs_to/pbt.rb

Overview

PolyBelongsTo::Pbt is where internal methods are for implementing core and duplication methods; available on all instances. They are available for other use if the need should arise.

Constant Summary collapse

AttrSanitizer =

Strips date and id fields dup'd attributes

Returns:

  • (Hash)

    attributes

lambda {|obj|
  return {} unless obj
  obj.dup.attributes.delete_if {|ky,vl|
    [:created_at, :updated_at, :deleted_at, obj.pbt_id_sym, obj.pbt_type_sym].include? ky.to_sym
  } 
}
BuildCmd =

Discerns which type of build command is used between obj and child

Returns:

  • (String, nil)

    build command for relation, or nil

lambda {|obj, child|
  return nil unless obj && child
  dup_name = CollectionProxy[obj, child]
  IsSingular[obj, child] ? "build_#{dup_name}" : IsPlural[obj, child] ? "#{dup_name}.build" : nil
}
Reflects =

Returns all has_one and has_many relationships as symbols (reflec_on_all_associations)

Returns:

  • (Array<Symbol>)
lambda {|obj|
  return [] unless obj
  [:has_one, :has_many].map { |has|
    obj.class.name.constantize.reflect_on_all_associations(has).map {|i| i.name.to_sym }
  }.flatten
}
ReflectsAsClasses =

Returns all has_one and has_many relationships as class objects (reflec_on_all_associations)

Returns:

  • (Array<Object>)
lambda {|obj|
  Reflects[obj].map {|ref|
    (obj.send(ref).try(:klass) || obj.send(ref).class).name.constantize
  }
}
IsReflected =

Check if the child object is a kind of child that belongs to obj

Returns:

  • (true, false)
lambda {|obj, child|
  !!CollectionProxy[obj, child]
}
SingularOrPlural =

Plurality of object relationship (plural for has_many)

Returns:

  • (Symbol, nil)

    :plural, :singular, or nil

lambda {|obj, child|
  return nil unless obj && child
  reflects = Reflects[obj]
  if reflects.include?(ActiveModel::Naming.singular(child).to_sym)
    :singular
  elsif CollectionProxy[obj, child]
    :plural
  else
    nil
  end
}
IsSingular =

Singularity of object relationship (for has_one)

Returns:

  • (true, false)
lambda {|obj, child|
  SingularOrPlural[obj, child] == :singular
}
IsPlural =

Plurality of object relationship (for has_many)

Returns:

  • (true, false)
lambda {|obj,child|
  SingularOrPlural[obj, child] == :plural
}
CollectionProxy =

Returns the symbol of the child relation object

Returns:

  • (Symbol)

    working relation on obj

lambda {|obj, child|
  return nil unless obj && child
  names = [ActiveModel::Naming.singular(child).to_s, ActiveModel::Naming.plural(child).to_s].uniq
  Reflects[obj].detect {|ref| "#{ref}"[/(?:#{ names.join('|') }).{,3}/]}
}
AsCollectionProxy =

Returns either a ActiveRecord::CollectionProxy or a PolyBelongsTo::FakedCollection as a proxy

Returns:

  • (Object)
lambda {|obj, child|
  return PolyBelongsTo::FakedCollection.new() unless obj && child
  return PolyBelongsTo::FakedCollection.new(obj, child) if IsSingular[obj, child]
  if CollectionProxy[obj, child]
    obj.send(PolyBelongsTo::Pbt::CollectionProxy[obj, child])
  else
    PolyBelongsTo::FakedCollection.new()
  end
}