Class: ActiveRecordNestedScope::Node

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

Defined Under Namespace

Classes: PolymorphicType

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Node.



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

def initialize(klass, name, parent = nil, source_type = nil)
  @klass = klass
  @name = name
  @parent = parent
  @source_type = source_type
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

#source_typeObject

Returns the value of attribute source_type.



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

def source_type
  @source_type
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


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

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

#childrenObject



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

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

#has_many?Boolean

Returns:

  • (Boolean)


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

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

#has_options?Boolean

Returns:

  • (Boolean)


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

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

#has_scope?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/activerecord_nested_scope/node.rb', line 68

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

#leaf?Boolean

Returns:

  • (Boolean)


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

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

#options(key) ⇒ Object



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

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

#polymorphic_belongs_to?Boolean

Returns:

  • (Boolean)


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

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

#reflectionObject



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

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

#scopeObject



72
73
74
# File 'lib/activerecord_nested_scope/node.rb', line 72

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

#search_childrenObject



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

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

#valid?Boolean

Returns:

  • (Boolean)


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

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