praxis 2.0.pre.6 → 2.0.pre.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +1 -3
- data/CHANGELOG.md +25 -0
- data/TODO.md +1 -4
- data/bin/praxis +67 -12
- data/lib/praxis.rb +10 -3
- data/lib/praxis/action_definition.rb +15 -13
- data/lib/praxis/action_definition/headers_dsl_compiler.rb +0 -7
- data/lib/praxis/api_general_info.rb +1 -1
- data/lib/praxis/application.rb +6 -2
- data/lib/praxis/blueprint.rb +357 -0
- data/lib/praxis/bootloader.rb +9 -3
- data/lib/praxis/bootloader_stages/environment.rb +16 -13
- data/lib/praxis/collection.rb +1 -11
- data/lib/praxis/config_hash.rb +44 -0
- data/lib/praxis/docs/{openapi → open_api}/info_object.rb +18 -10
- data/lib/praxis/docs/{openapi → open_api}/media_type_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/operation_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/parameter_object.rb +2 -2
- data/lib/praxis/docs/{openapi → open_api}/paths_object.rb +12 -15
- data/lib/praxis/docs/{openapi → open_api}/request_body_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/response_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/responses_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/schema_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/server_object.rb +0 -0
- data/lib/praxis/docs/{openapi → open_api}/tag_object.rb +0 -0
- data/lib/praxis/docs/open_api_generator.rb +91 -6
- data/lib/praxis/endpoint_definition.rb +273 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +57 -8
- data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +3 -16
- data/lib/praxis/extensions/attribute_filtering/sequel_filter_query_builder.rb +20 -8
- data/lib/praxis/extensions/field_expansion.rb +3 -36
- data/lib/praxis/extensions/pagination.rb +5 -32
- data/lib/praxis/extensions/pagination/ordering_params.rb +5 -1
- data/lib/praxis/extensions/pagination/pagination_params.rb +10 -4
- data/lib/praxis/field_expander.rb +90 -0
- data/lib/praxis/finalizable.rb +34 -0
- data/lib/praxis/mapper/active_model_compat.rb +4 -0
- data/lib/praxis/mapper/resource.rb +18 -2
- data/lib/praxis/mapper/selector_generator.rb +2 -1
- data/lib/praxis/mapper/sequel_compat.rb +7 -0
- data/lib/praxis/media_type.rb +3 -68
- data/lib/praxis/plugin_concern.rb +1 -1
- data/lib/praxis/plugins/mapper_plugin.rb +24 -15
- data/lib/praxis/plugins/pagination_plugin.rb +34 -4
- data/lib/praxis/renderer.rb +88 -0
- data/lib/praxis/request.rb +1 -1
- data/lib/praxis/resource_definition.rb +2 -311
- data/lib/praxis/response_definition.rb +2 -10
- data/lib/praxis/response_template.rb +3 -3
- data/lib/praxis/router.rb +2 -2
- data/lib/praxis/routing_config.rb +1 -1
- data/lib/praxis/tasks/api_docs.rb +17 -64
- data/lib/praxis/tasks/routes.rb +2 -2
- data/lib/praxis/types/media_type_common.rb +1 -11
- data/lib/praxis/version.rb +1 -1
- data/praxis.gemspec +0 -1
- data/spec/functional_spec.rb +5 -9
- data/spec/praxis/action_definition_spec.rb +12 -20
- data/spec/praxis/blueprint_spec.rb +373 -0
- data/spec/praxis/bootloader_spec.rb +10 -2
- data/spec/praxis/collection_spec.rb +0 -13
- data/spec/praxis/config_hash_spec.rb +64 -0
- data/spec/praxis/{resource_definition_spec.rb → endpoint_definition_spec.rb} +37 -64
- data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +19 -8
- data/spec/praxis/extensions/attribute_filtering/filtering_params_spec.rb +106 -0
- data/spec/praxis/extensions/field_expansion_spec.rb +5 -24
- data/spec/praxis/extensions/field_selection/active_record_query_selector_spec.rb +1 -1
- data/spec/praxis/extensions/field_selection/sequel_query_selector_spec.rb +1 -1
- data/spec/praxis/extensions/support/spec_resources_active_model.rb +1 -1
- data/spec/praxis/field_expander_spec.rb +149 -0
- data/spec/praxis/mapper/selector_generator_spec.rb +1 -1
- data/spec/praxis/media_type_identifier_spec.rb +5 -4
- data/spec/praxis/media_type_spec.rb +4 -93
- data/spec/praxis/renderer_spec.rb +188 -0
- data/spec/praxis/response_definition_spec.rb +0 -31
- data/spec/praxis/response_spec.rb +1 -1
- data/spec/praxis/router_spec.rb +8 -8
- data/spec/praxis/routing_config_spec.rb +3 -3
- data/spec/spec_app/app/controllers/instances.rb +13 -7
- data/spec/spec_app/design/media_types/instance.rb +1 -19
- data/spec/spec_app/design/media_types/volume.rb +1 -1
- data/spec/spec_app/design/media_types/volume_snapshot.rb +2 -14
- data/spec/spec_app/design/resources/instances.rb +5 -8
- data/spec/spec_app/design/resources/volume_snapshots.rb +1 -1
- data/spec/spec_app/design/resources/volumes.rb +1 -1
- data/spec/support/spec_authorization_plugin.rb +1 -1
- data/spec/support/spec_blueprints.rb +72 -0
- data/spec/support/{spec_resource_definitions.rb → spec_endpoint_definitions.rb} +2 -2
- data/spec/support/spec_media_types.rb +6 -26
- data/tasks/thor/app.rb +8 -34
- data/tasks/thor/example.rb +51 -285
- data/tasks/thor/model.rb +40 -0
- data/tasks/thor/scaffold.rb +117 -0
- data/tasks/thor/templates/generator/empty_app/.gitignore +0 -1
- data/tasks/thor/templates/generator/empty_app/Gemfile +7 -23
- data/tasks/thor/templates/generator/empty_app/README.md +1 -1
- data/tasks/thor/templates/generator/empty_app/Rakefile +4 -13
- data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.empty_directory +0 -0
- data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.gitkeep +0 -0
- data/tasks/thor/templates/generator/empty_app/config/environment.rb +26 -17
- data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.empty_directory +0 -0
- data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.gitkeep +0 -0
- data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.empty_directory +0 -0
- data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.gitkeep +0 -0
- data/tasks/thor/templates/generator/empty_app/docs/.empty_directory +0 -0
- data/tasks/thor/templates/generator/empty_app/docs/.gitkeep +0 -0
- data/tasks/thor/templates/generator/empty_app/spec/spec_helper.rb +14 -9
- data/tasks/thor/templates/generator/example_app/.gitignore +1 -0
- data/tasks/thor/templates/generator/example_app/Gemfile +19 -0
- data/tasks/thor/templates/generator/example_app/Rakefile +61 -0
- data/tasks/thor/templates/generator/example_app/app/models/user.rb +6 -0
- data/tasks/thor/templates/generator/example_app/app/v1/concerns/controller_base.rb +24 -0
- data/tasks/thor/templates/generator/example_app/app/v1/controllers/users.rb +17 -0
- data/tasks/thor/templates/generator/example_app/app/v1/resources/base.rb +11 -0
- data/tasks/thor/templates/generator/example_app/app/v1/resources/user.rb +25 -0
- data/tasks/thor/templates/generator/example_app/config.ru +30 -0
- data/tasks/thor/templates/generator/example_app/config/environment.rb +41 -0
- data/tasks/thor/templates/generator/example_app/db/migrate/20201010101010_create_users_table.rb +12 -0
- data/tasks/thor/templates/generator/example_app/db/seeds.rb +6 -0
- data/tasks/thor/templates/generator/example_app/design/api.rb +18 -0
- data/tasks/thor/templates/generator/example_app/design/v1/endpoints/users.rb +37 -0
- data/tasks/thor/templates/generator/example_app/design/v1/media_types/user.rb +21 -0
- data/tasks/thor/templates/generator/example_app/spec/helpers/database_helper.rb +20 -0
- data/tasks/thor/templates/generator/example_app/spec/spec_helper.rb +42 -0
- data/tasks/thor/templates/generator/example_app/spec/v1/controllers/users_spec.rb +37 -0
- data/tasks/thor/templates/generator/scaffold/design/endpoints/collection.rb +98 -0
- data/tasks/thor/templates/generator/scaffold/design/media_types/item.rb +18 -0
- data/tasks/thor/templates/generator/scaffold/implementation/controllers/collection.rb +77 -0
- data/tasks/thor/templates/generator/scaffold/implementation/resources/base.rb +11 -0
- data/tasks/thor/templates/generator/scaffold/implementation/resources/item.rb +45 -0
- data/tasks/thor/templates/generator/scaffold/models/active_record.rb +6 -0
- data/tasks/thor/templates/generator/scaffold/models/sequel.rb +6 -0
- metadata +62 -136
- data/lib/api_browser/.bowerrc +0 -3
- data/lib/api_browser/.editorconfig +0 -21
- data/lib/api_browser/Gruntfile.js +0 -581
- data/lib/api_browser/app/index.html +0 -59
- data/lib/api_browser/app/js/app.js +0 -48
- data/lib/api_browser/app/js/controllers/action.js +0 -47
- data/lib/api_browser/app/js/controllers/controller.js +0 -10
- data/lib/api_browser/app/js/controllers/menu.js +0 -93
- data/lib/api_browser/app/js/controllers/trait.js +0 -10
- data/lib/api_browser/app/js/controllers/type.js +0 -24
- data/lib/api_browser/app/js/directives/attribute_description.js +0 -56
- data/lib/api_browser/app/js/directives/attribute_table.js +0 -28
- data/lib/api_browser/app/js/directives/conditional_requirements.js +0 -13
- data/lib/api_browser/app/js/directives/fixed_if_fits.js +0 -38
- data/lib/api_browser/app/js/directives/highlight.js +0 -14
- data/lib/api_browser/app/js/directives/menu_item.js +0 -59
- data/lib/api_browser/app/js/directives/no_container.js +0 -8
- data/lib/api_browser/app/js/directives/readable_list.js +0 -87
- data/lib/api_browser/app/js/directives/request_examples.js +0 -31
- data/lib/api_browser/app/js/directives/type_placeholder.js +0 -30
- data/lib/api_browser/app/js/directives/url.js +0 -15
- data/lib/api_browser/app/js/factories/Configuration.js +0 -12
- data/lib/api_browser/app/js/factories/Documentation.js +0 -61
- data/lib/api_browser/app/js/factories/Example.js +0 -51
- data/lib/api_browser/app/js/factories/PageInfo.js +0 -9
- data/lib/api_browser/app/js/factories/normalize_attributes.js +0 -20
- data/lib/api_browser/app/js/factories/prepare_template.js +0 -15
- data/lib/api_browser/app/js/factories/template_for.js +0 -128
- data/lib/api_browser/app/js/filters/attribute_name.js +0 -10
- data/lib/api_browser/app/js/filters/friendly_json.js +0 -5
- data/lib/api_browser/app/js/filters/has_requirement.js +0 -14
- data/lib/api_browser/app/js/filters/header_info.js +0 -9
- data/lib/api_browser/app/js/filters/is_empty.js +0 -8
- data/lib/api_browser/app/js/filters/markdown.js +0 -6
- data/lib/api_browser/app/js/filters/resource_name.js +0 -5
- data/lib/api_browser/app/js/filters/tag_requirement.js +0 -13
- data/lib/api_browser/app/sass/modules/_body.scss +0 -40
- data/lib/api_browser/app/sass/modules/_cloke.scss +0 -8
- data/lib/api_browser/app/sass/modules/_header.scss +0 -10
- data/lib/api_browser/app/sass/modules/_nav.scss +0 -7
- data/lib/api_browser/app/sass/modules/_sidebar.scss +0 -134
- data/lib/api_browser/app/sass/modules/_switch.scss +0 -55
- data/lib/api_browser/app/sass/modules/_table.scss +0 -13
- data/lib/api_browser/app/sass/praxis.scss +0 -70
- data/lib/api_browser/app/sass/variables/_bootstrap-variables.scss +0 -774
- data/lib/api_browser/app/views/action.html +0 -97
- data/lib/api_browser/app/views/builtin/field-selector.html +0 -24
- data/lib/api_browser/app/views/controller.html +0 -55
- data/lib/api_browser/app/views/directives/attribute_description.html +0 -2
- data/lib/api_browser/app/views/directives/attribute_description/default.html +0 -2
- data/lib/api_browser/app/views/directives/attribute_description/example.html +0 -13
- data/lib/api_browser/app/views/directives/attribute_description/headers.html +0 -8
- data/lib/api_browser/app/views/directives/attribute_description/member_options.html +0 -4
- data/lib/api_browser/app/views/directives/attribute_description/values.html +0 -14
- data/lib/api_browser/app/views/directives/attribute_table.html +0 -17
- data/lib/api_browser/app/views/directives/menu_item.html +0 -8
- data/lib/api_browser/app/views/directives/url.html +0 -3
- data/lib/api_browser/app/views/examples/general.html +0 -26
- data/lib/api_browser/app/views/home.html +0 -5
- data/lib/api_browser/app/views/layout.html +0 -8
- data/lib/api_browser/app/views/menu.html +0 -42
- data/lib/api_browser/app/views/navbar.html +0 -9
- data/lib/api_browser/app/views/trait.html +0 -13
- data/lib/api_browser/app/views/type.html +0 -6
- data/lib/api_browser/app/views/type/details.html +0 -33
- data/lib/api_browser/app/views/types/embedded/array.html +0 -2
- data/lib/api_browser/app/views/types/embedded/default.html +0 -12
- data/lib/api_browser/app/views/types/embedded/field-selector.html +0 -13
- data/lib/api_browser/app/views/types/embedded/links.html +0 -11
- data/lib/api_browser/app/views/types/embedded/requirements.html +0 -6
- data/lib/api_browser/app/views/types/embedded/single_req.html +0 -9
- data/lib/api_browser/app/views/types/embedded/struct.html +0 -14
- data/lib/api_browser/app/views/types/label/link.html +0 -1
- data/lib/api_browser/app/views/types/label/primitive.html +0 -1
- data/lib/api_browser/app/views/types/label/primitive_collection.html +0 -1
- data/lib/api_browser/app/views/types/label/type.html +0 -1
- data/lib/api_browser/app/views/types/label/type_collection.html +0 -1
- data/lib/api_browser/app/views/types/main/array.html +0 -22
- data/lib/api_browser/app/views/types/main/default.html +0 -23
- data/lib/api_browser/app/views/types/main/hash.html +0 -23
- data/lib/api_browser/app/views/types/standalone/array.html +0 -3
- data/lib/api_browser/app/views/types/standalone/default.html +0 -18
- data/lib/api_browser/app/views/types/standalone/struct.html +0 -2
- data/lib/api_browser/bower_template.json +0 -41
- data/lib/api_browser/package-lock.json +0 -7110
- data/lib/api_browser/package.json +0 -43
- data/lib/praxis/docs/generator.rb +0 -243
- data/lib/praxis/docs/link_builder.rb +0 -30
- data/lib/praxis/links.rb +0 -135
- data/lib/praxis/types/multipart.rb +0 -109
- data/spec/api_browser/directives/type_placeholder_spec.js +0 -134
- data/spec/api_browser/factories/configuration_spec.js +0 -32
- data/spec/api_browser/factories/documentation_spec.js +0 -100
- data/spec/api_browser/factories/normalize_attributes_spec.js +0 -92
- data/spec/api_browser/factories/template_for_spec.js +0 -67
- data/spec/api_browser/filters/attribute_name_spec.js +0 -23
- data/spec/praxis/types/multipart_spec.rb +0 -112
- data/tasks/thor/templates/generator/empty_app/.rspec +0 -1
- data/tasks/thor/templates/generator/empty_app/Guardfile +0 -3
- data/tasks/thor/templates/generator/empty_app/config/rainbows.rb +0 -57
- data/tasks/thor/templates/generator/empty_app/docs/app.js +0 -1
- data/tasks/thor/templates/generator/empty_app/docs/styles.scss +0 -3
@@ -5,27 +5,43 @@ module Praxis
|
|
5
5
|
module AttributeFiltering
|
6
6
|
ALIAS_TABLE_PREFIX = ''
|
7
7
|
require_relative 'active_record_patches'
|
8
|
+
# Helper class that can present an SqlLiteral string which we have already quoted
|
9
|
+
# ... but! that can properly provide a "to_sym" that has the value unquoted
|
10
|
+
# This is necessary as (the latest AR code):
|
11
|
+
# * does not carry over "references" in joins if they are not SqlLiterals
|
12
|
+
# * but, at the same time, it indexes the references using the .to_sym value (which is really expected to be the normal string, without quotes)
|
13
|
+
# If we pass a normal SqlLiteral, instead of our wrapper, without quoting the table, the current AR code will never quote it to form the
|
14
|
+
# SQL string, as it's already a literal...so our "/" type separators as names won't work without quoting.
|
15
|
+
class QuasiSqlLiteral < Arel::Nodes::SqlLiteral
|
16
|
+
def initialize(quoted:, symbolized:)
|
17
|
+
@symbolized = symbolized
|
18
|
+
super(quoted)
|
19
|
+
end
|
20
|
+
def to_sym
|
21
|
+
@symbolized
|
22
|
+
end
|
23
|
+
end
|
8
24
|
|
9
25
|
class ActiveRecordFilterQueryBuilder
|
10
|
-
attr_reader :query, :model, :
|
26
|
+
attr_reader :query, :model, :filters_map
|
11
27
|
|
12
28
|
# Base query to build upon
|
13
29
|
def initialize(query: , model:, filters_map:, debug: false)
|
14
30
|
@query = query
|
15
31
|
@model = model
|
16
|
-
@
|
32
|
+
@filters_map = filters_map
|
17
33
|
@logger = debug ? Logger.new(STDOUT) : nil
|
18
34
|
end
|
19
35
|
|
20
|
-
def
|
21
|
-
@logger
|
36
|
+
def debug_query(msg, query)
|
37
|
+
@logger.info(msg + query.to_sql) if @logger
|
22
38
|
end
|
23
39
|
|
24
40
|
def generate(filters)
|
25
41
|
# Resolve the names and values first, based on filters_map
|
26
42
|
root_node = _convert_to_treenode(filters)
|
27
43
|
craft_filter_query(root_node, for_model: @model)
|
28
|
-
|
44
|
+
debug_query("SQL due to filters: ", @query.all)
|
29
45
|
@query
|
30
46
|
end
|
31
47
|
|
@@ -45,13 +61,30 @@ module Praxis
|
|
45
61
|
|
46
62
|
private
|
47
63
|
|
64
|
+
def _mapped_filter(name)
|
65
|
+
target = @filters_map[name]
|
66
|
+
unless target
|
67
|
+
if @model.attribute_names.include?(name.to_s)
|
68
|
+
# Cache it in the filters mapping (to avoid later lookups), and return it.
|
69
|
+
@filters_map[name] = name
|
70
|
+
target = name
|
71
|
+
end
|
72
|
+
end
|
73
|
+
return target
|
74
|
+
end
|
75
|
+
|
48
76
|
# Resolve and convert from filters, to a more manageable and param-type-independent structure
|
49
77
|
def _convert_to_treenode(filters)
|
50
78
|
# Resolve the names and values first, based on filters_map
|
51
79
|
resolved_array = []
|
52
80
|
filters.parsed_array.each do |filter|
|
53
|
-
mapped_value =
|
54
|
-
|
81
|
+
mapped_value = _mapped_filter(filter[:name])
|
82
|
+
unless mapped_value
|
83
|
+
msg = "Filtering by #{filter[:name]} is not allowed. No implementation mapping defined for it has been found \
|
84
|
+
and there is not a model attribute with this name either.\n" \
|
85
|
+
"Please add a mapping for #{filter[:name]} in the `filters_mapping` method of the appropriate Resource class"
|
86
|
+
raise msg
|
87
|
+
end
|
55
88
|
bindings_array = \
|
56
89
|
if mapped_value.is_a?(Proc)
|
57
90
|
result = mapped_value.call(filter)
|
@@ -85,7 +118,7 @@ module Praxis
|
|
85
118
|
end
|
86
119
|
|
87
120
|
def add_clause(column_prefix:, column_object:, op:, value:)
|
88
|
-
@query = @query.references(column_prefix) #Mark where clause referencing the appropriate alias
|
121
|
+
@query = @query.references(build_reference_value(column_prefix)) #Mark where clause referencing the appropriate alias
|
89
122
|
likeval = get_like_value(value)
|
90
123
|
case op
|
91
124
|
when '!' # name! means => name IS NOT NULL (and the incoming value is nil)
|
@@ -165,6 +198,22 @@ module Praxis
|
|
165
198
|
likeval
|
166
199
|
end
|
167
200
|
end
|
201
|
+
|
202
|
+
# The value that we need to stick in the references method is different in the latest Rails
|
203
|
+
maj, min, _ = ActiveRecord.gem_version.segments
|
204
|
+
if maj == 5 || (maj == 6 && min == 0)
|
205
|
+
# In AR 6 (and 6.0) the references are simple strings
|
206
|
+
def build_reference_value(column_prefix)
|
207
|
+
column_prefix
|
208
|
+
end
|
209
|
+
else
|
210
|
+
# The latest AR versions discard passing references to joins when they're not SqlLiterals ... so let's wrap it
|
211
|
+
# with our class, so that it is a literal (already quoted), but that can still provide the expected "symbol" without quotes
|
212
|
+
# so that our aliasing code can match it.
|
213
|
+
def build_reference_value(column_prefix)
|
214
|
+
QuasiSqlLiteral.new(quoted: query.connection.quote_table_name(column_prefix), symbolized: column_prefix.to_sym)
|
215
|
+
end
|
216
|
+
end
|
168
217
|
end
|
169
218
|
end
|
170
219
|
end
|
@@ -103,7 +103,7 @@ module Praxis
|
|
103
103
|
def self.construct(definition, **options)
|
104
104
|
return self if definition.nil?
|
105
105
|
|
106
|
-
DSLCompiler.new(self, options).parse(*definition)
|
106
|
+
DSLCompiler.new(self, **options).parse(*definition)
|
107
107
|
self
|
108
108
|
end
|
109
109
|
|
@@ -166,7 +166,6 @@ module Praxis
|
|
166
166
|
end
|
167
167
|
|
168
168
|
attr_name = match[:attribute].to_sym
|
169
|
-
# TODO: we should coerce values if there's a mediatype defined?
|
170
169
|
coerced = if media_type
|
171
170
|
filter_components = attr_name.to_s.split('.').map(&:to_sym)
|
172
171
|
attr, _enclosing_type = find_filter_attribute(filter_components, media_type)
|
@@ -217,21 +216,9 @@ module Praxis
|
|
217
216
|
errors << "Operator #{item[:op]} not allowed for filter #{attr_name}"
|
218
217
|
end
|
219
218
|
value_type = attr_filters[:value_type]
|
220
|
-
value = item[:value]
|
221
|
-
if value_type && !value_type.valid_type?(value)
|
222
|
-
# Allow a collection of values of the right type for multimatch (if operators are = or !=)
|
223
|
-
if ['=','!='].include?(item[:op])
|
224
|
-
coll_type = Attributor::Collection.of(value_type)
|
225
|
-
if !coll_type.valid_type?(value)
|
226
|
-
errors << "Invalid type in filter/s value for #{attr_name} " +\
|
227
|
-
"(one or more of the multiple matches in #{value} are not a #{value_type.name.split('::').last})"
|
228
|
-
end
|
229
|
-
else
|
230
|
-
errors << "Invalid type in filter value for #{attr_name} (#{value} using '#{item[:op]}' is not a #{value_type.name.split('::').last})"
|
231
|
-
end
|
232
|
-
end
|
233
|
-
|
234
219
|
next unless value_type == Attributor::String
|
220
|
+
|
221
|
+
value = item[:value]
|
235
222
|
unless value.empty?
|
236
223
|
fuzzy_match = attr_filters[:fuzzy_match]
|
237
224
|
if (value[-1] == '*' || value[0] == '*') && !fuzzy_match
|
@@ -9,7 +9,7 @@ module Praxis
|
|
9
9
|
class << self
|
10
10
|
def for(definition)
|
11
11
|
Class.new(self) do
|
12
|
-
@
|
12
|
+
@filters_map = case definition
|
13
13
|
when Hash
|
14
14
|
definition
|
15
15
|
when Array
|
@@ -18,7 +18,7 @@ module Praxis
|
|
18
18
|
raise "Cannot use FilterQueryBuilder.of without passing an array or a hash (Got: #{definition.class.name})"
|
19
19
|
end
|
20
20
|
class << self
|
21
|
-
attr_reader :
|
21
|
+
attr_reader :filters_map
|
22
22
|
end
|
23
23
|
end
|
24
24
|
end
|
@@ -33,13 +33,18 @@ module Praxis
|
|
33
33
|
end
|
34
34
|
|
35
35
|
# By default we'll simply use the incoming op and value, and will map
|
36
|
-
# the attribute based on what's on the `
|
36
|
+
# the attribute based on what's on the `filters_map` definition
|
37
37
|
def generate(filters)
|
38
38
|
raise "Not refactored yet!"
|
39
39
|
seen_associations = Set.new
|
40
40
|
filters.each do |(attr, spec)|
|
41
|
-
column_name =
|
42
|
-
|
41
|
+
column_name = _mapped_filter(attr)
|
42
|
+
unless column_name
|
43
|
+
msg = "Filtering by #{attr} is not allowed. No implementation mapping defined for it has been found \
|
44
|
+
and there is not a model attribute with this name either.\n" \
|
45
|
+
"Please add a mapping for #{attr} in the `filters_mapping` method of the appropriate Resource class"
|
46
|
+
raise msg
|
47
|
+
end
|
43
48
|
if column_name.is_a?(Proc)
|
44
49
|
bindings = column_name.call(spec)
|
45
50
|
# A hash of bindings, consisting of a key with column name and a value to the query value
|
@@ -64,9 +69,16 @@ module Praxis
|
|
64
69
|
add_clause(attr: column_name, op: op, value: value)
|
65
70
|
end
|
66
71
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
72
|
+
def _mapped_filter(name)
|
73
|
+
target = self.class.filters_map[name]
|
74
|
+
unless target
|
75
|
+
if @model.attribute_names.include?(name.to_s)
|
76
|
+
# Cache it in the filters mapping (to avoid later lookups), and return it.
|
77
|
+
self.class.filters_map[name] = name
|
78
|
+
target = name
|
79
|
+
end
|
80
|
+
end
|
81
|
+
return target
|
70
82
|
end
|
71
83
|
|
72
84
|
# Private to try to funnel all column names through `generate` that restricts
|
@@ -7,53 +7,20 @@ module Praxis
|
|
7
7
|
Praxis::ActionDefinition.send(:include, ActionDefinitionExtension)
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
10
|
def expanded_fields
|
12
11
|
@expansion ||= request.action.expanded_fields(self.request, self.media_type)
|
13
12
|
end
|
14
13
|
|
15
|
-
|
16
14
|
module ActionDefinitionExtension
|
17
15
|
extend ActiveSupport::Concern
|
18
16
|
|
19
17
|
def expanded_fields(request, media_type)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# Determine what, if any, fields to display.
|
24
|
-
fields = if use_fields
|
25
|
-
request.params.fields.fields
|
26
|
-
else
|
27
|
-
true
|
28
|
-
end
|
18
|
+
uses_fields = self.params && self.params.attributes.key?(:fields)
|
19
|
+
fields = uses_fields ? request.params.fields.fields : true
|
29
20
|
|
30
|
-
|
31
|
-
view = if use_view && (view_name = request.params.view)
|
32
|
-
media_type.views[view_name]
|
33
|
-
else
|
34
|
-
media_type.views[:default]
|
35
|
-
end
|
36
|
-
|
37
|
-
expandable = if fields == true
|
38
|
-
# We want to show ALL of the available fields.
|
39
|
-
# This can never be applied to the type (it's likely infinitely recursive).
|
40
|
-
# So use view_name determimed above.
|
41
|
-
view
|
42
|
-
else
|
43
|
-
# We want to show SOME of fields available on a view or type.
|
44
|
-
if use_view && request.params.view
|
45
|
-
# Use the requested view.
|
46
|
-
view
|
47
|
-
else
|
48
|
-
# Use the type.
|
49
|
-
media_type
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
Praxis::FieldExpander.expand(expandable,fields)
|
21
|
+
Praxis::FieldExpander.expand(media_type,fields)
|
54
22
|
end
|
55
23
|
end
|
56
|
-
|
57
24
|
end
|
58
25
|
end
|
59
26
|
end
|
@@ -14,10 +14,9 @@ module Praxis
|
|
14
14
|
module Pagination
|
15
15
|
extend ActiveSupport::Concern
|
16
16
|
# This PaginatedController concern should be added to controllers that have actions that define the
|
17
|
-
# pagination and order parameters so that
|
18
|
-
#
|
19
|
-
# This
|
20
|
-
# can be easily applied to other chainable query proxies.
|
17
|
+
# pagination and order parameters so that one can call the domain model to craft the query
|
18
|
+
# `domain_model.craft_pagination_query(base_query, pagination: _pagination)`
|
19
|
+
# This will handle all the required logic for paginating, ordering and generating the Link and TotalCount headers.
|
21
20
|
#
|
22
21
|
# Here's a simple example on how to use it for a fake Items controller
|
23
22
|
# class Items < V1::Controllers::BaseController
|
@@ -29,7 +28,8 @@ module Praxis
|
|
29
28
|
#
|
30
29
|
# def index(filters: nil, pagination: nil, order: nil, **_args)
|
31
30
|
# items = current_user.items.all
|
32
|
-
#
|
31
|
+
# domain_model = self.media_type.domain_model
|
32
|
+
# items = domain_model.craft_pagination_query( query: items, pagination: _pagination)
|
33
33
|
#
|
34
34
|
# display(items)
|
35
35
|
# end
|
@@ -71,33 +71,6 @@ module Praxis
|
|
71
71
|
@_pagination = PaginationStruct.new(pagination[:paginator], pagination[:order])
|
72
72
|
end
|
73
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
74
|
def build_pagination_headers(pagination:, current_url:, current_query_params:)
|
102
75
|
links = if pagination.paginator.by
|
103
76
|
# We're assuming that the last element has a "symbol/string" field with the same name of the "by" pagination.
|
@@ -88,6 +88,10 @@ module Praxis
|
|
88
88
|
|
89
89
|
attr_reader :items
|
90
90
|
|
91
|
+
def self.json_schema_type
|
92
|
+
:string
|
93
|
+
end
|
94
|
+
|
91
95
|
def self.native_type
|
92
96
|
self
|
93
97
|
end
|
@@ -111,7 +115,7 @@ module Praxis
|
|
111
115
|
def self.construct(pagination_definition, **options)
|
112
116
|
return self if pagination_definition.nil?
|
113
117
|
|
114
|
-
DSLCompiler.new(self, options).parse(*pagination_definition)
|
118
|
+
DSLCompiler.new(self, **options).parse(*pagination_definition)
|
115
119
|
self
|
116
120
|
end
|
117
121
|
|
@@ -175,6 +175,10 @@ module Praxis
|
|
175
175
|
end
|
176
176
|
end
|
177
177
|
|
178
|
+
def self.json_schema_type
|
179
|
+
:string
|
180
|
+
end
|
181
|
+
|
178
182
|
def self.native_type
|
179
183
|
self
|
180
184
|
end
|
@@ -198,7 +202,7 @@ module Praxis
|
|
198
202
|
def self.construct(pagination_definition, **options)
|
199
203
|
return self if pagination_definition.nil?
|
200
204
|
|
201
|
-
DSLCompiler.new(self, options).parse(*pagination_definition)
|
205
|
+
DSLCompiler.new(self, **options).parse(*pagination_definition)
|
202
206
|
self
|
203
207
|
end
|
204
208
|
|
@@ -217,10 +221,11 @@ module Praxis
|
|
217
221
|
|
218
222
|
selectable = mt_example.object.keys & simple_attrs.keys
|
219
223
|
by = selectable.sample(1).first
|
220
|
-
from = media_type.attributes[by].example(parent: mt_example)
|
224
|
+
from = media_type.attributes[by].example(parent: mt_example).to_s
|
221
225
|
# Make sure to encode the value of the from, as it can contain commas and such
|
222
|
-
|
223
|
-
"
|
226
|
+
# Only add the from parameter if it's not empty (an empty from is not allowed and it's gonna blow up in load)
|
227
|
+
optional_from_component = (from && !from.empty?) ? ",from=#{CGI.escape(from)}" : ''
|
228
|
+
"by=#{by}#{optional_from_component},items=#{defaults[:page_size]}"
|
224
229
|
else
|
225
230
|
"by=id,from=20,items=100"
|
226
231
|
end
|
@@ -356,6 +361,7 @@ module Praxis
|
|
356
361
|
else
|
357
362
|
s = "by=#{@by}"
|
358
363
|
s += ",from=#{@from}" if @from
|
364
|
+
s
|
359
365
|
end
|
360
366
|
str += ",items=#{items}" if @items
|
361
367
|
str += ",total_count=true" if @total_count
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Praxis
|
3
|
+
class FieldExpander
|
4
|
+
def self.expand(object, fields = true)
|
5
|
+
new.expand(object, fields)
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :stack
|
9
|
+
attr_reader :history
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
@stack = Hash.new do |hash, key|
|
13
|
+
hash[key] = Set.new
|
14
|
+
end
|
15
|
+
@history = Hash.new do |hash, key|
|
16
|
+
hash[key] = {}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def expand(object, fields = true)
|
21
|
+
if stack[object].include? fields
|
22
|
+
return history[object][fields] if history[object].include? fields
|
23
|
+
# We should probably never get here, since we should have a record
|
24
|
+
# of the history of an expansion if we're trying to redo it,
|
25
|
+
# but we should also be conservative and raise here just in case.
|
26
|
+
raise "Circular expansion detected for object #{object.inspect} with fields #{fields.inspect}"
|
27
|
+
else
|
28
|
+
stack[object] << fields
|
29
|
+
end
|
30
|
+
|
31
|
+
result = if object.is_a? Attributor::Attribute
|
32
|
+
expand_type(object.type, fields)
|
33
|
+
else
|
34
|
+
expand_type(object, fields)
|
35
|
+
end
|
36
|
+
|
37
|
+
result
|
38
|
+
ensure
|
39
|
+
stack[object].delete fields
|
40
|
+
end
|
41
|
+
|
42
|
+
def expand_fields(attributes, fields)
|
43
|
+
raise ArgumentError, 'expand_fields must be given a block' unless block_given?
|
44
|
+
|
45
|
+
unless fields == true
|
46
|
+
attributes = attributes.select do |k, _v|
|
47
|
+
fields.key?(k)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
attributes.each_with_object({}) do |(name, dumpable), hash|
|
52
|
+
sub_fields = case fields
|
53
|
+
when true
|
54
|
+
true
|
55
|
+
when Hash
|
56
|
+
fields[name] || true
|
57
|
+
end
|
58
|
+
hash[name] = yield(dumpable, sub_fields)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def expand_type(object, fields = true)
|
63
|
+
unless object.respond_to?(:attributes)
|
64
|
+
if object.respond_to?(:member_attribute)
|
65
|
+
return expand_type(object.member_attribute.type, fields)
|
66
|
+
else
|
67
|
+
return true
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# just include the full thing if it has no attributes
|
72
|
+
return true if object.attributes.empty?
|
73
|
+
|
74
|
+
# True, expands to the default fieldset for blueprints
|
75
|
+
fields = object.default_fieldset if object < Praxis::Blueprint && fields == true
|
76
|
+
|
77
|
+
return history[object][fields] if history[object].include? fields
|
78
|
+
|
79
|
+
history[object][fields] = {}
|
80
|
+
result = expand_fields(object.attributes, fields) do |dumpable, sub_fields|
|
81
|
+
expand(dumpable.type, sub_fields)
|
82
|
+
end
|
83
|
+
unless fields == true
|
84
|
+
non_matching = fields.keys - object.attributes.keys
|
85
|
+
raise "FieldExpansion error: attribute(s) #{non_matching} do not exist in #{object}" unless non_matching.empty?
|
86
|
+
end
|
87
|
+
history[object][fields].merge!(result)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|