Class: ActiveRecordNestedScope::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_nested_scope/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, name, parent = nil) ⇒ Node

Returns a new instance of Node.



5
6
7
8
9
# File 'lib/activerecord_nested_scope/node.rb', line 5

def initialize(klass, name, parent = nil)
  @klass = klass
  @name = name
  @parent = parent
end

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



3
4
5
# File 'lib/activerecord_nested_scope/node.rb', line 3

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/activerecord_nested_scope/node.rb', line 3

def name
  @name
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/activerecord_nested_scope/node.rb', line 3

def parent
  @parent
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/activerecord_nested_scope/node.rb', line 33

def belongs_to?
  reflection.class.name == 'ActiveRecord::Reflection::BelongsToReflection' && !reflection.polymorphic?
end

#childrenObject



41
42
43
# File 'lib/activerecord_nested_scope/node.rb', line 41

def children
  @children ||= search_children.select(&:valid?)
end

#has_many?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/activerecord_nested_scope/node.rb', line 29

def has_many?
  reflection.class.name.in?(['ActiveRecord::Reflection::HasManyReflection', 'ActiveRecord::Reflection::HasOneReflection'])
end

#has_options?Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/activerecord_nested_scope/node.rb', line 11

def has_options?
  options = @klass.nested_scope_options
  options && options[@name]
end

#has_scope?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/activerecord_nested_scope/node.rb', line 66

def has_scope?
  @parent && @parent.reflection.scope.present?
end

#leaf?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/activerecord_nested_scope/node.rb', line 25

def leaf?
  options(:through).blank?
end

#options(key) ⇒ Object



16
17
18
19
# File 'lib/activerecord_nested_scope/node.rb', line 16

def options(key)
  options = @klass.nested_scope_options.to_h
  options.dig(@name, key)
end

#polymorphic_belongs_to?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/activerecord_nested_scope/node.rb', line 37

def polymorphic_belongs_to?
  reflection.class.name == 'ActiveRecord::Reflection::BelongsToReflection' && reflection.polymorphic?
end

#reflectionObject



21
22
23
# File 'lib/activerecord_nested_scope/node.rb', line 21

def reflection
  @klass.reflect_on_association(options(:through))
end

#scopeObject



70
71
72
# File 'lib/activerecord_nested_scope/node.rb', line 70

def scope
  @klass.all.instance_eval(&@parent.reflection.scope) if has_scope?
end

#search_childrenObject



45
46
47
48
49
50
51
52
53
# File 'lib/activerecord_nested_scope/node.rb', line 45

def search_children
  if leaf?
    []
  elsif polymorphic_belongs_to?
    polymorphic_klasses.map { |klass| Node.new(klass, @name, self) }
  else
    [Node.new(reflection.klass, @name, self)]
  end
end

#valid?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
# File 'lib/activerecord_nested_scope/node.rb', line 55

def valid?
  return false unless has_options?

  if options(:through) && !reflection
    STDERR.puts "can't find reflection for #{options(:through)} in #{klass}"
    return false
  end

  return true
end