Class: SlackBot::Pager

Inherits:
Object
  • Object
show all
Defined in:
lib/slack_bot/pager.rb,
sig/slack_bot.rbs

Constant Summary collapse

DEFAULT_PAGE =

Returns:

  • (Integer)
1
DEFAULT_LIMIT =

Returns:

  • (Integer)
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_cursor, args:, limit: nil, page: nil) ⇒ Pager

Returns a new instance of Pager.

Parameters:

  • source_cursor (ActiveRecord::Relation)
  • args: (Args)
  • limit: (Integer) (defaults to: nil)
  • page: (Integer) (defaults to: nil)


7
8
9
10
11
12
# File 'lib/slack_bot/pager.rb', line 7

def initialize(source_cursor, args:, limit: nil, page: nil)
  @source_cursor = source_cursor
  @args = args
  @limit = limit || @args[:per_page]&.to_i || DEFAULT_LIMIT
  @page = page || @args[:page]&.to_i || DEFAULT_PAGE
end

Instance Attribute Details

#argsArgs (readonly)

Returns the value of attribute args.

Returns:



6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def args
  @args
end

#limitInteger (readonly)

Returns the value of attribute limit.

Returns:

  • (Integer)


6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def limit
  @limit
end

#pageInteger (readonly)

Returns the value of attribute page.

Returns:

  • (Integer)


6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def page
  @page
end

#source_cursorActiveRecord::Relation (readonly)

Returns the value of attribute source_cursor.

Returns:

  • (ActiveRecord::Relation)


6
7
8
# File 'lib/slack_bot/pager.rb', line 6

def source_cursor
  @source_cursor
end

Instance Method Details

#cursorActiveRecord::Relation

Returns:

  • (ActiveRecord::Relation)


26
27
28
# File 'lib/slack_bot/pager.rb', line 26

def cursor
  source_cursor.limit(limit).offset(offset)
end

#offsetInteger

Returns:

  • (Integer)


22
23
24
# File 'lib/slack_bot/pager.rb', line 22

def offset
  (page - 1) * limit
end

#pages_countInteger

Returns:

  • (Integer)


18
19
20
# File 'lib/slack_bot/pager.rb', line 18

def pages_count
  (total_count.to_f / limit).ceil
end

#total_countInteger

Returns:

  • (Integer)


14
15
16
# File 'lib/slack_bot/pager.rb', line 14

def total_count
  @total_count ||= source_cursor.count
end