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.
- checksums.yaml +4 -4
- data/.rspec +0 -1
- data/.ruby-version +1 -0
- data/.travis.yml +5 -20
- data/CHANGELOG.md +32 -0
- data/Gemfile +1 -1
- data/Guardfile +2 -1
- data/Rakefile +1 -7
- data/TODO.md +28 -0
- data/lib/api_browser/package-lock.json +7110 -0
- data/lib/praxis.rb +7 -4
- data/lib/praxis/action_definition.rb +10 -17
- data/lib/praxis/action_definition/headers_dsl_compiler.rb +1 -1
- data/lib/praxis/api_general_info.rb +21 -0
- data/lib/praxis/application.rb +2 -3
- data/lib/praxis/bootloader_stages/routing.rb +2 -4
- data/lib/praxis/config.rb +1 -1
- data/lib/praxis/docs/generator.rb +11 -6
- data/lib/praxis/docs/open_api_generator.rb +255 -0
- data/lib/praxis/docs/openapi/info_object.rb +31 -0
- data/lib/praxis/docs/openapi/media_type_object.rb +59 -0
- data/lib/praxis/docs/openapi/operation_object.rb +40 -0
- data/lib/praxis/docs/openapi/parameter_object.rb +69 -0
- data/lib/praxis/docs/openapi/paths_object.rb +58 -0
- data/lib/praxis/docs/openapi/request_body_object.rb +51 -0
- data/lib/praxis/docs/openapi/response_object.rb +63 -0
- data/lib/praxis/docs/openapi/responses_object.rb +44 -0
- data/lib/praxis/docs/openapi/schema_object.rb +87 -0
- data/lib/praxis/docs/openapi/server_object.rb +24 -0
- data/lib/praxis/docs/openapi/tag_object.rb +21 -0
- data/lib/praxis/extensions/attribute_filtering.rb +2 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +148 -157
- data/lib/praxis/extensions/attribute_filtering/active_record_patches.rb +15 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/5x.rb +90 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_0.rb +68 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_patches/6_1_plus.rb +58 -0
- data/lib/praxis/extensions/attribute_filtering/filter_tree_node.rb +35 -0
- data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +13 -12
- data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +3 -2
- data/lib/praxis/extensions/field_selection/active_record_query_selector.rb +24 -30
- data/lib/praxis/extensions/field_selection/field_selector.rb +4 -0
- data/lib/praxis/extensions/field_selection/sequel_query_selector.rb +32 -39
- data/lib/praxis/extensions/pagination.rb +130 -0
- data/lib/praxis/extensions/pagination/active_record_pagination_handler.rb +42 -0
- data/lib/praxis/extensions/pagination/header_generator.rb +70 -0
- data/lib/praxis/extensions/pagination/ordering_params.rb +234 -0
- data/lib/praxis/extensions/pagination/pagination_handler.rb +68 -0
- data/lib/praxis/extensions/pagination/pagination_params.rb +374 -0
- data/lib/praxis/extensions/pagination/sequel_pagination_handler.rb +45 -0
- data/lib/praxis/handlers/json.rb +2 -0
- data/lib/praxis/handlers/www_form.rb +5 -0
- data/lib/praxis/handlers/{xml.rb → xml-sample.rb} +6 -0
- data/lib/praxis/links.rb +4 -0
- data/lib/praxis/mapper/active_model_compat.rb +57 -4
- data/lib/praxis/mapper/resource.rb +18 -11
- data/lib/praxis/mapper/selector_generator.rb +99 -75
- data/lib/praxis/mapper/sequel_compat.rb +43 -3
- data/lib/praxis/media_type.rb +1 -56
- data/lib/praxis/media_type_identifier.rb +2 -1
- data/lib/praxis/multipart/part.rb +5 -2
- data/lib/praxis/notifications.rb +1 -1
- data/lib/praxis/plugins/mapper_plugin.rb +17 -3
- data/lib/praxis/plugins/pagination_plugin.rb +71 -0
- data/lib/praxis/resource_definition.rb +8 -16
- data/lib/praxis/response_definition.rb +1 -1
- data/lib/praxis/route.rb +3 -5
- data/lib/praxis/routing_config.rb +3 -7
- data/lib/praxis/tasks/api_docs.rb +23 -0
- data/lib/praxis/tasks/routes.rb +9 -14
- data/lib/praxis/trait.rb +1 -1
- data/lib/praxis/types/media_type_common.rb +12 -2
- data/lib/praxis/types/multipart.rb +1 -1
- data/lib/praxis/types/multipart_array.rb +64 -2
- data/lib/praxis/types/multipart_array/part_definition.rb +1 -1
- data/lib/praxis/validation_handler.rb +1 -2
- data/lib/praxis/version.rb +1 -1
- data/praxis.gemspec +11 -9
- data/spec/functional_spec.rb +9 -6
- data/spec/praxis/action_definition_spec.rb +4 -16
- data/spec/praxis/api_general_info_spec.rb +6 -6
- data/spec/praxis/application_spec.rb +1 -1
- data/spec/praxis/collection_spec.rb +3 -2
- data/spec/praxis/config_spec.rb +2 -2
- data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +304 -0
- data/spec/praxis/extensions/attribute_filtering/filter_tree_node_spec.rb +39 -0
- data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +34 -0
- data/spec/praxis/extensions/field_expansion_spec.rb +6 -24
- data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +110 -0
- data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +148 -0
- data/spec/praxis/extensions/pagination/active_record_pagination_handler_spec.rb +130 -0
- data/spec/praxis/extensions/support/spec_resources_active_model.rb +173 -0
- data/spec/praxis/extensions/support/spec_resources_sequel.rb +106 -0
- data/spec/praxis/mapper/selector_generator_spec.rb +306 -282
- data/spec/praxis/media_type_spec.rb +5 -139
- data/spec/praxis/middleware_app_spec.rb +1 -1
- data/spec/praxis/request_spec.rb +3 -22
- data/spec/praxis/request_stages/action_spec.rb +8 -1
- data/spec/praxis/resource_definition_spec.rb +1 -1
- data/spec/praxis/response_definition_spec.rb +15 -13
- data/spec/praxis/response_spec.rb +1 -1
- data/spec/praxis/route_spec.rb +2 -9
- data/spec/praxis/router_spec.rb +1 -1
- data/spec/praxis/routing_config_spec.rb +4 -13
- data/spec/praxis/types/multipart_array_spec.rb +4 -21
- data/spec/spec_app/app/controllers/instances.rb +1 -1
- data/spec/spec_app/config/environment.rb +0 -2
- data/spec/spec_app/design/api.rb +7 -1
- data/spec/spec_app/design/media_types/instance.rb +0 -8
- data/spec/spec_app/design/media_types/volume.rb +0 -12
- data/spec/spec_app/design/resources/instances.rb +1 -2
- data/spec/spec_helper.rb +17 -0
- data/spec/support/be_deep_equal_matcher.rb +39 -0
- data/spec/support/spec_media_types.rb +0 -73
- data/spec/support/spec_resources.rb +42 -49
- metadata +91 -56
- data/spec/praxis/handlers/xml_spec.rb +0 -177
- data/spec/praxis/links_spec.rb +0 -68
- data/spec/spec_app/app/models/person.rb +0 -3
@@ -0,0 +1,130 @@
|
|
1
|
+
begin
|
2
|
+
require 'link_header'
|
3
|
+
rescue LoadError
|
4
|
+
warn "Praxis::Pagination requires the 'link_header' gem, which can not be found. " \
|
5
|
+
"Please make sure it's in your Gemfile or installed in your system."
|
6
|
+
end
|
7
|
+
require 'praxis/extensions/pagination/pagination_params'
|
8
|
+
require 'praxis/extensions/pagination/ordering_params'
|
9
|
+
require 'praxis/extensions/pagination/pagination_handler'
|
10
|
+
require 'praxis/extensions/pagination/header_generator'
|
11
|
+
|
12
|
+
module Praxis
|
13
|
+
module Extensions
|
14
|
+
module Pagination
|
15
|
+
extend ActiveSupport::Concern
|
16
|
+
# This PaginatedController concern should be added to controllers that have actions that define the
|
17
|
+
# pagination and order parameters so that calling `paginate( query: <base_query>, table: <main_table_name> )`
|
18
|
+
# would handle all the required logic for paginating, ordering and generating the Link and TotalCount headers.
|
19
|
+
# This assumes that the query object are chainable and based on ActiveRecord at the moment (although that logic)
|
20
|
+
# can be easily applied to other chainable query proxies.
|
21
|
+
#
|
22
|
+
# Here's a simple example on how to use it for a fake Items controller
|
23
|
+
# class Items < V1::Controllers::BaseController
|
24
|
+
# include Praxis::Controller
|
25
|
+
# include Praxis::Extensions::Rendering
|
26
|
+
# implements V1::Endpoints::Items
|
27
|
+
#
|
28
|
+
# include Praxis::Extensions::Pagination
|
29
|
+
#
|
30
|
+
# def index(filters: nil, pagination: nil, order: nil, **_args)
|
31
|
+
# items = current_user.items.all
|
32
|
+
# items = handle_pagination( query: items)
|
33
|
+
#
|
34
|
+
# display(items)
|
35
|
+
# end
|
36
|
+
# end
|
37
|
+
#
|
38
|
+
# This code will properly add the right clauses to the final query based on the pagination strategy and ordering
|
39
|
+
# and it will also generate the Link header with the appropriate relationships depending on the paging strategy.
|
40
|
+
# When total_count is requested in the pagination a header with TotalCount will also be included.
|
41
|
+
|
42
|
+
PaginationStruct = Struct.new(:paginator, :order, :total_count)
|
43
|
+
|
44
|
+
included do
|
45
|
+
after :action do |controller, _callee|
|
46
|
+
if controller.response.status < 300
|
47
|
+
# If this action has the pagination parameter defined,
|
48
|
+
# calculate and set the pagination headers (Link header and possibly Total-Count)
|
49
|
+
if controller._pagination.paginator
|
50
|
+
headers = controller.build_pagination_headers(
|
51
|
+
pagination: controller._pagination,
|
52
|
+
current_url: controller.request.path,
|
53
|
+
current_query_params: controller.request.query
|
54
|
+
)
|
55
|
+
controller.response.headers.merge! headers
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Will set the typed paginator and order object into a controller ivar
|
62
|
+
# This is lazily evaluated and memoized, so there's no need to only calculate things for actions that paginate/sort
|
63
|
+
def _pagination
|
64
|
+
return @_pagination if @_pagination
|
65
|
+
|
66
|
+
pagination = {}
|
67
|
+
attrs = request.action&.params&.type&.attributes
|
68
|
+
pagination[:paginator] = request.params.pagination if attrs&.key? :pagination
|
69
|
+
pagination[:order] = request.params.order if attrs&.key? :order
|
70
|
+
|
71
|
+
@_pagination = PaginationStruct.new(pagination[:paginator], pagination[:order])
|
72
|
+
end
|
73
|
+
|
74
|
+
# Main entrypoint: Handles all pagination pieces
|
75
|
+
# takes:
|
76
|
+
# * the query to build from and the table
|
77
|
+
# * the request (for link header generation)
|
78
|
+
# * requires the _pagination variable to be there (set by this module) to return the pagination struct
|
79
|
+
def handle_pagination(query:, type: :active_record)
|
80
|
+
handler_klass = \
|
81
|
+
case type
|
82
|
+
when :active_record
|
83
|
+
ActiveRecordPaginationHandler
|
84
|
+
when :sequel
|
85
|
+
SequelPaginationHandler
|
86
|
+
else
|
87
|
+
raise "Attempting to use pagination but Active Record or Sequel gems found"
|
88
|
+
end
|
89
|
+
|
90
|
+
# Gather and save the count if required
|
91
|
+
if _pagination.paginator&.total_count
|
92
|
+
_pagination.total_count = handler_klass.count(query.dup)
|
93
|
+
end
|
94
|
+
|
95
|
+
query = handler_klass.order(query, _pagination.order)
|
96
|
+
# Maybe this is a class instance instead of a class method?...(of the appropriate AR/Sequel type)...
|
97
|
+
# self.class.paginate(query, table, _pagination)
|
98
|
+
handler_klass.paginate(query, _pagination)
|
99
|
+
end
|
100
|
+
|
101
|
+
def build_pagination_headers(pagination:, current_url:, current_query_params:)
|
102
|
+
links = if pagination.paginator.by
|
103
|
+
# We're assuming that the last element has a "symbol/string" field with the same name of the "by" pagination.
|
104
|
+
last_element = response.body.last
|
105
|
+
if last_element
|
106
|
+
last_value = last_element[pagination.paginator.by.to_sym] || last_element[pagination.paginator.by]
|
107
|
+
end
|
108
|
+
HeaderGenerator.build_cursor_headers(
|
109
|
+
paginator: pagination.paginator,
|
110
|
+
last_value: last_value,
|
111
|
+
total_count: pagination.total_count
|
112
|
+
)
|
113
|
+
else
|
114
|
+
HeaderGenerator.build_paging_headers(
|
115
|
+
paginator: pagination.paginator,
|
116
|
+
total_count: pagination.total_count
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
HeaderGenerator.generate_headers(
|
121
|
+
links: links,
|
122
|
+
current_url: current_url,
|
123
|
+
current_query_params: current_query_params,
|
124
|
+
total_count: pagination.total_count
|
125
|
+
)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'pagination_handler'
|
2
|
+
|
3
|
+
module Praxis
|
4
|
+
module Extensions
|
5
|
+
module Pagination
|
6
|
+
class ActiveRecordPaginationHandler < PaginationHandler
|
7
|
+
|
8
|
+
def self.where_lt(query, attr, value)
|
9
|
+
# TODO: common for AR/Sequel? Seems we could use Arel and more-specific Sequel things
|
10
|
+
query.where(query.table[attr].lt(value))
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.where_gt(query, attr, value)
|
14
|
+
query.where(query.table[attr].gt(value))
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.order(query, order)
|
18
|
+
return query unless order
|
19
|
+
query = query.reorder('')
|
20
|
+
|
21
|
+
order.each do |spec_hash|
|
22
|
+
direction, name = spec_hash.first
|
23
|
+
query = query.order(name => direction)
|
24
|
+
end
|
25
|
+
query
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.count(query)
|
29
|
+
query.count(:all)
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.offset(query, offset)
|
33
|
+
query.offset(offset)
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.limit(query, limit)
|
37
|
+
query.limit(limit)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
module Praxis
|
2
|
+
module Extensions
|
3
|
+
module Pagination
|
4
|
+
class HeaderGenerator
|
5
|
+
def self.build_cursor_headers(paginator:, last_value:, total_count: nil)
|
6
|
+
[:next, :prev, :first, :last].each_with_object({}) do |rel_name, info|
|
7
|
+
case rel_name
|
8
|
+
when :next
|
9
|
+
# If we don't know the total, we'll try to go to the next page
|
10
|
+
# but assume we're done if there isn't a last value...
|
11
|
+
if last_value
|
12
|
+
info[:next] = { by: paginator.by, from: last_value, items: paginator.items }
|
13
|
+
info[:next][:total_count] = true if total_count.present?
|
14
|
+
end
|
15
|
+
when :prev
|
16
|
+
# Not possible to go back
|
17
|
+
when :first
|
18
|
+
info[:first] = { by: paginator.by, items: paginator.items }
|
19
|
+
info[:first][:total_count] = true if total_count.present?
|
20
|
+
when :last
|
21
|
+
# Not possible to scroll to last
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# This is only for plain paging
|
27
|
+
def self.build_paging_headers(paginator:, total_count: nil)
|
28
|
+
last_page = total_count.nil? ? nil : (total_count / (paginator.items * 1.0)).ceil
|
29
|
+
[:next, :prev, :first, :last].each_with_object({}) do |rel_name, info|
|
30
|
+
num = case rel_name
|
31
|
+
when :first
|
32
|
+
1
|
33
|
+
when :prev
|
34
|
+
next if paginator.page < 2
|
35
|
+
paginator.page - 1
|
36
|
+
when :next
|
37
|
+
# don't include this link if we know the total and we see there are no more pages
|
38
|
+
next if last_page.present? && (paginator.page >= last_page)
|
39
|
+
# if we don't know the total, we'll specify to the next page even if it ends up being blank
|
40
|
+
paginator.page + 1
|
41
|
+
when :last
|
42
|
+
next if last_page.blank?
|
43
|
+
last_page
|
44
|
+
end
|
45
|
+
info[rel_name] = {
|
46
|
+
page: num,
|
47
|
+
items: paginator.items,
|
48
|
+
total_count: total_count.present?
|
49
|
+
}
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.generate_headers(links:, current_url:, current_query_params:, total_count:)
|
54
|
+
mapped = links.map do |(rel, info)|
|
55
|
+
# Make sure to encode it our way (with comma-separated args, as it is our own syntax, and not a query string one)
|
56
|
+
pagination_param = info.map { |(k, v)| "#{k}=#{v}" }.join(",")
|
57
|
+
new_url = current_url + "?" + current_query_params.dup.merge(pagination: pagination_param).to_query
|
58
|
+
|
59
|
+
LinkHeader::Link.new(new_url, [["rel", rel.to_s]])
|
60
|
+
end
|
61
|
+
link_header = LinkHeader.new(mapped)
|
62
|
+
|
63
|
+
headers = { "Link" => link_header.to_s }
|
64
|
+
headers["Total-Count"] = total_count if total_count
|
65
|
+
headers
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,234 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
|
3
|
+
module Praxis
|
4
|
+
module Extensions
|
5
|
+
module Pagination
|
6
|
+
class OrderingParams
|
7
|
+
include Attributor::Type
|
8
|
+
include Attributor::Dumpable
|
9
|
+
extend Forwardable
|
10
|
+
|
11
|
+
def_delegators :items, :empty?
|
12
|
+
|
13
|
+
# DSL for restricting how to order.
|
14
|
+
# It allows the concrete list of the fields one can use (through 'by_fields')
|
15
|
+
# It also allows to enforce that list for all positions of the ordering definition (through 'enforce_for :all|:first')
|
16
|
+
# By default, only the first ordering position will be subject to that enforcement (i.e., 'enforce_for :first' is the default)
|
17
|
+
# Example
|
18
|
+
#
|
19
|
+
# attribute :order, Praxis::Types::OrderingParams.for(MediaTypes::Bar) do
|
20
|
+
# by_fields :id, :name
|
21
|
+
# enforce_for :all
|
22
|
+
# end
|
23
|
+
class DSLCompiler < Attributor::DSLCompiler
|
24
|
+
def by_fields(*fields)
|
25
|
+
requested = fields.map(&:to_sym)
|
26
|
+
non_matching = requested - target.media_type.attributes.keys
|
27
|
+
unless non_matching.empty?
|
28
|
+
raise "Error, you've requested to order by fields that do not exist in the mediatype!\n" \
|
29
|
+
"The following #{non_matching.size} field/s do not exist in media type #{target.media_type.name} :\n" +
|
30
|
+
non_matching.join(',').to_s
|
31
|
+
end
|
32
|
+
target.fields_allowed = requested
|
33
|
+
end
|
34
|
+
|
35
|
+
def enforce_for(which)
|
36
|
+
case which.to_sym
|
37
|
+
when :all
|
38
|
+
target.enforce_all = true
|
39
|
+
when :first
|
40
|
+
# nothing, that's the default
|
41
|
+
else
|
42
|
+
raise "Error: unknown parameter for the 'enforce_for' : #{which}. Only :all or :first are allowed"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Configurable DEFAULTS
|
48
|
+
@enforce_all_fields = false
|
49
|
+
|
50
|
+
def self.enforce_all_fields(newval = nil)
|
51
|
+
newval ? @enforce_all_fields = newval : @enforce_all_fields
|
52
|
+
end
|
53
|
+
|
54
|
+
# Ordering type that allows you to specify the ordering characteristing of a requested listing
|
55
|
+
# Ordering is based on given mediatype, which allows for ensuring validation of type names etc.
|
56
|
+
|
57
|
+
# Syntax (similar to json-api)
|
58
|
+
# * One can specify ordering based on several fields (to resolve tie breakers) by separating them with commas
|
59
|
+
# * Requesting a descending order can be done by adding a `-` before the field name. Prepending a `+` enforces
|
60
|
+
# ascending order (which is the default if no sign is specified)
|
61
|
+
# Example:
|
62
|
+
# `name,last_name,-birth_date`
|
63
|
+
|
64
|
+
# Abstract class, which needs to be used by subclassing it through the .for method, to link it to a particular
|
65
|
+
# MediaType, so that the field name checking and value coercion can be performed
|
66
|
+
class << self
|
67
|
+
attr_reader :media_type
|
68
|
+
attr_accessor :fields_allowed
|
69
|
+
attr_accessor :enforce_all # True when we need to enforce the allowed fields at all ordering positions
|
70
|
+
|
71
|
+
def for(media_type, **_opts)
|
72
|
+
unless media_type < Praxis::MediaType
|
73
|
+
raise ArgumentError, "Invalid type: #{media_type.name} for Ordering. " \
|
74
|
+
"Must be a subclass of MediaType"
|
75
|
+
end
|
76
|
+
|
77
|
+
::Class.new(self) do
|
78
|
+
@media_type = media_type
|
79
|
+
if media_type
|
80
|
+
# By default all fields in the mediatype are allowed (but defining a DSL block will override it to more specific ones)
|
81
|
+
@fields_allowed = media_type.attributes.keys
|
82
|
+
end
|
83
|
+
# Default is to only enforce the allowed fields in the first ordering position (the one typicall uses an index if there)
|
84
|
+
@enforce_all = OrderingParams.enforce_all_fields
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
attr_reader :items
|
90
|
+
|
91
|
+
def self.native_type
|
92
|
+
self
|
93
|
+
end
|
94
|
+
|
95
|
+
def self.name
|
96
|
+
'Praxis::Types::OrderingParams'
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.display_name
|
100
|
+
'Ordering'
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.family
|
104
|
+
'string'
|
105
|
+
end
|
106
|
+
|
107
|
+
def self.constructable?
|
108
|
+
true
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.construct(pagination_definition, **options)
|
112
|
+
return self if pagination_definition.nil?
|
113
|
+
|
114
|
+
DSLCompiler.new(self, options).parse(*pagination_definition)
|
115
|
+
self
|
116
|
+
end
|
117
|
+
|
118
|
+
def self.example(_context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
|
119
|
+
fields = if media_type
|
120
|
+
chosen_set = if enforce_all
|
121
|
+
fields_allowed.sample(2)
|
122
|
+
else
|
123
|
+
starting_set = fields_allowed.sample(1)
|
124
|
+
simple_attrs = media_type.attributes.select do |_k, attr|
|
125
|
+
attr.type == Attributor::String || attr.type < Attributor::Numeric || attr.type < Attributor::Temporal
|
126
|
+
end.keys
|
127
|
+
starting_set + simple_attrs.select { |attr| attr != starting_set.first }.sample(1)
|
128
|
+
end
|
129
|
+
chosen_set.each_with_object([]) do |chosen, arr|
|
130
|
+
sign = rand(10) < 5 ? "-" : ""
|
131
|
+
arr << "#{sign}#{chosen}"
|
132
|
+
end.join(',')
|
133
|
+
else
|
134
|
+
"name,last_name,-birth_date"
|
135
|
+
end
|
136
|
+
load(fields)
|
137
|
+
end
|
138
|
+
|
139
|
+
def self.validate(value, context = Attributor::DEFAULT_ROOT_CONTEXT, _attribute = nil)
|
140
|
+
instance = load(value, context)
|
141
|
+
instance.validate(context)
|
142
|
+
end
|
143
|
+
|
144
|
+
def self.load(order, _context = Attributor::DEFAULT_ROOT_CONTEXT, **_options)
|
145
|
+
return order if order.is_a?(native_type)
|
146
|
+
|
147
|
+
parsed_order = {}
|
148
|
+
unless order.nil?
|
149
|
+
parsed_order = order.split(',').each_with_object([]) do |order_string, arr|
|
150
|
+
item = if order_string[0] == '-'
|
151
|
+
{ desc: order_string[1..-1].to_s }
|
152
|
+
elsif order_string[0] == '+'
|
153
|
+
{ asc: order_string[1..-1].to_s }
|
154
|
+
else
|
155
|
+
{ asc: order_string.to_s }
|
156
|
+
end
|
157
|
+
arr.push item
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
new(parsed_order)
|
162
|
+
end
|
163
|
+
|
164
|
+
def self.dump(value, **_opts)
|
165
|
+
load(value).dump
|
166
|
+
end
|
167
|
+
|
168
|
+
def self.describe(_root = false, example: nil)
|
169
|
+
hash = super
|
170
|
+
|
171
|
+
if fields_allowed
|
172
|
+
hash[:fields_allowed] = fields_allowed
|
173
|
+
hash[:enforced_for] = enforce_all ? :all : :first
|
174
|
+
end
|
175
|
+
|
176
|
+
hash
|
177
|
+
end
|
178
|
+
|
179
|
+
def initialize(parsed)
|
180
|
+
@items = parsed
|
181
|
+
end
|
182
|
+
|
183
|
+
def validate(_context = Attributor::DEFAULT_ROOT_CONTEXT)
|
184
|
+
return [] if items.blank?
|
185
|
+
|
186
|
+
errors = []
|
187
|
+
if self.class.fields_allowed
|
188
|
+
# Validate against the enforced components (either all, or just the first one)
|
189
|
+
enforceable_items = self.class.enforce_all ? items : [items.first]
|
190
|
+
|
191
|
+
enforceable_items.each do |spec|
|
192
|
+
_dir, field = spec.first
|
193
|
+
field = field.to_sym
|
194
|
+
next unless !self.class.fields_allowed.include?(field)
|
195
|
+
errors << if self.class.media_type.attributes.key?(field)
|
196
|
+
"Ordering by field \'#{field}\' is disallowed. Ordering is only allowed using the following fields: " +
|
197
|
+
self.class.fields_allowed.map { |f| "\'#{f}\'" }.join(', ').to_s
|
198
|
+
else
|
199
|
+
"Ordering by field \'#{field}\' is not possible as this field does not exist in " \
|
200
|
+
"media type #{self.class.media_type.name}"
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
errors
|
206
|
+
end
|
207
|
+
|
208
|
+
def dump
|
209
|
+
items.each_with_object([]) do |spec, arr|
|
210
|
+
dir, field = spec.first
|
211
|
+
arr << if dir == :desc
|
212
|
+
"-#{field}"
|
213
|
+
else
|
214
|
+
field
|
215
|
+
end
|
216
|
+
end.join(',')
|
217
|
+
end
|
218
|
+
|
219
|
+
def each
|
220
|
+
items.each do |item|
|
221
|
+
yield item
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
# Alias it to a much shorter and sweeter name in the Types namespace.
|
230
|
+
module Praxis
|
231
|
+
module Types
|
232
|
+
OrderingParams = Praxis::Extensions::Pagination::OrderingParams
|
233
|
+
end
|
234
|
+
end
|