Module: ClosureTree::ArelHelpers
- Included in:
- Support
- Defined in:
- lib/closure_tree/arel_helpers.rb
Instance Method Summary collapse
-
#aliased_table(table, alias_name) ⇒ Object
Helper to create an Arel node for a table with an alias.
- #build_hierarchy_delete_query(hierarchy_table, id) ⇒ Object
-
#build_hierarchy_insert_query(hierarchy_table, node_id, parent_id) ⇒ Object
Build Arel queries for hierarchy operations.
-
#hierarchy_table ⇒ Object
Get hierarchy table using the model_class This is for Support class methods.
-
#hierarchy_table_for(model) ⇒ Object
Get hierarchy table from a model class This method should be called from instance methods where hierarchy_class is available.
-
#model_table ⇒ Object
Get model’s arel table.
-
#to_sql_with_connection(arel_manager) ⇒ Object
Convert an Arel AST to SQL using the correct connection’s visitor This ensures proper quoting for the specific database adapter (MySQL uses backticks, PostgreSQL uses double quotes).
Instance Method Details
#aliased_table(table, alias_name) ⇒ Object
Helper to create an Arel node for a table with an alias
32 33 34 |
# File 'lib/closure_tree/arel_helpers.rb', line 32 def aliased_table(table, alias_name) table.alias(alias_name) end |
#build_hierarchy_delete_query(hierarchy_table, id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/closure_tree/arel_helpers.rb', line 60 def build_hierarchy_delete_query(hierarchy_table, id) # Build the innermost subquery inner_subquery_manager = Arel::SelectManager.new(hierarchy_table) inner_subquery_manager.project(hierarchy_table[:descendant_id]) inner_subquery_manager.where( hierarchy_table[:ancestor_id].eq(id) .or(hierarchy_table[:descendant_id].eq(id)) ) inner_subquery = inner_subquery_manager.as('x') # Build the middle subquery with DISTINCT middle_subquery = Arel::SelectManager.new middle_subquery.from(inner_subquery) middle_subquery.project(inner_subquery[:descendant_id]).distinct # Build the DELETE statement delete_manager = Arel::DeleteManager.new delete_manager.from(hierarchy_table) delete_manager.where(hierarchy_table[:descendant_id].in(middle_subquery)) delete_manager end |
#build_hierarchy_insert_query(hierarchy_table, node_id, parent_id) ⇒ Object
Build Arel queries for hierarchy operations
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/closure_tree/arel_helpers.rb', line 37 def build_hierarchy_insert_query(hierarchy_table, node_id, parent_id) x = aliased_table(hierarchy_table, 'x') # Build the SELECT subquery - use SelectManager select_query = Arel::SelectManager.new(x) select_query.project( x[:ancestor_id], Arel.sql(quote(node_id)), x[:generations] + 1 ) select_query.where(x[:descendant_id].eq(parent_id)) # Build the INSERT statement insert_manager = Arel::InsertManager.new insert_manager.into(hierarchy_table) insert_manager.columns << hierarchy_table[:ancestor_id] insert_manager.columns << hierarchy_table[:descendant_id] insert_manager.columns << hierarchy_table[:generations] insert_manager.select(select_query) insert_manager end |
#hierarchy_table ⇒ Object
Get hierarchy table using the model_class This is for Support class methods
24 25 26 27 28 29 |
# File 'lib/closure_tree/arel_helpers.rb', line 24 def hierarchy_table @hierarchy_table ||= begin hierarchy_class_name = [:hierarchy_class_name] || "#{model_class}Hierarchy" hierarchy_class_name.constantize.arel_table end end |
#hierarchy_table_for(model) ⇒ Object
Get hierarchy table from a model class This method should be called from instance methods where hierarchy_class is available
12 13 14 15 16 17 18 19 20 |
# File 'lib/closure_tree/arel_helpers.rb', line 12 def hierarchy_table_for(model) if model.respond_to?(:hierarchy_class) model.hierarchy_class.arel_table elsif model.class.respond_to?(:hierarchy_class) model.class.hierarchy_class.arel_table else raise ArgumentError, "Cannot find hierarchy_class for #{model}" end end |
#model_table ⇒ Object
Get model’s arel table
6 7 8 |
# File 'lib/closure_tree/arel_helpers.rb', line 6 def model_table @model_table ||= model_class.arel_table end |
#to_sql_with_connection(arel_manager) ⇒ Object
Convert an Arel AST to SQL using the correct connection’s visitor This ensures proper quoting for the specific database adapter (MySQL uses backticks, PostgreSQL uses double quotes)
85 86 87 88 89 |
# File 'lib/closure_tree/arel_helpers.rb', line 85 def to_sql_with_connection(arel_manager) collector = Arel::Collectors::SQLString.new visitor = connection.visitor visitor.accept(arel_manager.ast, collector).value end |