Class: ActiveRecordNestedScope::Node
- Inherits:
-
Object
- Object
- ActiveRecordNestedScope::Node
- Defined in:
- lib/activerecord_nested_scope/node.rb
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.
Instance Method Summary collapse
- #belongs_to? ⇒ Boolean
- #children ⇒ Object
- #has_many? ⇒ Boolean
- #has_options? ⇒ Boolean
- #has_scope? ⇒ Boolean
-
#initialize(klass, name, parent = 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) ⇒ 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
#klass ⇒ Object
Returns the value of attribute klass.
3 4 5 |
# File 'lib/activerecord_nested_scope/node.rb', line 3 def klass @klass end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/activerecord_nested_scope/node.rb', line 3 def name @name end |
#parent ⇒ Object
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
33 34 35 |
# File 'lib/activerecord_nested_scope/node.rb', line 33 def belongs_to? reflection.class.name == 'ActiveRecord::Reflection::BelongsToReflection' && !reflection.polymorphic? end |
#children ⇒ Object
41 42 43 |
# File 'lib/activerecord_nested_scope/node.rb', line 41 def children @children ||= search_children.select(&:valid?) end |
#has_many? ⇒ 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
11 12 13 14 |
# File 'lib/activerecord_nested_scope/node.rb', line 11 def = @klass. && [@name] end |
#has_scope? ⇒ Boolean
66 67 68 |
# File 'lib/activerecord_nested_scope/node.rb', line 66 def has_scope? @parent && @parent.reflection.scope.present? end |
#leaf? ⇒ Boolean
25 26 27 |
# File 'lib/activerecord_nested_scope/node.rb', line 25 def leaf? (:through).blank? end |
#options(key) ⇒ Object
16 17 18 19 |
# File 'lib/activerecord_nested_scope/node.rb', line 16 def (key) = @klass..to_h .dig(@name, key) end |
#polymorphic_belongs_to? ⇒ 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 |
#reflection ⇒ Object
21 22 23 |
# File 'lib/activerecord_nested_scope/node.rb', line 21 def reflection @klass.reflect_on_association((:through)) end |
#scope ⇒ Object
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_children ⇒ Object
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
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/activerecord_nested_scope/node.rb', line 55 def valid? return false unless if (:through) && !reflection STDERR.puts "can't find reflection for #{options(:through)} in #{klass}" return false end return true end |