Class: Pagy::Cursor

Inherits:
Pagy
  • Object
show all
Defined in:
lib/pagy_cursor/pagy/cursor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ Cursor

Returns a new instance of Cursor.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pagy_cursor/pagy/cursor.rb', line 8

def initialize(vars)
  @vars = VARS.merge(vars.delete_if{|_,v| v.nil? || v == '' })
  @items = vars[:items] || VARS[:items]
  @before = vars[:before]
  @after = vars[:after]
  @arel_table = vars[:arel_table]
  @primary_key = vars[:primary_key]
  @reorder = vars[:order] || {}

  if @before.present? and @after.present?
    raise(ArgumentError, 'before and after can not be both mentioned')
  end

  if vars[:backend] == 'uuid'

    @comparation = 'lt' # arel table less than
    @position = @before
    @order = @reorder.merge({ :created_at => :desc , @primary_key => :desc })

    if @after.present? || (@reorder.present? && @reorder.values.uniq.first.to_sym == :asc)
      @comparation = 'gt' # arel table greater than
      @position = @after
      @order = @reorder.merge({ :created_at => :asc , @primary_key => :asc })
    end
  else

    @comparation = 'lt'
    @position = @before
    @order = @reorder.merge({ @primary_key => :desc })

    if @after.present? || (@reorder.present? && @reorder.values.uniq.first.to_sym == :asc)
      @comparation = 'gt'
      @position = @after
      @order = @reorder.merge({ @primary_key => :asc })
    end
  end
end

Instance Attribute Details

#afterObject (readonly)

Returns the value of attribute after.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def after
  @after
end

#arel_tableObject (readonly)

Returns the value of attribute arel_table.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def arel_table
  @arel_table
end

#beforeObject (readonly)

Returns the value of attribute before.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def before
  @before
end

#comparationObject (readonly)

Returns the value of attribute comparation.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def comparation
  @comparation
end

#has_moreObject Also known as: has_more?

Returns the value of attribute has_more.



5
6
7
# File 'lib/pagy_cursor/pagy/cursor.rb', line 5

def has_more
  @has_more
end

#orderObject (readonly)

Returns the value of attribute order.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def order
  @order
end

#positionObject (readonly)

Returns the value of attribute position.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def position
  @position
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



4
5
6
# File 'lib/pagy_cursor/pagy/cursor.rb', line 4

def primary_key
  @primary_key
end