Sha256: bca0dc6e2a945203a89d2b9b8a1a46d6767c3e050af6e0e62796b55642c97e2f
Contents?: true
Size: 1.87 KB
Versions: 5
Compression:
Stored size: 1.87 KB
Contents
require_relative 'relation/distinct_on' require_relative 'relation/auxiliary_statement' module Torque module PostgreSQL module Relation include DistinctOn include AuxiliaryStatement # Resolve column definition up to second value. # For example, based on Post model: # # resolve_column(['name', :title]) # # Returns ['name', '"posts"."title"'] # # resolve_column([:title, {authors: :name}]) # # Returns ['"posts"."title"', '"authors"."name"'] # # resolve_column([{authors: [:name, :age]}]) # # Returns ['"authors"."name"', '"authors"."age"'] def resolve_column(list, base = false) base = resolve_base_table(base) list.map do |item| case item when String ::Arel::Nodes::SqlLiteral.new(klass.send(:sanitize_sql, item.to_s)) when Symbol base ? base.arel_attribute(item) : klass.arel_attribute(item) when Array resolve_column(item, base) when Hash raise ArgumentError, "Unsupported Hash for attributes on third level" if base item.map do |key, other_list| other_list = [other_list] unless other_list.kind_of? Enumerable resolve_column(other_list, key) end else raise ArgumentError, "Unsupported argument type: #{value} (#{value.class})" end end.flatten end # Get the TableMetadata from a relation def resolve_base_table(relation) return unless relation table = predicate_builder.send(:table) if table.associated_with?(relation) table.associated_table(relation).send(:klass) else raise ArgumentError, "Relation for #{relation} not found on #{klass}" end end end ActiveRecord::Relation.include Relation end end
Version data entries
5 entries across 5 versions & 1 rubygems