Module: Rooftop::Coercions::ParentCoercion

Defined in:
lib/rooftop/coercions/parent_coercion.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# 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

Instance Method Details

#has_parent?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rooftop/coercions/parent_coercion.rb', line 24

def has_parent?
  respond_to?(:parent) && parent.is_a?(Fixnum) && parent != 0
end

#resolve_parent_idObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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