praxis 2.0.pre.1 → 2.0.pre.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +0 -1
  3. data/.ruby-version +1 -0
  4. data/.travis.yml +5 -20
  5. data/CHANGELOG.md +32 -0
  6. data/Gemfile +1 -1
  7. data/Guardfile +2 -1
  8. data/Rakefile +1 -7
  9. data/TODO.md +28 -0
  10. data/lib/api_browser/package-lock.json +7110 -0
  11. data/lib/praxis.rb +7 -4
  12. data/lib/praxis/action_definition.rb +10 -17
  13. data/lib/praxis/action_definition/headers_dsl_compiler.rb +1 -1
  14. data/lib/praxis/api_general_info.rb +21 -0
  15. data/lib/praxis/application.rb +2 -3
  16. data/lib/praxis/bootloader_stages/routing.rb +2 -4
  17. data/lib/praxis/config.rb +1 -1
  18. data/lib/praxis/docs/generator.rb +11 -6
  19. data/lib/praxis/docs/open_api_generator.rb +255 -0
  20. data/lib/praxis/docs/openapi/info_object.rb +31 -0
  21. data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
  22. data/lib/praxis/docs/openapi/operation_object.rb +40 -0
  23. data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
  24. data/lib/praxis/docs/openapi/paths_object.rb +58 -0
  25. data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
  26. data/lib/praxis/docs/openapi/response_object.rb +63 -0
  27. data/lib/praxis/docs/openapi/responses_object.rb +44 -0
  28. data/lib/praxis/docs/openapi/schema_object.rb +87 -0
  29. data/lib/praxis/docs/openapi/server_object.rb +24 -0
  30. data/lib/praxis/docs/openapi/tag_object.rb +21 -0
  31. data/lib/praxis/extensions/attribute_filtering.rb +2 -0
  32. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +148 -157
  33. data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +15 -0
  34. data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +90 -0
  35. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +68 -0
  36. data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +58 -0
  37. data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +35 -0
  38. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +13 -12
  39. data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +3 -2
  40. data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +24 -30
  41. data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
  42. data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +32 -39
  43. data/lib/praxis/extensions/pagination.rb +130 -0
  44. data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +42 -0
  45. data/lib/praxis/extensions/pagination/header_generator.rb +70 -0
  46. data/lib/praxis/extensions/pagination/ordering_params.rb +234 -0
  47. data/lib/praxis/extensions/pagination/pagination_handler.rb +68 -0
  48. data/lib/praxis/extensions/pagination/pagination_params.rb +374 -0
  49. data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +45 -0
  50. data/lib/praxis/handlers/json.rb +2 -0
  51. data/lib/praxis/handlers/www_form.rb +5 -0
  52. data/lib/praxis/handlers/{xml.rb → xml-sample.rb} +6 -0
  53. data/lib/praxis/links.rb +4 -0
  54. data/lib/praxis/mapper/active_model_compat.rb +57 -4
  55. data/lib/praxis/mapper/resource.rb +18 -11
  56. data/lib/praxis/mapper/selector_generator.rb +99 -75
  57. data/lib/praxis/mapper/sequel_compat.rb +43 -3
  58. data/lib/praxis/media_type.rb +1 -56
  59. data/lib/praxis/media_type_identifier.rb +2 -1
  60. data/lib/praxis/multipart/part.rb +5 -2
  61. data/lib/praxis/notifications.rb +1 -1
  62. data/lib/praxis/plugins/mapper_plugin.rb +17 -3
  63. data/lib/praxis/plugins/pagination_plugin.rb +71 -0
  64. data/lib/praxis/resource_definition.rb +8 -16
  65. data/lib/praxis/response_definition.rb +1 -1
  66. data/lib/praxis/route.rb +3 -5
  67. data/lib/praxis/routing_config.rb +3 -7
  68. data/lib/praxis/tasks/api_docs.rb +23 -0
  69. data/lib/praxis/tasks/routes.rb +9 -14
  70. data/lib/praxis/trait.rb +1 -1
  71. data/lib/praxis/types/media_type_common.rb +12 -2
  72. data/lib/praxis/types/multipart.rb +1 -1
  73. data/lib/praxis/types/multipart_array.rb +64 -2
  74. data/lib/praxis/types/multipart_array/part_definition.rb +1 -1
  75. data/lib/praxis/validation_handler.rb +1 -2
  76. data/lib/praxis/version.rb +1 -1
  77. data/praxis.gemspec +11 -9
  78. data/spec/functional_spec.rb +9 -6
  79. data/spec/praxis/action_definition_spec.rb +4 -16
  80. data/spec/praxis/api_general_info_spec.rb +6 -6
  81. data/spec/praxis/application_spec.rb +1 -1
  82. data/spec/praxis/collection_spec.rb +3 -2
  83. data/spec/praxis/config_spec.rb +2 -2
  84. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +304 -0
  85. data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +39 -0
  86. data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +34 -0
  87. data/spec/praxis/extensions/field_expansion_spec.rb +6 -24
  88. data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +110 -0
  89. data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +148 -0
  90. data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +130 -0
  91. data/spec/praxis/extensions/support/spec_resources_active_model.rb +173 -0
  92. data/spec/praxis/extensions/support/spec_resources_sequel.rb +106 -0
  93. data/spec/praxis/mapper/selector_generator_spec.rb +306 -282
  94. data/spec/praxis/media_type_spec.rb +5 -139
  95. data/spec/praxis/middleware_app_spec.rb +1 -1
  96. data/spec/praxis/request_spec.rb +3 -22
  97. data/spec/praxis/request_stages/action_spec.rb +8 -1
  98. data/spec/praxis/resource_definition_spec.rb +1 -1
  99. data/spec/praxis/response_definition_spec.rb +15 -13
  100. data/spec/praxis/response_spec.rb +1 -1
  101. data/spec/praxis/route_spec.rb +2 -9
  102. data/spec/praxis/router_spec.rb +1 -1
  103. data/spec/praxis/routing_config_spec.rb +4 -13
  104. data/spec/praxis/types/multipart_array_spec.rb +4 -21
  105. data/spec/spec_app/app/controllers/instances.rb +1 -1
  106. data/spec/spec_app/config/environment.rb +0 -2
  107. data/spec/spec_app/design/api.rb +7 -1
  108. data/spec/spec_app/design/media_types/instance.rb +0 -8
  109. data/spec/spec_app/design/media_types/volume.rb +0 -12
  110. data/spec/spec_app/design/resources/instances.rb +1 -2
  111. data/spec/spec_helper.rb +17 -0
  112. data/spec/support/be_deep_equal_matcher.rb +39 -0
  113. data/spec/support/spec_media_types.rb +0 -73
  114. data/spec/support/spec_resources.rb +42 -49
  115. metadata +91 -56
  116. data/spec/praxis/handlers/xml_spec.rb +0 -177
  117. data/spec/praxis/links_spec.rb +0 -68
  118. data/spec/spec_app/app/models/person.rb +0 -3
@@ -0,0 +1,68 @@
1
+ module Praxis
2
+ module Extensions
3
+ module Pagination
4
+ class PaginationHandler
5
+ class PaginationException < Exception; end
6
+
7
+ def self.paginate(query, pagination)
8
+ return query unless pagination.paginator
9
+
10
+ paginator = pagination.paginator
11
+
12
+ q = if paginator.page
13
+ offset(query, (paginator.page - 1) * paginator.items)
14
+ else
15
+ # If there is an order clause that complies with the "by" field sorting, we can use it directly
16
+ # i.e., We can be smart about allowing the main sort field matching the pagination one (in case you want to sub-order in a custom way)
17
+ oclause = if pagination.order.nil? || pagination.order.empty? # No ordering specified => use ascending based on the "by" field
18
+ direction = :asc
19
+ order(query, [{ asc: paginator.by }])
20
+ else
21
+ first_ordering = pagination.order.items.first
22
+ direction = first_ordering.keys.first
23
+ unless first_ordering[direction].to_sym == pagination.paginator.by.to_sym
24
+ string_clause = pagination.order.items.map { |h|
25
+ dir, name = h.first
26
+ "#{name} #{dir}"
27
+ }.join(',')
28
+ raise PaginationException,
29
+ "Ordering clause [#{string_clause}] is incompatible with pagination by field '#{pagination.paginator.by}'. " \
30
+ "When paginating by a field value, one cannot specify the 'order' clause " \
31
+ "unless the clause's primary field matches the pagination field."
32
+ end
33
+ order(query, pagination.order)
34
+ end
35
+
36
+ if paginator.from
37
+ if direction == :desc
38
+ where_lt(oclause, paginator.by, paginator.from)
39
+ else
40
+ where_gt(oclause, paginator.by, paginator.from)
41
+ end
42
+ else
43
+ oclause
44
+ end
45
+
46
+ end
47
+ limit(q, paginator.items)
48
+ end
49
+
50
+ def self.where_lt(query, attr, value)
51
+ raise "implement in derived class"
52
+ end
53
+
54
+ def self.where_gt(query, attr, value)
55
+ raise "implement in derived class"
56
+ end
57
+
58
+ def self.offset(query, offset)
59
+ raise "implement in derived class"
60
+ end
61
+
62
+ def self.limit(query, limit)
63
+ raise "implement in derived class"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,374 @@
1
+ module Praxis
2
+ module Extensions
3
+ module Pagination
4
+ class PaginationParams
5
+ include Attributor::Type
6
+ include Attributor::Dumpable
7
+
8
+ # Pagination type that allows you to define a parameter type in an endpoint, including a DSL to restrict or configure
9
+ # the pagination options of every single definition, and which will take care of parsing, coercing and validating the
10
+ # pagination syntax below. It also takes care of generating the Link
11
+ # and total count headers.
12
+ # Pagination supports both cursor and page-based:
13
+ # 1 - Page-based pagination (i.e., offset-limit based paging assuming an explicit or implicit ordering underneath)
14
+ # * it is done by using the 'page=<integer>' parameter, indicating which page number to output (based on a given page size)
15
+ # * setting the page size on a per-request basis can be achieved by setting the 'items=<integer>` parameter
16
+ # * example: `page=5,items=50` (should return items 200-250 from the collection)
17
+ # 2- Cursor-based pagination (paginating based on a field value)
18
+ # * it is done by using 'by=email', indicating the field name to use, and possibly using 'from=joe@example.com' to indicate
19
+ # after which values of the field to start listing (no 'from' values assume starting from the beginning).
20
+ # * the default page size can be overriden on a per-request basis as well with the 'items=<integer>` parameter
21
+ # # example `by=email,from=joe@example.com,items=100`
22
+ #
23
+ # In either situation, one can also request to receive the total count of elements existing in the collection (pre-paging)
24
+ # by using 'total_count=true'.
25
+ #
26
+ # Every pagination request will also receive a Link header (RFC 5988) properly populated with followable links.
27
+ # When the 'total_count=true' parameter is used, a 'Total-Count' header will also be returned containing the total number
28
+ # of existing results pre-pagination. Note that calculating the total count incurs in an extra DB query so it does have
29
+ # performance implications
30
+
31
+ ######################################################
32
+ # DSL for definition pagination parameters in a defined filter.
33
+ # Available options are:
34
+ #
35
+ # One can limit which fields the pagination (by cursor) can be allowed. Typically only indexed fields should
36
+ # be allowed for performance reasons:
37
+ # * by_fields <Array of field names> (if not provided, all fields are allowed)
38
+ # One can limit the total maximum of items of the requested page size from the client can ask for:
39
+ # * max_items <integer> (there is a static upper limit to thie value set by the MAX_ITEMS constant)
40
+ # One can set the default amount of items to return when not specified by the client
41
+ # * page_size <integer> (less or equal than max_items, if the max is set)
42
+ # One can expicitly disallow either paging or cursor based pagination (by default both are allowed)
43
+ # * disallow :paging | :cursor
44
+ # One can set the default pagination mode when no :page, :by/:from parameters are passed in.
45
+ # * default <mode>: <value> where mode can be :page or :by (and the value is an integer or a field name respectively)
46
+ #
47
+ # Here's a full example:
48
+ # attribute :pagination, Types::PaginationParams.for(MediaTypes::Book) do
49
+ # by_fields :id, :name
50
+ # max_items 500
51
+ # page_size 50
52
+ # disallow :paging
53
+ # default by: :id
54
+ # end
55
+ class DSLCompiler < Attributor::DSLCompiler
56
+ def by_fields(*fields)
57
+ requested = fields.map(&:to_sym)
58
+ non_matching = requested - target.media_type.attributes.keys
59
+ unless non_matching.empty?
60
+ raise "Error, you've requested to paginate by fields that do not exist in the mediatype!\n" \
61
+ "The following #{non_matching.size} field/s do not exist in media type #{target.media_type.name} :\n" +
62
+ non_matching.join(',').to_s
63
+ end
64
+ target.fields_allowed = requested
65
+ end
66
+
67
+ def max_items(max)
68
+ target.defaults[:max_items] = Integer(max)
69
+ end
70
+
71
+ def page_size(size)
72
+ target.defaults[:page_size] = Integer(size)
73
+ end
74
+
75
+ def disallow(pagination_type)
76
+ default_mode, default_value = target.defaults[:default_mode].first
77
+ case pagination_type
78
+ when :paging
79
+ if default_mode == :page
80
+ raise "Cannot disallow page-based pagination if you define a default pagination of: page: #{default_value}"
81
+ end
82
+ target.defaults[:disallow_paging] = true
83
+ when :cursor
84
+ if default_mode == :by
85
+ raise "Cannot disallow cursor-based pagination if you define a default pagination of: by: #{default_value}"
86
+ end
87
+ target.defaults[:disallow_cursor] = true
88
+ end
89
+ end
90
+
91
+ def default(spec)
92
+ unless spec.is_a?(Hash) && spec.keys.size == 1 && [:by, :page].include?(spec.keys.first)
93
+ raise "'default' syntax for pagination takes exactly one key specification. Either by: <:fieldname> or page: <num>" \
94
+ "#{spec} is invalid"
95
+ end
96
+ mode, value = spec.first
97
+ def_mode = case mode
98
+ when :by
99
+ if target.fields_allowed && !target.fields_allowed&.include?(value)
100
+ raise "Error setting default pagination. Field #{value} is not amongst the allowed fields."
101
+ end
102
+ if target.defaults[:disallow_cursor]
103
+ raise "Cannot define a default pagination that is cursor based, if cursor-based pagination is disallowed."
104
+ end
105
+ { by: value }
106
+ when :page
107
+ unless value.is_a?(Integer)
108
+ raise "Error setting default pagination. Initial page should be a integer (but got #{value})"
109
+ end
110
+ if target.defaults[:disallow_paging]
111
+ raise "Cannot define a default pagination that is page-based, if page-based pagination is disallowed."
112
+ end
113
+ { page: value }
114
+ end
115
+ target.defaults[:default_mode] = def_mode
116
+ end
117
+ end
118
+
119
+ # Configurable DEFAULTS
120
+ @max_items = nil # Unlimited by default (since it's not possible to set it to nil for now from the app)
121
+ @default_page_size = 100
122
+ @paging_default_mode = { page: 1 }
123
+ @disallow_paging_by_default = false
124
+ @disallow_cursor_by_default = false
125
+
126
+ def self.max_items(newval = nil)
127
+ newval ? @max_items = newval : @max_items
128
+ end
129
+
130
+ def self.default_page_size(newval = nil)
131
+ newval ? @default_page_size = newval : @default_page_size
132
+ end
133
+
134
+ def self.disallow_paging_by_default(newval = nil)
135
+ newval ? @disallow_paging_by_default = newval : @disallow_paging_by_default
136
+ end
137
+
138
+ def self.disallow_cursor_by_default(newval = nil)
139
+ newval ? @disallow_cursor_by_default = newval : @disallow_cursor_by_default
140
+ end
141
+
142
+ def self.paging_default_mode(newval = nil)
143
+ if newval
144
+ unless newval.respond_to?(:keys) && newval.keys.size == 1 && [:by, :page].include?(newval.keys.first)
145
+ raise "Error setting paging_default_mode, value must be a hash with :by or :page keys"
146
+ end
147
+ @paging_default_mode = newval
148
+ end
149
+ @paging_default_mode
150
+ end
151
+
152
+ # Abstract class, which needs to be used by subclassing it through the .for method, to link it to a particular
153
+ # MediaType, so that the field name checking and value coercion can be performed
154
+ class << self
155
+ attr_reader :media_type
156
+ attr_reader :defaults
157
+ attr_accessor :fields_allowed
158
+
159
+ def for(media_type, **_opts)
160
+ unless media_type < Praxis::MediaType
161
+ raise ArgumentError, "Invalid type: #{media_type.name} for Paginator. " \
162
+ "Must be a subclass of MediaType"
163
+ end
164
+
165
+ ::Class.new(self) do
166
+ @media_type = media_type
167
+ @defaults = {
168
+ page_size: PaginationParams.default_page_size,
169
+ max_items: PaginationParams.max_items,
170
+ disallow_paging: PaginationParams.disallow_paging_by_default,
171
+ disallow_cursor: PaginationParams.disallow_cursor_by_default,
172
+ default_mode: PaginationParams.paging_default_mode
173
+ }
174
+ end
175
+ end
176
+ end
177
+
178
+ def self.native_type
179
+ self
180
+ end
181
+
182
+ def self.name
183
+ 'Extensions::Pagination::PaginationParams'
184
+ end
185
+
186
+ def self.display_name
187
+ 'Paginator'
188
+ end
189
+
190
+ def self.family
191
+ 'string'
192
+ end
193
+
194
+ def self.constructable?
195
+ true
196
+ end
197
+
198
+ def self.construct(pagination_definition, **options)
199
+ return self if pagination_definition.nil?
200
+
201
+ DSLCompiler.new(self, options).parse(*pagination_definition)
202
+ self
203
+ end
204
+
205
+ attr_reader :by
206
+ attr_reader :from
207
+ attr_reader :items
208
+ attr_reader :page
209
+ attr_reader :total_count
210
+
211
+ def self.example(_context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
212
+ fields = if media_type
213
+ mt_example = media_type.example
214
+ simple_attrs = media_type.attributes.select do |_k, attr|
215
+ attr.type == Attributor::String || attr.type == Attributor::Integer
216
+ end
217
+
218
+ selectable = mt_example.object.keys & simple_attrs.keys
219
+ by = selectable.sample(1).first
220
+ from = media_type.attributes[by].example(parent: mt_example)
221
+ # Make sure to encode the value of the from, as it can contain commas and such
222
+ from = CGI.escape(from) if from.is_a? String
223
+ "by=#{by},from=#{from},items=#{defaults[:page_size]}"
224
+ else
225
+ "by=id,from=20,items=100"
226
+ end
227
+ load(fields)
228
+ end
229
+
230
+ def self.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil)
231
+ instance = load(value, context)
232
+ instance.validate(context)
233
+ end
234
+
235
+ CLAUSE_REGEX = /(?<type>[^=]+)=(?<value>.+)$/
236
+ def self.load(paginator, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
237
+ return paginator if paginator.is_a?(native_type) || paginator.nil?
238
+ parsed = {}
239
+ unless paginator.nil?
240
+ parsed = paginator.split(',').each_with_object({}) do |paginator_string, hash|
241
+ match = CLAUSE_REGEX.match(paginator_string)
242
+ case match[:type].to_sym
243
+ when :page
244
+ hash[:page] = Integer(match[:value])
245
+ when :by
246
+ hash[:by] = match[:value]
247
+ when :from
248
+ hash[:from] = match[:value]
249
+ when :total_count
250
+ hash[:total_count] = (match[:value] != 'false') # unless explicitly set to false, we'll take it as true...
251
+ when :items
252
+ hash[:items] = Integer(match[:value])
253
+ else
254
+ raise "Error loading pagination parameters: unknown parameter with name '#{match[:type]}' found"
255
+ end
256
+ end
257
+ end
258
+
259
+ parsed[:items] = defaults[:page_size] unless parsed.key?(:items)
260
+ parsed[:from] = coerce_field(parsed[:by], parsed[:from]) if parsed.key?(:from)
261
+
262
+ # If no by/from or page specified, we're gonna apply the defaults
263
+ unless parsed.key?(:by) || parsed.key?(:from) || parsed.key?(:page)
264
+ mode, value = defaults[:default_mode].first
265
+ case mode
266
+ when :by
267
+ parsed[:by] = value
268
+ when :page
269
+ parsed[:page] = value
270
+ end
271
+ end
272
+
273
+ new(parsed)
274
+ end
275
+
276
+ def self.dump(value, **_opts)
277
+ load(value).dump
278
+ end
279
+
280
+ def self.describe(_root = false, example: nil)
281
+ hash = super
282
+
283
+ hash[:fields_allowed] = fields_allowed if fields_allowed
284
+ if defaults
285
+ hash[:max_items] = defaults[:max_items]
286
+ hash[:page_size] = defaults[:page_size]
287
+ hash[:default_mode] = defaults[:default_mode]
288
+
289
+ disallowed = []
290
+ disallowed << :paging if defaults[:disallow_paging] == true
291
+ disallowed << :cursor if defaults[:disallow_cursor] == true
292
+ hash[:disallowed] = disallowed unless disallowed.empty?
293
+ end
294
+
295
+ hash
296
+ end
297
+
298
+ # Silently ignore if the fiels does not exist...let's let the validation check it instead
299
+ def self.coerce_field(name, value)
300
+ if media_type&.attributes
301
+ attrs = media_type&.attributes || {}
302
+ attribute = attrs[name.to_sym]
303
+ attribute.type.load(value) if attribute
304
+ else
305
+ value
306
+ end
307
+ end
308
+
309
+ # Instance methods
310
+ def initialize(parsed)
311
+ @by = parsed[:by]
312
+ @from = parsed[:from]
313
+ @items = parsed[:items]
314
+ @page = parsed[:page]
315
+ @total_count = parsed[:total_count]
316
+ end
317
+
318
+ def validate(_context = Attributor::DEFAULT_ROOT_CONTEXT) # rubocop:disable Metrics/PerceivedComplexity
319
+ errors = []
320
+
321
+ if page
322
+ if self.class.defaults[:disallow_paging]
323
+ errors << "Page-based pagination is disallowed (i.e., using 'page=' parameter)"
324
+ end
325
+ elsif self.class.defaults[:disallow_cursor]
326
+ errors << "Cursor-based pagination is disallowed (i.e., using 'by=' or 'from=' parameter)"
327
+ end
328
+
329
+ if page && page <= 0
330
+ errors << "Page parameter cannot be zero or negative! (got: #{parsed.page})"
331
+ end
332
+
333
+ if items && (items <= 0 || ( self.class.defaults[:max_items] && items > self.class.defaults[:max_items]) )
334
+ errors << "Value of 'items' is invalid (got: #{items}). It must be positive, and smaller than the maximum amount of items per request (set to #{self.class.defaults[:max_items]})"
335
+ end
336
+
337
+ if page && (by || from)
338
+ errors << "Cannot specify the field to use and its start value to paginate from when using a fix pager (i.e., `by` and/or `from` params are not compabible with `page`)"
339
+ end
340
+
341
+ if by && self.class.fields_allowed && !self.class.fields_allowed.include?(by.to_sym)
342
+ errors << if self.class.media_type.attributes.key?(by.to_sym)
343
+ "Paginating by field \'#{by}\' is disallowed"
344
+ else
345
+ "Paginating by field \'#{by}\' is not possible as this field does not exist in "\
346
+ "media type #{self.class.media_type.name}"
347
+ end
348
+ end
349
+ errors
350
+ end
351
+
352
+ # Dump back string parseable form
353
+ def dump
354
+ str = if @page
355
+ "page=#{@page}"
356
+ else
357
+ s = "by=#{@by}"
358
+ s += ",from=#{@from}" if @from
359
+ end
360
+ str += ",items=#{items}" if @items
361
+ str += ",total_count=true" if @total_count
362
+ str
363
+ end
364
+ end
365
+ end
366
+ end
367
+ end
368
+
369
+ # Alias it to a much shorter and sweeter name in the Types namespace.
370
+ module Praxis
371
+ module Types
372
+ PaginationParams = Praxis::Extensions::Pagination::PaginationParams
373
+ end
374
+ end
@@ -0,0 +1,45 @@
1
+ require_relative 'pagination_handler'
2
+
3
+ module Praxis
4
+ module Extensions
5
+ module Pagination
6
+ class SequelPaginationHandler < PaginationHandler
7
+
8
+ def self.where_lt(query, attr, value)
9
+ query.where("#{attr} < ?", value)
10
+ end
11
+
12
+ def self.where_gt(query, attr, value)
13
+ query.where("#{attr} > ?", value)
14
+ end
15
+
16
+ def self.order(query, order)
17
+ return query unless order
18
+ order_clause = order.map do |spec_hash|
19
+ direction, name = spec_hash.first
20
+ case direction.to_sym
21
+ when :desc
22
+ Sequel.desc(name.to_sym)
23
+ else
24
+ Sequel.asc(name.to_sym)
25
+ end
26
+ end
27
+ query = query.order(*order_clause)
28
+ query
29
+ end
30
+
31
+ def self.count(query)
32
+ query.count
33
+ end
34
+
35
+ def self.offset(query, offset)
36
+ query.offset(offset)
37
+ end
38
+
39
+ def self.limit(query, limit)
40
+ query.limit(limit)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end