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
lambda do |obj| return {} unless obj obj.dup.attributes.delete_if do |ky, _| [:created_at, :updated_at, :deleted_at, obj.pbt_id_sym, obj.pbt_type_sym].include? ky.to_sym end end
- BuildCmd =
Discerns which type of build command is used between obj and child
lambda do |obj, child| return nil unless obj && child dup_name = CollectionProxy[obj, child] if IsSingular[obj, child] "build_#{dup_name}" elsif IsPlural[obj, child] "#{dup_name}.build" end end
- Reflects =
Returns all has_one and has_many relationships as symbols (reflect_on_all_associations)
lambda do |obj, habtm = false| return [] unless obj [:has_one, :has_many]. tap {|array| array << :has_and_belongs_to_many if habtm }. flat_map do |has| obj.class.name.constantize.reflect_on_all_associations(has).map(&:name) end end
- ReflectsAsClasses =
Returns all has_one and has_many relationships as class objects (reflect_on_all_associations)
lambda do |obj, habtm = false| Reflects[obj, habtm].map do |ref| (obj.send(ref).try(:klass) || obj.send(ref).class).name.constantize end end
- IsReflected =
Check if the child object is a kind of child that belongs to obj
lambda do |obj, child| !!CollectionProxy[obj, child] end
- SingularOrPlural =
Plurality of object relationship (plural for has_many)
lambda do |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 end end
- IsSingular =
Singularity of object relationship (for has_one)
lambda do |obj, child| SingularOrPlural[obj, child] == :singular end
- IsPlural =
Plurality of object relationship (for has_many)
lambda do |obj, child| SingularOrPlural[obj, child] == :plural end
- CollectionProxy =
Returns the symbol of the child relation object
lambda do |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}/]} end
- AsCollectionProxy =
Returns either a ActiveRecord::CollectionProxy or a PolyBelongsTo::FakedCollection as a proxy
lambda do |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 end