Module: ActiveRecord::QueryMethods

Defined in:
lib/postgres_with/active_record/relation/query_methods.rb

Defined Under Namespace

Classes: WithChain

Instance Method Summary collapse

Instance Method Details

#build_arel_from_array(array) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 127

def build_arel_from_array(array)
  unless array.map(&:class).uniq == [Arel::Nodes::As]
    raise ArgumentError, "Unsupported argument type: #{array} #{array.class}"
  end

  array
end

#build_arel_from_hash(with_value) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 106

def build_arel_from_hash(with_value)
  with_value.map do |name, expression|
    select = case expression
             when String
               Arel::Nodes::SqlLiteral.new("(#{expression})")
             when ActiveRecord::Relation
               expression.arel
             when Arel::SelectManager
               expression
             end
    if name.to_s.start_with?('_materialized_')
      name = name.gsub('_materialized_', '')
      table = Arel::Table.new(name)
      Arel::Nodes::AsMaterialized.new(table, select)
    else
      table = Arel::Table.new(name)
      Arel::Nodes::As.new(table, select)
    end
  end
end

#build_arel_with_extensions(aliases = nil) ⇒ Object Also known as: build_arel



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 78

def build_arel_with_extensions(aliases = nil)
  arel = build_arel_without_extensions(aliases)

  with_statements = with_values.flat_map do |with_value|
    case with_value
    when String
      Arel::Nodes::SqlLiteral.new(with_value)
    when Hash
      build_arel_from_hash(with_value)
    when Arel::Nodes::As
      with_value
    when Array
      build_arel_from_array(with_value)
    else
      raise ArgumentError, "Unsupported argument type: #{with_value} #{with_value.class}"
    end
  end
  unless with_statements.empty?
    if recursive_value
      arel.with :recursive, with_statements
    else
      arel.with with_statements
    end
  end

  arel
end

#with(opts = :chain, *rest) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 59

def with(opts = :chain, *rest)
  if opts == :chain
    WithChain.new(spawn)
  elsif opts.blank?
    self
  else
    spawn.with!(opts, *rest)
  end
end

#with!(opts = :chain, *rest) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
# File 'lib/postgres_with/active_record/relation/query_methods.rb', line 69

def with!(opts = :chain, *rest) # :nodoc:
  if opts == :chain
    WithChain.new(self)
  else
    self.with_values += [opts] + rest
    self
  end
end