Class: Pagy::Request
- Inherits:
-
Object
- Object
- Pagy::Request
- Defined in:
- lib/pagy/classes/request.rb
Overview
Decouple the request from the env, allowing non-rack apps to use pagy by passing a hash. Resolve the :page and :limit options from params.
Instance Attribute Summary collapse
-
#base_url ⇒ Object
readonly
Returns the value of attribute base_url.
-
#cookie ⇒ Object
readonly
Returns the value of attribute cookie.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(options) ⇒ Request
constructor
A new instance of Request.
- #resolve_limit ⇒ Object
- #resolve_page(force_integer: true) ⇒ Object
Constructor Details
#initialize(options) ⇒ Request
Returns a new instance of Request.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/pagy/classes/request.rb', line 7 def initialize() @options = request = @options[:request] @base_url, @path, @params, @cookie = if request.is_a?(Hash) request.values_at(:base_url, :path, :params, :cookie) else [request.base_url, request.path, get_params(request), request.['pagy']] end end |
Instance Attribute Details
#base_url ⇒ Object (readonly)
Returns the value of attribute base_url.
18 19 20 |
# File 'lib/pagy/classes/request.rb', line 18 def base_url @base_url end |
#cookie ⇒ Object (readonly)
Returns the value of attribute cookie.
18 19 20 |
# File 'lib/pagy/classes/request.rb', line 18 def @cookie end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
18 19 20 |
# File 'lib/pagy/classes/request.rb', line 18 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
18 19 20 |
# File 'lib/pagy/classes/request.rb', line 18 def path @path end |
Instance Method Details
#resolve_limit ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/pagy/classes/request.rb', line 27 def resolve_limit limit_key = @options[:limit_key] || DEFAULT[:limit_key] return @options[:limit] || DEFAULT[:limit] \ unless @options[:client_max_limit] && (requested_limit = @params.dig(@options[:root_key], limit_key) || @params[limit_key]) [requested_limit.to_i, @options[:client_max_limit]].min end |
#resolve_page(force_integer: true) ⇒ Object
20 21 22 23 24 25 |
# File 'lib/pagy/classes/request.rb', line 20 def resolve_page(force_integer: true) page_key = @options[:page_key] || DEFAULT[:page_key] page = @params.dig(@options[:root_key], page_key) || @params[page_key] page = nil if page == '' # fix for app-generated queries like ?page= force_integer ? (page || 1).to_i : page end |