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.



7
8
9
10
11
12
# File 'lib/activerecord_nested_scope/node.rb', line 7

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.



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

def klass
  @klass
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parentObject

Returns the value of attribute parent.



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

def parent
  @parent
end

#source_typeObject

Returns the value of attribute source_type.



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

def source_type
  @source_type
end

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


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

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

#childrenObject



44
45
46
# File 'lib/activerecord_nested_scope/node.rb', line 44

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

#has_many?Boolean

Returns:

  • (Boolean)


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

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

#has_options?Boolean

Returns:

  • (Boolean)


14
15
16
17
# File 'lib/activerecord_nested_scope/node.rb', line 14

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

#has_scope?Boolean

Returns:

  • (Boolean)


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

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

#leaf?Boolean

Returns:

  • (Boolean)


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

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

#options(key) ⇒ Object



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

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

#polymorphic_belongs_to?Boolean

Returns:

  • (Boolean)


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

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

#reflectionObject



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

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

#scopeObject



74
75
76
# File 'lib/activerecord_nested_scope/node.rb', line 74

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

#search_childrenObject



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

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)


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

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