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



139
140
141
142
143
144
145
146
147
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 139

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



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 178

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 149

def build_with(arel)
  visitor = arel.engine.connection.visitor
  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::SqlLiteral.new "(#{expression})"
        when ActiveRecord::Relation
          select = Arel::SqlLiteral.new "(#{expression.to_sql})"
        when Arel::SelectManager
          select = Arel::SqlLiteral.new visitor.accept(expression)
        end
        as = Arel::Nodes::As.new Arel::SqlLiteral.new(name.to_s), select
      end
    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



130
131
132
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 130

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

#ranked!(value) ⇒ Object



134
135
136
137
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 134

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

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



111
112
113
114
115
116
117
118
119
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 111

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:



121
122
123
124
125
126
127
128
# File 'lib/postgres_ext/active_record/relation/query_methods.rb', line 121

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