Module: PolyBelongsTo::Core

Extended by:
ActiveSupport::Concern
Defined in:
lib/poly_belongs_to/core.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#orphan?Boolean

Return true or false on whether the record is orphaned

Returns:

  • (Boolean)

    Boolean



220
221
222
# File 'lib/poly_belongs_to/core.rb', line 220

def orphan?
  pbts.present? && !pbt_parent.present?
end

#pbtSymbol?

Returns first belongs_to relation.

Returns:

  • (Symbol, nil)

    first belongs_to relation



132
133
134
# File 'lib/poly_belongs_to/core.rb', line 132

def pbt
  self.class.pbt
end

#pbt_idInteger?

Value of parent id. nil if no parent

Returns:

  • (Integer, nil)


149
150
151
152
# File 'lib/poly_belongs_to/core.rb', line 149

def pbt_id
  val = pbt
  val ? send("#{val}_id") : nil
end

#pbt_id_symSymbol?

The symbol as an id field for the first belongs_to relation

Returns:

  • (Symbol, nil)

    :‘belongs_to_object`_id or nil



201
202
203
# 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) ⇒ Symbol

Symbol for html form params

Parameters:

  • allow_as_nested (true, false) (defaults to: true)

    Allow parameter name to be nested attribute symbol

Returns:

  • (Symbol)

    The symbol for the form in the view



214
215
216
# 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_parentObject?

Get the parent relation. Polymorphic relations are prioritized first.

Returns:

  • (Object, nil)

    ActiveRecord object instasnce



162
163
164
165
166
167
168
169
170
171
# 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_parentsArray<Object>

All belongs_to parents as class objects. One if polymorphic.

Returns:

  • (Array<Object>)

    ActiveRecord classes of parent objects.



189
190
191
192
193
194
195
196
197
# 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_parentObject?

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.

Returns:

  • (Object, nil)

    top parent ActiveRecord object instace



176
177
178
179
180
181
182
183
184
185
# 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_typeString?

Value of polymorphic relation type. nil if not polymorphic.

Returns:

  • (String, nil)


156
157
158
# File 'lib/poly_belongs_to/core.rb', line 156

def pbt_type
  poly? ? send("#{pbt}_type") : nil
end

#pbt_type_symSymbol?

The symbol as an sym field for the polymorphic belongs_to relation, or nil

Returns:

  • (Symbol, nil)

    :‘belongs_to_object`_sym or nil



207
208
209
# File 'lib/poly_belongs_to/core.rb', line 207

def pbt_type_sym
  self.class.pbt_type_sym
end

#pbtsArray<Symbol>

Returns belongs_to relations.

Returns:

  • (Array<Symbol>)

    belongs_to relations



137
138
139
# File 'lib/poly_belongs_to/core.rb', line 137

def pbts
  self.class.pbts
end

#poly?true, false

Boolean reponse of current class being polymorphic

Returns:

  • (true, false)


143
144
145
# File 'lib/poly_belongs_to/core.rb', line 143

def poly?
  self.class.poly?
end