Class: ParamsReady::Query::RelationParameterBuilder

Inherits:
Builder show all
Includes:
GroupingLike, HavingModel, Parameter::AbstractStructParameterBuilder::StructLike
Defined in:
lib/params_ready/query/relation.rb

Constant Summary collapse

DEFAULT_LIMIT =
10

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Parameter::AbstractStructParameterBuilder::StructLike

#add

Methods inherited from Builder

#helper

Methods inherited from AbstractBuilder

#build, builder, define_parameter, define_registered_parameter, #fetch, fetch_builder, #include, #initialize, #open?, register, register_deprecated, resolve

Methods included from Extensions::Registry

#human_string, #registry

Constructor Details

This class inherits a constructor from ParamsReady::AbstractBuilder

Class Method Details

.instance(name, altn: nil) ⇒ Object



279
280
281
# File 'lib/params_ready/query/relation.rb', line 279

def self.instance(name, altn: nil)
  new RelationDefinition.new(name, altn: altn)
end

Instance Method Details

#join_table(arel_table, type, table_alias: nil, &block) ⇒ Object



307
308
309
310
# File 'lib/params_ready/query/relation.rb', line 307

def join_table(arel_table, type, table_alias: nil, &block)
  join = Join.new arel_table, type, table_alias: nil, &block
  @definition.add_join(join)
end

#order(&proc) ⇒ Object



300
301
302
303
304
305
# File 'lib/params_ready/query/relation.rb', line 300

def order(&proc)
  ordering_builder = @definition.init_ordering_builder(empty: false)
  ordering_builder.instance_eval(&proc) unless proc.nil?
  ordering = ordering_builder.build
  add ordering
end

#paginate(limit = DEFAULT_LIMIT, max_limit = nil, method: :offset, &block) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/params_ready/query/relation.rb', line 286

def paginate(limit = DEFAULT_LIMIT, max_limit = nil, method: :offset, &block)
  case method
  when :offset
    raise ParamsReadyError, 'Block not expected' unless block.nil?
    add Pagination::OffsetPaginationDefinition.new(0, limit, max_limit).finish
  when :keyset
    ordering_builder = @definition.init_ordering_builder(empty: true)
    rcpb = Pagination::KeysetPaginationBuilder.new ordering_builder, limit, max_limit
    add rcpb.build(&block)
  else
    raise "Unimplemented pagination method '#{method}'"
  end
end