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.
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
#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 |
#source_type ⇒ Object
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
34 35 36 |
# File 'lib/activerecord_nested_scope/node.rb', line 34 def belongs_to? reflection.class.name == 'ActiveRecord::Reflection::BelongsToReflection' && !reflection.polymorphic? end |
#children ⇒ Object
42 43 44 |
# File 'lib/activerecord_nested_scope/node.rb', line 42 def children @children ||= search_children.select(&:valid?) end |
#has_many? ⇒ 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
12 13 14 15 |
# File 'lib/activerecord_nested_scope/node.rb', line 12 def = @klass. && [@name] end |
#has_scope? ⇒ Boolean
68 69 70 |
# File 'lib/activerecord_nested_scope/node.rb', line 68 def has_scope? @parent && @parent.reflection.scope.present? end |
#leaf? ⇒ Boolean
26 27 28 |
# File 'lib/activerecord_nested_scope/node.rb', line 26 def leaf? (:through).blank? end |
#options(key) ⇒ Object
17 18 19 20 |
# File 'lib/activerecord_nested_scope/node.rb', line 17 def (key) = @klass..to_h .dig(@name, key) end |
#polymorphic_belongs_to? ⇒ 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 |
#reflection ⇒ Object
22 23 24 |
# File 'lib/activerecord_nested_scope/node.rb', line 22 def reflection @klass.reflect_on_association((:through)) end |
#scope ⇒ Object
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_children ⇒ Object
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
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/activerecord_nested_scope/node.rb', line 57 def valid? return false unless if (:through) && !reflection STDERR.puts "can't find reflection for #{(:through)} in #{klass}" return false end return true end |