Module: ActiveRecord::QueryMethods

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

Defined Under Namespace

Classes: WhereChain, WithChain

Instance Method Summary collapse

Instance Method Details

#build_arel_with_extensionsObject



204
205
206
207
208
209
210
211
212
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 204

def build_arel_with_extensions
  arel = build_arel_without_extensions

  build_with(arel)

  build_rank(arel, rank_value) if rank_value

  arel
end

#build_rank(arel, rank_window_options) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 242

def build_rank(arel, rank_window_options)
  unless arel.projections.count == 1 && Arel::Nodes::Count === arel.projections.first
    rank_window = case rank_window_options
                  when :order
                    arel.orders
                  when Symbol
                    table[rank_window_options].asc
                  when Hash
                    rank_window_options.map { |field, dir| table[field].send(dir) }
                  else
                    Arel::Nodes::SqlLiteral.new "(#{rank_window_options})"
                  end

    unless rank_window.blank?
      rank_node = Arel::Nodes::SqlLiteral.new 'rank()'
      window = Arel::Nodes::Window.new
      if String === rank_window
        window = window.frame rank_window
      else
        window = window.order(rank_window)
      end
      over_node = Arel::Nodes::Over.new rank_node, window

      arel.project(over_node)
    end
  end
end

#build_with(arel) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 214

def build_with(arel)
  with_statements = with_values.flat_map do |with_value|
    case with_value
    when String
      with_value
    when Hash
      with_value.map  do |name, expression|
        case expression
        when String
          select = Arel::Nodes::SqlLiteral.new "(#{expression})"
        when ActiveRecord::Relation, Arel::SelectManager
          select = Arel::Nodes::SqlLiteral.new "(#{expression.to_sql})"
        end
        Arel::Nodes::As.new Arel::Nodes::SqlLiteral.new("\"#{name.to_s}\""), select
      end
    when Arel::Nodes::As
      with_value
    end
  end
  unless with_statements.empty?
    if recursive_value
      arel.with :recursive, with_statements
    else
      arel.with with_statements
    end
  end
end

#ranked(options = :order) ⇒ Object



195
196
197
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 195

def ranked(options = :order)
  spawn.ranked! options
end

#ranked!(value) ⇒ Object



199
200
201
202
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 199

def ranked!(value)
  self.rank_value = value
  self
end

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



176
177
178
179
180
181
182
183
184
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 176

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:



186
187
188
189
190
191
192
193
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 186

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