praxis 2.0.pre.9 → 2.0.pre.10
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/CHANGELOG.md +11 -0
- data/TODO.md +1 -4
- data/bin/praxis +11 -13
- 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 +15 -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 +0 -0
- 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 +0 -0
- data/lib/praxis/docs/{openapi → open_api}/paths_object.rb +0 -0
- 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 +90 -5
- data/lib/praxis/endpoint_definition.rb +273 -0
- data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +1 -1
- data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +1 -1
- data/lib/praxis/extensions/field_expansion.rb +3 -36
- data/lib/praxis/extensions/pagination.rb +2 -2
- data/lib/praxis/extensions/pagination/ordering_params.rb +1 -1
- data/lib/praxis/extensions/pagination/pagination_params.rb +6 -4
- data/lib/praxis/field_expander.rb +90 -0
- data/lib/praxis/finalizable.rb +34 -0
- data/lib/praxis/mapper/selector_generator.rb +1 -1
- data/lib/praxis/media_type.rb +3 -68
- data/lib/praxis/plugin_concern.rb +1 -1
- data/lib/praxis/plugins/mapper_plugin.rb +7 -7
- 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 +13 -63
- data/lib/praxis/tasks/routes.rb +1 -1
- 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 +4 -6
- data/spec/praxis/extensions/field_expansion_spec.rb +5 -24
- data/spec/praxis/field_expander_spec.rb +149 -0
- 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 +45 -285
- 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 +25 -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 +54 -0
- data/tasks/thor/templates/generator/example_app/app/models/user.rb +6 -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/user.rb +46 -0
- data/tasks/thor/templates/generator/example_app/config.ru +31 -0
- data/tasks/thor/templates/generator/example_app/config/environment.rb +40 -0
- data/tasks/thor/templates/generator/example_app/db/migrate/20201010101010_create_users_table.rb +11 -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 +26 -0
- data/tasks/thor/templates/generator/example_app/spec/helpers/database_helper.rb +18 -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
- metadata +49 -135
- 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
@@ -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
|
@@ -29,7 +29,7 @@ module Praxis
|
|
29
29
|
#
|
30
30
|
# def index(filters: nil, pagination: nil, order: nil, **_args)
|
31
31
|
# items = current_user.items.all
|
32
|
-
# items =
|
32
|
+
# items = _craft_pagination_query( query: items)
|
33
33
|
#
|
34
34
|
# display(items)
|
35
35
|
# end
|
@@ -76,7 +76,7 @@ module Praxis
|
|
76
76
|
# * the query to build from and the table
|
77
77
|
# * the request (for link header generation)
|
78
78
|
# * requires the _pagination variable to be there (set by this module) to return the pagination struct
|
79
|
-
def
|
79
|
+
def _craft_pagination_query(query:, type: :active_record)
|
80
80
|
handler_klass = \
|
81
81
|
case type
|
82
82
|
when :active_record
|
@@ -115,7 +115,7 @@ module Praxis
|
|
115
115
|
def self.construct(pagination_definition, **options)
|
116
116
|
return self if pagination_definition.nil?
|
117
117
|
|
118
|
-
DSLCompiler.new(self, options).parse(*pagination_definition)
|
118
|
+
DSLCompiler.new(self, **options).parse(*pagination_definition)
|
119
119
|
self
|
120
120
|
end
|
121
121
|
|
@@ -202,7 +202,7 @@ module Praxis
|
|
202
202
|
def self.construct(pagination_definition, **options)
|
203
203
|
return self if pagination_definition.nil?
|
204
204
|
|
205
|
-
DSLCompiler.new(self, options).parse(*pagination_definition)
|
205
|
+
DSLCompiler.new(self, **options).parse(*pagination_definition)
|
206
206
|
self
|
207
207
|
end
|
208
208
|
|
@@ -221,10 +221,11 @@ module Praxis
|
|
221
221
|
|
222
222
|
selectable = mt_example.object.keys & simple_attrs.keys
|
223
223
|
by = selectable.sample(1).first
|
224
|
-
from = media_type.attributes[by].example(parent: mt_example)
|
224
|
+
from = media_type.attributes[by].example(parent: mt_example).to_s
|
225
225
|
# Make sure to encode the value of the from, as it can contain commas and such
|
226
|
-
|
227
|
-
"
|
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]}"
|
228
229
|
else
|
229
230
|
"by=id,from=20,items=100"
|
230
231
|
end
|
@@ -360,6 +361,7 @@ module Praxis
|
|
360
361
|
else
|
361
362
|
s = "by=#{@by}"
|
362
363
|
s += ",from=#{@from}" if @from
|
364
|
+
s
|
363
365
|
end
|
364
366
|
str += ",items=#{items}" if @items
|
365
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
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module Praxis
|
3
|
+
module Finalizable
|
4
|
+
def self.extended(klass)
|
5
|
+
klass.module_eval do
|
6
|
+
@finalizable = Set.new
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def inherited(base)
|
11
|
+
@finalizable << base
|
12
|
+
base.instance_variable_set(:@finalizable, @finalizable)
|
13
|
+
base.instance_variable_set(:@finalized, false)
|
14
|
+
end
|
15
|
+
|
16
|
+
def finalizable
|
17
|
+
@finalizable
|
18
|
+
end
|
19
|
+
|
20
|
+
def finalized?
|
21
|
+
@finalized
|
22
|
+
end
|
23
|
+
|
24
|
+
def _finalize!
|
25
|
+
@finalized = true
|
26
|
+
end
|
27
|
+
|
28
|
+
def finalize!
|
29
|
+
finalizable.reject(&:finalized?).each(&:_finalize!)
|
30
|
+
|
31
|
+
finalize! unless finalizable.all?(&:finalized?)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -48,7 +48,7 @@ module Praxis::Mapper
|
|
48
48
|
new_fields_as_hash = association[:remote_key_columns].each_with_object({}) do|name, hash|
|
49
49
|
hash[name] = true
|
50
50
|
end
|
51
|
-
fields.merge
|
51
|
+
fields = fields.merge(new_fields_as_hash)
|
52
52
|
end
|
53
53
|
|
54
54
|
node.add(fields) unless fields == true
|
data/lib/praxis/media_type.rb
CHANGED
@@ -34,8 +34,9 @@ module Praxis
|
|
34
34
|
# description: 'The factory in which this widget was produced'
|
35
35
|
# end
|
36
36
|
#
|
37
|
-
# # All resources
|
38
|
-
#
|
37
|
+
# # All resources will be assigned a default_fieldset (with only non-blueprint attributes)
|
38
|
+
# # But one can explicitly tailor that by using the `default_fieldset` DSL
|
39
|
+
# default_fieldset do
|
39
40
|
# attribute :id
|
40
41
|
# attribute :color
|
41
42
|
# attribute :material
|
@@ -44,72 +45,6 @@ module Praxis
|
|
44
45
|
class MediaType < Praxis::Blueprint
|
45
46
|
|
46
47
|
include Types::MediaTypeCommon
|
47
|
-
|
48
|
-
class FieldResolver
|
49
|
-
def self.resolve(type,fields)
|
50
|
-
self.new.resolve(type,fields)
|
51
|
-
end
|
52
|
-
|
53
|
-
attr_reader :history
|
54
|
-
|
55
|
-
def initialize
|
56
|
-
@history = Hash.new do |hash,key|
|
57
|
-
hash[key] = Hash.new
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def resolve(type,fields)
|
62
|
-
history_key = fields
|
63
|
-
history_type = type
|
64
|
-
if fields.kind_of?(Array)
|
65
|
-
loop do
|
66
|
-
type = type.member_attribute.type
|
67
|
-
fields = fields.first
|
68
|
-
break unless fields.kind_of?(Array)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
return true if fields == true
|
73
|
-
|
74
|
-
if history[history_type].include? history_key
|
75
|
-
return history[history_type][history_key]
|
76
|
-
end
|
77
|
-
|
78
|
-
result = history[history_type][history_key] = {}
|
79
|
-
|
80
|
-
|
81
|
-
fields.each do |name, sub_fields|
|
82
|
-
|
83
|
-
new_type = type.attributes[name].type
|
84
|
-
result[name] = resolve(new_type, sub_fields)
|
85
|
-
end
|
86
|
-
|
87
|
-
result
|
88
|
-
end
|
89
|
-
|
90
|
-
# perform a deep recursive *in place* merge
|
91
|
-
# form all values in +source+ onto +target+
|
92
|
-
#
|
93
|
-
# note: can not use ActiveSupport's Hash#deep_merge! because it does not
|
94
|
-
# properly do a recursive `deep_merge!`, but instead does `deep_merge`,
|
95
|
-
# which destroys the self-referential behavior of field hashes.
|
96
|
-
#
|
97
|
-
# note: unlike Hash#merge, doesn't take a block.
|
98
|
-
def deep_merge(target, source)
|
99
|
-
source.each do |current_key, source_value|
|
100
|
-
target_value = target[current_key]
|
101
|
-
|
102
|
-
target[current_key] = if target_value.is_a?(Hash) && source_value.is_a?(Hash)
|
103
|
-
deep_merge(target_value, source_value)
|
104
|
-
else
|
105
|
-
source_value
|
106
|
-
end
|
107
|
-
end
|
108
|
-
target
|
109
|
-
end
|
110
|
-
|
111
|
-
end
|
112
|
-
|
113
48
|
end
|
114
49
|
|
115
50
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
require 'singleton'
|
2
|
-
require 'praxis/extensions/attribute_filtering'
|
3
2
|
|
4
|
-
module Praxis
|
3
|
+
module Praxis
|
5
4
|
module Plugins
|
6
5
|
module MapperPlugin
|
7
6
|
include Praxis::PluginConcern
|
@@ -36,20 +35,21 @@ module Praxis
|
|
36
35
|
return unless self.media_type.respond_to?(:domain_model) &&
|
37
36
|
self.media_type.domain_model < Praxis::Mapper::Resource
|
38
37
|
|
39
|
-
|
40
|
-
selector_generator.add(self.media_type.domain_model, resolved)
|
38
|
+
selector_generator.add(self.media_type.domain_model, self.expanded_fields)
|
41
39
|
end
|
42
40
|
|
43
|
-
def build_query(base_query) # rubocop:disable Metrics/AbcSize
|
41
|
+
def build_query(base_query, type: :active_record) # rubocop:disable Metrics/AbcSize
|
44
42
|
domain_model = self.media_type&.domain_model
|
45
43
|
raise "No domain model defined for #{self.name}. Cannot use the attribute filtering helpers without it" unless domain_model
|
46
44
|
|
47
45
|
filters = request.params.filters if request.params&.respond_to?(:filters)
|
46
|
+
# Handle filters
|
48
47
|
base_query = domain_model.craft_filter_query( base_query , filters: filters )
|
49
|
-
|
48
|
+
# Handle field and nested field selection
|
50
49
|
base_query = domain_model.craft_field_selection_query(base_query, selectors: selector_generator.selectors)
|
50
|
+
# handle pagination and ordering
|
51
|
+
base_query = _craft_pagination_query(query: base_query, type: type) if self.respond_to?(:_pagination)
|
51
52
|
|
52
|
-
# TODO: handle pagination and ordering
|
53
53
|
base_query
|
54
54
|
end
|
55
55
|
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Praxis
|
4
|
+
class Renderer
|
5
|
+
attr_reader :include_nil
|
6
|
+
attr_reader :cache
|
7
|
+
|
8
|
+
class CircularRenderingError < StandardError
|
9
|
+
attr_reader :object
|
10
|
+
attr_reader :context
|
11
|
+
|
12
|
+
def initialize(object, context)
|
13
|
+
@object = object
|
14
|
+
@context = context
|
15
|
+
|
16
|
+
first = Attributor.humanize_context(context[0..10])
|
17
|
+
last = Attributor.humanize_context(context[-5..-1])
|
18
|
+
pretty_context = "#{first}...#{last}"
|
19
|
+
super("SystemStackError in rendering #{object.class} with context: #{pretty_context}")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(include_nil: false)
|
24
|
+
@cache = Hash.new do |hash, key|
|
25
|
+
hash[key] = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
@include_nil = include_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
# Renders an object using a given list of fields.
|
32
|
+
#
|
33
|
+
# @param [Object] object the object to render
|
34
|
+
# @param [Hash] fields the correct set of fields, as from FieldExpander
|
35
|
+
def render(object, fields, context: Attributor::DEFAULT_ROOT_CONTEXT)
|
36
|
+
if object.is_a? Praxis::Blueprint
|
37
|
+
@cache[object._cache_key][fields] ||= _render(object, fields, context: context)
|
38
|
+
else
|
39
|
+
if object.class < Attributor::Collection
|
40
|
+
object.each_with_index.collect do |sub_object, i|
|
41
|
+
sub_context = context + ["at(#{i})"]
|
42
|
+
render(sub_object, fields, context: sub_context)
|
43
|
+
end
|
44
|
+
else
|
45
|
+
_render(object, fields, context: context)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
rescue SystemStackError
|
49
|
+
raise CircularRenderingError.new(object, context)
|
50
|
+
end
|
51
|
+
|
52
|
+
def _render(object, fields, context: Attributor::DEFAULT_ROOT_CONTEXT)
|
53
|
+
if fields == true
|
54
|
+
return case object
|
55
|
+
when Attributor::Dumpable
|
56
|
+
object.dump
|
57
|
+
else
|
58
|
+
object
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
fields.each_with_object({}) do |(key, subfields), hash|
|
63
|
+
begin
|
64
|
+
value = object._get_attr(key)
|
65
|
+
rescue => e
|
66
|
+
raise Attributor::DumpError.new(context: context, name: key, type: object.class, original_exception: e)
|
67
|
+
end
|
68
|
+
|
69
|
+
if value.nil?
|
70
|
+
hash[key] = nil if self.include_nil
|
71
|
+
next
|
72
|
+
end
|
73
|
+
|
74
|
+
if subfields == true
|
75
|
+
hash[key] = case value
|
76
|
+
when Attributor::Dumpable
|
77
|
+
value.dump
|
78
|
+
else
|
79
|
+
value
|
80
|
+
end
|
81
|
+
else
|
82
|
+
new_context = context + [key]
|
83
|
+
hash[key] = render(value, subfields, context: new_context)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|