Class: Jquard::Tables::Query
- Inherits:
-
Object
- Object
- Jquard::Tables::Query
- Defined in:
- lib/jquard/tables/query.rb
Defined Under Namespace
Classes: Result
Constant Summary collapse
- PER_PAGE_OPTIONS =
[ 10, 25, 50 ].freeze
- DEFAULT_PER_PAGE =
10
Instance Method Summary collapse
-
#initialize(table:, scope:, params:) ⇒ Query
constructor
A new instance of Query.
- #result ⇒ Object
Constructor Details
#initialize(table:, scope:, params:) ⇒ Query
Returns a new instance of Query.
12 13 14 15 16 |
# File 'lib/jquard/tables/query.rb', line 12 def initialize(table:, scope:, params:) @table = table @scope = scope @params = params.to_h.symbolize_keys end |
Instance Method Details
#result ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jquard/tables/query.rb', line 18 def result scope = search(@scope) scope, sort, direction = order(scope) total_count = scope.count per_page = resolve_per_page pages = [ (total_count.to_f / per_page).ceil, 1 ].max page = @params[:page].to_i.clamp(1, pages) records = scope.offset((page - 1) * per_page).limit(per_page) Result.new( records: records, total_count: total_count, page: page, pages: pages, per_page: per_page, from: total_count.zero? ? 0 : (page - 1) * per_page + 1, to: [ page * per_page, total_count ].min, sort: sort, direction: direction ) end |