Class: ActionBlocks::FieldsEngine
- Inherits:
-
Object
- Object
- ActionBlocks::FieldsEngine
- Defined in:
- lib/action_blocks/data_engine/fields_engine.rb
Overview
Data Engine
Instance Attribute Summary collapse
-
#joins ⇒ Object
Returns the value of attribute joins.
-
#root_key ⇒ Object
Returns the value of attribute root_key.
-
#root_klass ⇒ Object
Returns the value of attribute root_klass.
-
#select_reqs ⇒ Object
Returns the value of attribute select_reqs.
-
#selects ⇒ Object
Returns the value of attribute selects.
-
#tables ⇒ Object
Returns the value of attribute tables.
-
#wheres ⇒ Object
Returns the value of attribute wheres.
Instance Method Summary collapse
- #create_table_and_joins(klass, node, key, parent_key, join_prefix = nil, associations = nil) ⇒ Object
- #froms ⇒ Object
-
#initialize(root_klass, user: nil, table_alias_prefix:, select_reqs: []) ⇒ FieldsEngine
constructor
A new instance of FieldsEngine.
- #ordered_joins ⇒ Object
- #params_to_arel(aggregate_params) ⇒ Object
- #process ⇒ Object
- #query ⇒ Object
- #walk_colpath(klass, node, parent_key, col_path, colname, function) ⇒ Object
Constructor Details
#initialize(root_klass, user: nil, table_alias_prefix:, select_reqs: []) ⇒ FieldsEngine
Returns a new instance of FieldsEngine.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 7 def initialize(root_klass, user: nil, table_alias_prefix:, select_reqs: []) @root_klass = root_klass @user = user @table_alias_prefix = table_alias_prefix @select_reqs = select_reqs @tables = {} @selects = [] @joins = {} @wheres = [] end |
Instance Attribute Details
#joins ⇒ Object
Returns the value of attribute joins.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def joins @joins end |
#root_key ⇒ Object
Returns the value of attribute root_key.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def root_key @root_key end |
#root_klass ⇒ Object
Returns the value of attribute root_klass.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def root_klass @root_klass end |
#select_reqs ⇒ Object
Returns the value of attribute select_reqs.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def select_reqs @select_reqs end |
#selects ⇒ Object
Returns the value of attribute selects.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def selects @selects end |
#tables ⇒ Object
Returns the value of attribute tables.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def tables @tables end |
#wheres ⇒ Object
Returns the value of attribute wheres.
4 5 6 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 4 def wheres @wheres end |
Instance Method Details
#create_table_and_joins(klass, node, key, parent_key, join_prefix = nil, associations = nil) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 61 def create_table_and_joins(klass, node, key, parent_key, join_prefix = nil, associations = nil) # Create Arel Table Alias relation = klass.reflections[node.to_s] if node.is_a? Symbol unless @tables[key] @tables[key] = (relation ? relation.klass : node).arel_table.alias(key) unless @tables[key] # Create Join fk = associations ? associations[parent_key.to_s][:foreign_key] : relation.join_foreign_key pk = associations ? associations[parent_key.to_s][:primary_key] : relation.join_primary_key join_on = @tables[key].create_on(@tables[parent_key][fk].eq(@tables[key][pk])) @joins[join_prefix ? [join_prefix, node.to_s.underscore].compact.join('_').to_sym : key] = @tables[parent_key].create_join(@tables[key], join_on, Arel::Nodes::OuterJoin) end relation ? relation.klass : node end |
#froms ⇒ Object
85 86 87 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 85 def froms @root_klass.arel_table.alias([@table_alias_prefix, @root_klass.to_s.underscore.pluralize].compact.join('_')) end |
#ordered_joins ⇒ Object
81 82 83 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 81 def ordered_joins @joins.values end |
#params_to_arel(aggregate_params) ⇒ Object
57 58 59 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 57 def params_to_arel(aggregate_params) aggregate_params.map { |param| param.is_a?(String) ? Arel::Nodes.build_quoted(param) : param } if aggregate_params end |
#process ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 18 def process root_table = @root_klass.arel_table.alias([@table_alias_prefix, @root_klass.to_s.underscore.pluralize].compact.join('_')) @root_key = [@table_alias_prefix, @root_klass.to_s.underscore.pluralize].compact.join('_').to_sym # Add base table to tables @tables[@root_key.to_sym] = root_table # Add needed relations to tables @select_reqs.each do |selectreq| # binding.pry colname = selectreq[:field_name] colpath = selectreq[:path] function = selectreq[:function] node, *rest = colpath walk_colpath(@root_klass, node, @root_key, rest, colname, function) end end |
#query ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 93 def query @root_klass .from(froms) .select(selects) .joins(ordered_joins) .where(wheres.compact.reduce(&:and)) end |
#walk_colpath(klass, node, parent_key, col_path, colname, function) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/action_blocks/data_engine/fields_engine.rb', line 36 def walk_colpath(klass, node, parent_key, col_path, colname, function) key = [@table_alias_prefix, parent_key, node].compact.join('_').to_sym if !col_path.empty? next_klass = create_table_and_joins(klass, node, key, parent_key) # Recurse next_node, *rest = col_path walk_colpath(next_klass, next_node, key, rest, colname, function) else # Create Arel Select select = if function.nil? @tables[parent_key][node.to_sym].as(colname.to_s) else DatabaseFunctions.new.instance_exec(@tables[parent_key][node.to_sym], @user, &function).as(colname.to_s) end @selects << select end end |