module Rooftop::Coercions::ParentCoercion

Public Class Methods

included(base) click to toggle source
# File lib/rooftop/coercions/parent_coercion.rb, line 5
def self.included(base)
  base.extend ClassMethods
  # base.send(:after_find, ->(r) {
  #   if r.has_parent?
  #     r.instance_variable_set(:"parent_#{base.to_s.underscore}", resolve_parent_id())
  #     r.class.send(:attr_reader, :"parent_#{base.to_s.underscore}")
  #   end
  # })
  # base.send(:coerce_field, parent: ->(p) { base.send(:resolve_parent_id,p) })
end

Public Instance Methods

has_parent?() click to toggle source
# File lib/rooftop/coercions/parent_coercion.rb, line 24
def has_parent?
  respond_to?(:parent) && parent.is_a?(Fixnum) && parent != 0
end
resolve_parent_id() click to toggle source
# File lib/rooftop/coercions/parent_coercion.rb, line 28
def resolve_parent_id
  if respond_to?(:parent)
    if parent.is_a?(Fixnum)
      if parent == 0
        #no parent
        return nil
      else
        return self.class.send(:find, id)
      end
    else
      return parent
    end

  end

end