Module: ActsAsOrderedTree::Adapters::PostgreSQLAdapter
- Defined in:
- lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb
Instance Method Summary collapse
-
#ancestors ⇒ Object
Recursive ancestors fetcher.
- #descendants ⇒ Object
- #root ⇒ Object
-
#self_and_ancestors ⇒ Object
Recursive ancestors fetcher.
- #self_and_descendants ⇒ Object
Instance Method Details
#ancestors ⇒ Object
Recursive ancestors fetcher
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb', line 27 def ancestors query = <<-QUERY SELECT id, #{parent_column}, 1 AS _depth FROM #{self.class.quoted_table_name} WHERE #{arel[:id].eq(parent.try(:id)).to_sql} UNION ALL SELECT alias1.id, alias1.#{parent_column}, _depth + 1 FROM #{self.class.quoted_table_name} alias1 INNER JOIN ancestors ON alias1.id = ancestors.#{parent_column} QUERY recursive_scope.with_recursive("ancestors", query). order("ancestors._depth DESC") end |
#descendants ⇒ Object
61 62 63 |
# File 'lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb', line 61 def descendants self_and_descendants.where(arel[:id].not_eq(id)) end |
#root ⇒ Object
42 43 44 |
# File 'lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb', line 42 def root root? ? self : ancestors.first end |
#self_and_ancestors ⇒ Object
Recursive ancestors fetcher
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb', line 7 def self_and_ancestors if persisted? && !send("#{parent_column}_changed?") query = <<-QUERY SELECT id, #{parent_column}, 1 AS _depth FROM #{self.class.quoted_table_name} WHERE #{arel[:id].eq(id).to_sql} UNION ALL SELECT alias1.id, alias1.#{parent_column}, _depth + 1 FROM #{self.class.quoted_table_name} alias1 INNER JOIN self_and_ancestors ON alias1.id = self_and_ancestors.#{parent_column} QUERY recursive_scope.with_recursive("self_and_ancestors", query). order("self_and_ancestors._depth DESC") else ancestors + [self] end end |
#self_and_descendants ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/acts_as_ordered_tree/adapters/postgresql_adapter.rb', line 46 def self_and_descendants query = <<-QUERY SELECT id, #{parent_column}, ARRAY[#{position_column}] AS _positions FROM #{self.class.quoted_table_name} WHERE #{arel[:id].eq(id).to_sql} UNION ALL SELECT alias1.id, alias1.#{parent_column}, _positions || alias1.#{position_column} FROM descendants INNER JOIN #{self.class.quoted_table_name} alias1 ON alias1.parent_id = descendants.id QUERY recursive_scope.with_recursive("descendants", query). order("descendants._positions ASC") end |