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
-
#orphan? ⇒ Boolean
Return true or false on whether the record is orphaned.
-
#pbt ⇒ Symbol?
First belongs_to relation.
-
#pbt_id ⇒ Integer?
Value of parent id.
-
#pbt_id_sym ⇒ Symbol?
The symbol as an id field for the first belongs_to relation.
-
#pbt_params_name(allow_as_nested = true) ⇒ Symbol
Symbol for html form params.
-
#pbt_parent ⇒ Object?
Get the parent relation.
-
#pbt_parents ⇒ Array<Object>
All belongs_to parents as class objects.
-
#pbt_top_parent ⇒ Object?
Climb up each parent object in the hierarchy until the top is reached.
-
#pbt_type ⇒ String?
Value of polymorphic relation type.
-
#pbt_type_sym ⇒ Symbol?
The symbol as an sym field for the polymorphic belongs_to relation, or nil.
-
#pbts ⇒ Array<Symbol>
Belongs_to relations.
-
#poly? ⇒ true, false
Boolean reponse of current class being polymorphic.
Instance Method Details
#orphan? ⇒ Boolean
Return true or false on whether the record is orphaned
220 221 222 |
# File 'lib/poly_belongs_to/core.rb', line 220 def orphan? pbts.present? && !pbt_parent.present? end |
#pbt ⇒ Symbol?
Returns first belongs_to relation.
132 133 134 |
# File 'lib/poly_belongs_to/core.rb', line 132 def pbt self.class.pbt end |
#pbt_id ⇒ Integer?
Value of parent id. nil if no parent
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_sym ⇒ Symbol?
The symbol as an id field for the first belongs_to relation
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
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_parent ⇒ Object?
Get the parent relation. Polymorphic relations are prioritized first.
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_parents ⇒ Array<Object>
All belongs_to parents as class objects. One if polymorphic.
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_parent ⇒ Object?
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.
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_type ⇒ String?
Value of polymorphic relation type. nil if not polymorphic.
156 157 158 |
# File 'lib/poly_belongs_to/core.rb', line 156 def pbt_type poly? ? send("#{pbt}_type") : nil end |
#pbt_type_sym ⇒ Symbol?
The symbol as an sym field for the polymorphic belongs_to relation, or nil
207 208 209 |
# File 'lib/poly_belongs_to/core.rb', line 207 def pbt_type_sym self.class.pbt_type_sym end |
#pbts ⇒ Array<Symbol>
Returns 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
143 144 145 |
# File 'lib/poly_belongs_to/core.rb', line 143 def poly? self.class.poly? end |