Class: ActiveRecordNestedScope::Node
- Inherits:
-
Object
- Object
- ActiveRecordNestedScope::Node
- Defined in:
- lib/activerecord_nested_scope/node.rb
Defined Under Namespace
Classes: PolymorphicType
Instance Attribute Summary collapse
-
#klass ⇒ Object
Returns the value of attribute klass.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#source_type ⇒ Object
Returns the value of attribute source_type.
Instance Method Summary collapse
- #belongs_to? ⇒ Boolean
- #children ⇒ Object
- #has_many? ⇒ Boolean
- #has_options? ⇒ Boolean
- #has_scope? ⇒ Boolean
-
#initialize(klass, name, parent = nil, source_type = nil) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #options(key) ⇒ Object
- #polymorphic_belongs_to? ⇒ Boolean
- #reflection ⇒ Object
- #scope ⇒ Object
- #search_children ⇒ Object
- #valid? ⇒ Boolean
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
#klass ⇒ Object
Returns the value of attribute klass.
5 6 7 |
# File 'lib/activerecord_nested_scope/node.rb', line 5 def klass @klass end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/activerecord_nested_scope/node.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/activerecord_nested_scope/node.rb', line 5 def parent @parent end |
#source_type ⇒ Object
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
36 37 38 |
# File 'lib/activerecord_nested_scope/node.rb', line 36 def belongs_to? reflection.class.name == 'ActiveRecord::Reflection::BelongsToReflection' && !reflection.polymorphic? end |
#children ⇒ Object
44 45 46 |
# File 'lib/activerecord_nested_scope/node.rb', line 44 def children @children ||= search_children.select(&:valid?) end |
#has_many? ⇒ 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
14 15 16 17 |
# File 'lib/activerecord_nested_scope/node.rb', line 14 def = @klass. && [@name] end |
#has_scope? ⇒ Boolean
70 71 72 |
# File 'lib/activerecord_nested_scope/node.rb', line 70 def has_scope? @parent && @parent.reflection.scope.present? end |
#leaf? ⇒ Boolean
28 29 30 |
# File 'lib/activerecord_nested_scope/node.rb', line 28 def leaf? (:through).blank? end |
#options(key) ⇒ Object
19 20 21 22 |
# File 'lib/activerecord_nested_scope/node.rb', line 19 def (key) = @klass..to_h .dig(@name, key) end |
#polymorphic_belongs_to? ⇒ 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 |
#reflection ⇒ Object
24 25 26 |
# File 'lib/activerecord_nested_scope/node.rb', line 24 def reflection @klass.reflect_on_association((:through)) end |
#scope ⇒ Object
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_children ⇒ Object
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
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/activerecord_nested_scope/node.rb', line 59 def valid? return false unless if (:through) && !reflection STDERR.puts "can't find reflection for #{(:through)} in #{klass}" return false end return true end |