Module: Traits::Model::STI
- Included in:
- Traits::Model
- Defined in:
- lib/traits/concerns/model/sti.rb
Instance Method Summary collapse
- #sti_attribute_name ⇒ Object
- #sti_base? ⇒ Boolean
- #sti_chain ⇒ Object
- #sti_derived? ⇒ Boolean
- #to_hash ⇒ Object
- #uses_sti? ⇒ Boolean
Instance Method Details
#sti_attribute_name ⇒ Object
18 19 20 |
# File 'lib/traits/concerns/model/sti.rb', line 18 def sti_attribute_name model_class.inheritance_column.to_sym end |
#sti_base? ⇒ Boolean
8 9 10 11 12 |
# File 'lib/traits/concerns/model/sti.rb', line 8 def sti_base? model_class.descends_from_active_record? && model_class.subclasses.any? do |subclass| subclass.superclass == model_class end end |
#sti_chain ⇒ Object
22 23 24 25 26 27 28 29 30 |
# File 'lib/traits/concerns/model/sti.rb', line 22 def sti_chain model_class = self.model_class chain = [model_class] until model_class.superclass == ActiveRecord::Base do model_class = model_class.superclass chain.unshift(model_class) end chain end |
#sti_derived? ⇒ Boolean
14 15 16 |
# File 'lib/traits/concerns/model/sti.rb', line 14 def sti_derived? !model_class.descends_from_active_record? end |
#to_hash ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/traits/concerns/model/sti.rb', line 32 def to_hash super.merge!( sti_base: sti_base?, sti_derived: sti_derived?, sti_attribute_name: sti_attribute_name ) end |
#uses_sti? ⇒ Boolean
4 5 6 |
# File 'lib/traits/concerns/model/sti.rb', line 4 def uses_sti? sti_base? || sti_derived? end |