module PolyBelongsTo::Core

PolyBelongsTo::Core are the core set of methods included on all ActiveModel & ActiveRecord instances.

Public Class Methods

habtm_of() click to toggle source

@return [Array<Symbol>] has_many relations

# File lib/poly_belongs_to/core.rb, line 29
def self.habtm_of
  reflect_on_all_associations(:has_and_belongs_to_many).map(&:name)
end
has_many_of() click to toggle source

@return [Array<Symbol>] has_many relations

# File lib/poly_belongs_to/core.rb, line 24
def self.has_many_of
  reflect_on_all_associations(:has_many).map(&:name)
end
has_one_of() click to toggle source

@return [Array<Symbol>] has_one relations

# File lib/poly_belongs_to/core.rb, line 19
def self.has_one_of
  reflect_on_all_associations(:has_one).map(&:name)
end
pbt() click to toggle source

@return [Symbol, nil] first belongs_to relation

# File lib/poly_belongs_to/core.rb, line 9
def self.pbt
  reflect_on_all_associations(:belongs_to).first.try(:name)
end
pbt_id_sym() click to toggle source

The symbol as an id field for the first belongs_to relation @return [Symbol, nil] :`belongs_to_object`_id or nil

# File lib/poly_belongs_to/core.rb, line 52
def self.pbt_id_sym
  val = pbt
  val ? "#{val}_id".to_sym : nil
end
pbt_mistyped() click to toggle source

Returns records with invalid class names stored in polymorphic records @return [Array<Object>, nil] ActiveRecord mistyped objects

# File lib/poly_belongs_to/core.rb, line 96
def self.pbt_mistyped
  return nil unless poly?
  where(pbt_type_sym => pbt_mistypes)
end
pbt_mistypes() click to toggle source

Returns strings of the invalid class names stored in polymorphic records @return [Array<String>]

# File lib/poly_belongs_to/core.rb, line 72
def self.pbt_mistypes
  pbt_poly_types.select do |i|
    begin
      !i.constantize.respond_to?(:pluck)
    rescue
      true
    end
  end
end
pbt_orphans() click to toggle source

Return Array of current Class records that are orphaned from parents @return [Array<Object>, nil] ActiveRecord orphan objects

# File lib/poly_belongs_to/core.rb, line 103
def self.pbt_orphans
  return nil unless pbts.present?
  poly? ? _pbt_polymorphic_orphans : _pbt_nonpolymorphic_orphans
end
pbt_params_name(allow_as_nested = true) click to toggle source

Symbol for html form params @param allow_as_nested [true, false] Allow parameter name to be nested attribute symbol @return [Symbol] The symbol for the form in the view

# File lib/poly_belongs_to/core.rb, line 42
def self.pbt_params_name(allow_as_nested = true)
  if poly? && allow_as_nested
    "#{table_name}_attributes".to_sym
  else
    name.downcase.to_sym
  end
end
pbt_poly_types() click to toggle source

Returns an unique array of strings of every polymorphic record type @return [Array<String>]

# File lib/poly_belongs_to/core.rb, line 65
def self.pbt_poly_types
  return [] unless poly?
  uniq.pluck(pbt_type_sym)
end
pbt_type_sym() click to toggle source

The symbol as an sym field for the polymorphic belongs_to relation, or nil @return [Symbol, nil] :`belongs_to_object`_sym or nil

# File lib/poly_belongs_to/core.rb, line 59
def self.pbt_type_sym
  poly? ? "#{pbt}_type".to_sym : nil
end
pbt_valid_types() click to toggle source

Returns strings of the valid class names stored in polymorphic records @return [Array<String>]

# File lib/poly_belongs_to/core.rb, line 84
def self.pbt_valid_types
  pbt_poly_types.delete_if do |i|
    begin
      !i.constantize.respond_to?(:pluck)
    rescue
      true
    end
  end
end
pbts() click to toggle source

@return [Array<Symbol>] belongs_to relations

# File lib/poly_belongs_to/core.rb, line 14
def self.pbts
  reflect_on_all_associations(:belongs_to).map(&:name)
end
poly?() click to toggle source

Boolean reponse of current class being polymorphic @return [true, false]

# File lib/poly_belongs_to/core.rb, line 35
def self.poly?
  !!reflect_on_all_associations(:belongs_to).first.try {|i| i.options[:polymorphic] }
end

Private Class Methods

_pbt_nonpolymorphic_orphans() click to toggle source

Return Array of current Class nonpolymorphic records that are orphaned from parents @return [Array<Object>] ActiveRecord orphan objects

# File lib/poly_belongs_to/core.rb, line 122
def self._pbt_nonpolymorphic_orphans
  where(arel_table[pbt_id_sym].not_in(pbt.to_s.camelize.constantize.arel_table.project(:id)))
end
_pbt_polymorphic_orphans() click to toggle source

Return Array of current Class polymorphic records that are orphaned from parents @return [Array<Object>] ActiveRecord orphan objects

# File lib/poly_belongs_to/core.rb, line 111
def self._pbt_polymorphic_orphans
  accumulative = nil
  pbt_valid_types.each do |type|
    arel_part = arel_table[pbt_id_sym].not_in(type.constantize.arel_table.project(:id)).and(arel_table[pbt_type_sym].eq(type))
    accumulative = accumulative.present? ? accumulative.or(arel_part) : arel_part
  end
  where(accumulative)
end

Public Instance Methods

orphan?() click to toggle source

Return true or false on whether the record is orphaned @return [Boolean

# File lib/poly_belongs_to/core.rb, line 220
def orphan?
  pbts.present? && !pbt_parent.present?
end
pbt() click to toggle source

@return [Symbol, nil] first belongs_to relation

# File lib/poly_belongs_to/core.rb, line 132
def pbt
  self.class.pbt
end
pbt_id() click to toggle source

Value of parent id. nil if no parent @return [Integer, nil]

# File lib/poly_belongs_to/core.rb, line 149
def pbt_id
  val = pbt
  val ? send("#{val}_id") : nil
end
pbt_id_sym() click to toggle source

The symbol as an id field for the first belongs_to relation @return [Symbol, nil] :`belongs_to_object`_id or nil

# File lib/poly_belongs_to/core.rb, line 201
def pbt_id_sym
  self.class.pbt_id_sym
end
pbt_params_name(allow_as_nested = true) click to toggle source

Symbol for html form params @param allow_as_nested [true, false] Allow parameter name to be nested attribute symbol @return [Symbol] The symbol for the form in the view

# File lib/poly_belongs_to/core.rb, line 214
def pbt_params_name(allow_as_nested = true)
  self.class.pbt_params_name(allow_as_nested)
end
pbt_parent() click to toggle source

Get the parent relation. Polymorphic relations are prioritized first. @return [Object, nil] ActiveRecord object instasnce

# File lib/poly_belongs_to/core.rb, line 162
def pbt_parent
  val = pbt
  if val && !pbt_id.nil?
    if poly?
      "#{pbt_type}".constantize.where(id: pbt_id).first
    else
      "#{val}".camelize.constantize.where(id: pbt_id).first
    end
  end
end
pbt_parents() click to toggle source

All belongs_to parents as class objects. One if polymorphic. @return [Array<Object>] ActiveRecord classes of parent objects.

# File lib/poly_belongs_to/core.rb, line 189
def pbt_parents
  if poly?
    Array[pbt_parent].compact
  else
    self.class.pbts.map do |i|
      try{ "#{i}".camelize.constantize.where(id: send("#{i}_id")).first }
    end.compact
  end
end
pbt_top_parent() click to toggle source

Climb up each parent object in the hierarchy until the top is reached.

This has a no-repeat safety built in.  Polymorphic parents have priority.

@return [Object, nil] top parent ActiveRecord object instace

# File lib/poly_belongs_to/core.rb, line 176
def pbt_top_parent
  record = self
  return nil unless record.pbt_parent
  no_repeat = PolyBelongsTo::SingletonSet.new
  while !no_repeat.include?(record.pbt_parent) && !record.pbt_parent.nil?
    no_repeat.add?(record)
    record = record.pbt_parent
  end
  record
end
pbt_type() click to toggle source

Value of polymorphic relation type. nil if not polymorphic. @return [String, nil]

# File lib/poly_belongs_to/core.rb, line 156
def pbt_type
  poly? ? send("#{pbt}_type") : nil
end
pbt_type_sym() click to toggle source

The symbol as an sym field for the polymorphic belongs_to relation, or nil @return [Symbol, nil] :`belongs_to_object`_sym or nil

# File lib/poly_belongs_to/core.rb, line 207
def pbt_type_sym
  self.class.pbt_type_sym
end
pbts() click to toggle source

@return [Array<Symbol>] belongs_to relations

# File lib/poly_belongs_to/core.rb, line 137
def pbts
  self.class.pbts
end
poly?() click to toggle source

Boolean reponse of current class being polymorphic @return [true, false]

# File lib/poly_belongs_to/core.rb, line 143
def poly?
  self.class.poly?
end