Module: DutyFree::Util
- Defined in:
- lib/duty_free/util.rb
Class Method Summary collapse
- ._clean_name(name, import_template_as) ⇒ Object
- ._prefix_join(prefixes, separator = nil) ⇒ Object
-
._recurse_arel(piece, prefix = '') ⇒ Object
def self._parse(arel) arel = arel.arel if arel.is_a?(ActiveRecord::Relation) sels = arel.ast.cores.select { |x| x.is_a?(Arel::Nodes::SelectCore) } # source (joinsource) / projections (6) is the most interesting here sels.each_with_index do |sel, idx| puts “#idx =============” # Columns: sel.projections.map do |x| case when x.is_a?(Arel::Nodes::SqlLiteral) puts x.to_s else puts “#xx.class #xx.name” end end end nil end.
Class Method Details
._clean_name(name, import_template_as) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/duty_free/util.rb', line 74 def self._clean_name(name, import_template_as) return name if name.is_a?(Symbol) # Expand aliases (import_template_as || []).each do |k, v| if (k[-1] == ' ' && name.start_with?(k)) || name == k name.replace(v + name[k.length..-1]) break end end name end |
._prefix_join(prefixes, separator = nil) ⇒ Object
70 71 72 |
# File 'lib/duty_free/util.rb', line 70 def self._prefix_join(prefixes, separator = nil) prefixes.reject(&:blank?).join(separator || '.') end |
._recurse_arel(piece, prefix = '') ⇒ Object
def self._parse(arel)
arel = arel.arel if arel.is_a?(ActiveRecord::Relation)
sels = arel.ast.cores.select { |x| x.is_a?(Arel::Nodes::SelectCore) }
# source (joinsource) / projections (6) is the most interesting here
sels.each_with_index do |sel, idx|
puts "#{idx} ============="
# Columns:
sel.projections.map do |x|
case
when x.is_a?(Arel::Nodes::SqlLiteral)
puts x.to_s
else
puts "#{x.class} #{x.name}"
end
end
end
nil
end
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/duty_free/util.rb', line 25 def self._recurse_arel(piece, prefix = '') names = [] # Our JOINs mashup of nested arrays and hashes if piece.is_a?(Array) names += piece.inject([]) { |s, v| s + _recurse_arel(v, prefix) } elsif piece.is_a?(Hash) names += piece.inject([]) do |s, v| new_prefix = "#{prefix}#{v.first}_" s << new_prefix s + _recurse_arel(v.last, new_prefix) end # ActiveRecord AREL objects elsif piece.is_a?(Arel::Nodes::Join) # INNER or OUTER JOIN # rubocop:disable Style/IdenticalConditionalBranches if piece.right.is_a?(Arel::Table) # Came in from AR < 3.2? # Arel 2.x and older is a little curious because these JOINs work "back to front". # The left side here is either another earlier JOIN, or at the end of the whole tree, it is # the first table. names += _recurse_arel(piece.left) # The right side here at the top is the very last table, and anywhere else down the tree it is # the later "JOIN" table of this pair. (The table that comes after all the rest of the JOINs # from the left side.) names << (piece.right.table_alias || piece.right.name) else # "Normal" setup, fed from a JoinSource which has an array of JOINs # The left side is the "JOIN" table names += _recurse_arel(piece.left) # (The right side of these is the "ON" clause) end # rubocop:enable Style/IdenticalConditionalBranches elsif piece.is_a?(Arel::Table) # Table names << (piece.table_alias || piece.name) elsif piece.is_a?(Arel::Nodes::TableAlias) # Alias # Can get the real table name from: self._recurse_arel(piece.left) names << piece.right.to_s # This is simply a string; the alias name itself elsif piece.is_a?(Arel::Nodes::JoinSource) # Leaving this until the end because AR < 3.2 doesn't know at all about JoinSource! # The left side is the "FROM" table # names += _recurse_arel(piece.left) names << (piece.left.table_alias || piece.left.name) # The right side is an array of all JOINs names += piece.right.inject([]) { |s, v| s + _recurse_arel(v) } end names end |