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.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/CHANGELOG.md +11 -0
  4. data/TODO.md +1 -4
  5. data/bin/praxis +11 -13
  6. data/lib/praxis.rb +10 -3
  7. data/lib/praxis/action_definition.rb +15 -13
  8. data/lib/praxis/action_definition/headers_dsl_compiler.rb +0 -7
  9. data/lib/praxis/api_general_info.rb +1 -1
  10. data/lib/praxis/application.rb +6 -2
  11. data/lib/praxis/blueprint.rb +357 -0
  12. data/lib/praxis/bootloader.rb +9 -3
  13. data/lib/praxis/bootloader_stages/environment.rb +15 -13
  14. data/lib/praxis/collection.rb +1 -11
  15. data/lib/praxis/config_hash.rb +44 -0
  16. data/lib/praxis/docs/{openapi → open_api}/info_object.rb +0 -0
  17. data/lib/praxis/docs/{openapi → open_api}/media_type_object.rb +0 -0
  18. data/lib/praxis/docs/{openapi → open_api}/operation_object.rb +0 -0
  19. data/lib/praxis/docs/{openapi → open_api}/parameter_object.rb +0 -0
  20. data/lib/praxis/docs/{openapi → open_api}/paths_object.rb +0 -0
  21. data/lib/praxis/docs/{openapi → open_api}/request_body_object.rb +0 -0
  22. data/lib/praxis/docs/{openapi → open_api}/response_object.rb +0 -0
  23. data/lib/praxis/docs/{openapi → open_api}/responses_object.rb +0 -0
  24. data/lib/praxis/docs/{openapi → open_api}/schema_object.rb +0 -0
  25. data/lib/praxis/docs/{openapi → open_api}/server_object.rb +0 -0
  26. data/lib/praxis/docs/{openapi → open_api}/tag_object.rb +0 -0
  27. data/lib/praxis/docs/open_api_generator.rb +90 -5
  28. data/lib/praxis/endpoint_definition.rb +273 -0
  29. data/lib/praxis/extensions/attribute_filtering/active_record_filter_query_builder.rb +1 -1
  30. data/lib/praxis/extensions/attribute_filtering/filtering_params.rb +1 -1
  31. data/lib/praxis/extensions/field_expansion.rb +3 -36
  32. data/lib/praxis/extensions/pagination.rb +2 -2
  33. data/lib/praxis/extensions/pagination/ordering_params.rb +1 -1
  34. data/lib/praxis/extensions/pagination/pagination_params.rb +6 -4
  35. data/lib/praxis/field_expander.rb +90 -0
  36. data/lib/praxis/finalizable.rb +34 -0
  37. data/lib/praxis/mapper/selector_generator.rb +1 -1
  38. data/lib/praxis/media_type.rb +3 -68
  39. data/lib/praxis/plugin_concern.rb +1 -1
  40. data/lib/praxis/plugins/mapper_plugin.rb +7 -7
  41. data/lib/praxis/renderer.rb +88 -0
  42. data/lib/praxis/request.rb +1 -1
  43. data/lib/praxis/resource_definition.rb +2 -311
  44. data/lib/praxis/response_definition.rb +2 -10
  45. data/lib/praxis/response_template.rb +3 -3
  46. data/lib/praxis/router.rb +2 -2
  47. data/lib/praxis/routing_config.rb +1 -1
  48. data/lib/praxis/tasks/api_docs.rb +13 -63
  49. data/lib/praxis/tasks/routes.rb +1 -1
  50. data/lib/praxis/types/media_type_common.rb +1 -11
  51. data/lib/praxis/version.rb +1 -1
  52. data/praxis.gemspec +0 -1
  53. data/spec/functional_spec.rb +5 -9
  54. data/spec/praxis/action_definition_spec.rb +12 -20
  55. data/spec/praxis/blueprint_spec.rb +373 -0
  56. data/spec/praxis/bootloader_spec.rb +10 -2
  57. data/spec/praxis/collection_spec.rb +0 -13
  58. data/spec/praxis/config_hash_spec.rb +64 -0
  59. data/spec/praxis/{resource_definition_spec.rb → endpoint_definition_spec.rb} +37 -64
  60. data/spec/praxis/extensions/attribute_filtering/active_record_filter_query_builder_spec.rb +4 -6
  61. data/spec/praxis/extensions/field_expansion_spec.rb +5 -24
  62. data/spec/praxis/field_expander_spec.rb +149 -0
  63. data/spec/praxis/media_type_identifier_spec.rb +5 -4
  64. data/spec/praxis/media_type_spec.rb +4 -93
  65. data/spec/praxis/renderer_spec.rb +188 -0
  66. data/spec/praxis/response_definition_spec.rb +0 -31
  67. data/spec/praxis/response_spec.rb +1 -1
  68. data/spec/praxis/router_spec.rb +8 -8
  69. data/spec/praxis/routing_config_spec.rb +3 -3
  70. data/spec/spec_app/app/controllers/instances.rb +13 -7
  71. data/spec/spec_app/design/media_types/instance.rb +1 -19
  72. data/spec/spec_app/design/media_types/volume.rb +1 -1
  73. data/spec/spec_app/design/media_types/volume_snapshot.rb +2 -14
  74. data/spec/spec_app/design/resources/instances.rb +5 -8
  75. data/spec/spec_app/design/resources/volume_snapshots.rb +1 -1
  76. data/spec/spec_app/design/resources/volumes.rb +1 -1
  77. data/spec/support/spec_authorization_plugin.rb +1 -1
  78. data/spec/support/spec_blueprints.rb +72 -0
  79. data/spec/support/{spec_resource_definitions.rb → spec_endpoint_definitions.rb} +2 -2
  80. data/spec/support/spec_media_types.rb +6 -26
  81. data/tasks/thor/app.rb +8 -34
  82. data/tasks/thor/example.rb +45 -285
  83. data/tasks/thor/templates/generator/empty_app/.gitignore +0 -1
  84. data/tasks/thor/templates/generator/empty_app/Gemfile +7 -23
  85. data/tasks/thor/templates/generator/empty_app/README.md +1 -1
  86. data/tasks/thor/templates/generator/empty_app/Rakefile +4 -13
  87. data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.empty_directory +0 -0
  88. data/tasks/thor/templates/generator/empty_app/{design/response_templates → app/v1/resources}/.gitkeep +0 -0
  89. data/tasks/thor/templates/generator/empty_app/config/environment.rb +25 -17
  90. data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.empty_directory +0 -0
  91. data/tasks/thor/templates/generator/empty_app/{design/v1/resources → config/initializers}/.gitkeep +0 -0
  92. data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.empty_directory +0 -0
  93. data/tasks/thor/templates/generator/empty_app/design/v1/endpoints/.gitkeep +0 -0
  94. data/tasks/thor/templates/generator/empty_app/docs/.empty_directory +0 -0
  95. data/tasks/thor/templates/generator/empty_app/docs/.gitkeep +0 -0
  96. data/tasks/thor/templates/generator/empty_app/spec/spec_helper.rb +14 -9
  97. data/tasks/thor/templates/generator/example_app/.gitignore +1 -0
  98. data/tasks/thor/templates/generator/example_app/Gemfile +19 -0
  99. data/tasks/thor/templates/generator/example_app/Rakefile +54 -0
  100. data/tasks/thor/templates/generator/example_app/app/models/user.rb +6 -0
  101. data/tasks/thor/templates/generator/example_app/app/v1/controllers/users.rb +17 -0
  102. data/tasks/thor/templates/generator/example_app/app/v1/resources/user.rb +46 -0
  103. data/tasks/thor/templates/generator/example_app/config.ru +31 -0
  104. data/tasks/thor/templates/generator/example_app/config/environment.rb +40 -0
  105. data/tasks/thor/templates/generator/example_app/db/migrate/20201010101010_create_users_table.rb +11 -0
  106. data/tasks/thor/templates/generator/example_app/design/api.rb +18 -0
  107. data/tasks/thor/templates/generator/example_app/design/v1/endpoints/users.rb +37 -0
  108. data/tasks/thor/templates/generator/example_app/design/v1/media_types/user.rb +26 -0
  109. data/tasks/thor/templates/generator/example_app/spec/helpers/database_helper.rb +18 -0
  110. data/tasks/thor/templates/generator/example_app/spec/spec_helper.rb +42 -0
  111. data/tasks/thor/templates/generator/example_app/spec/v1/controllers/users_spec.rb +37 -0
  112. metadata +49 -135
  113. data/lib/api_browser/.bowerrc +0 -3
  114. data/lib/api_browser/.editorconfig +0 -21
  115. data/lib/api_browser/Gruntfile.js +0 -581
  116. data/lib/api_browser/app/index.html +0 -59
  117. data/lib/api_browser/app/js/app.js +0 -48
  118. data/lib/api_browser/app/js/controllers/action.js +0 -47
  119. data/lib/api_browser/app/js/controllers/controller.js +0 -10
  120. data/lib/api_browser/app/js/controllers/menu.js +0 -93
  121. data/lib/api_browser/app/js/controllers/trait.js +0 -10
  122. data/lib/api_browser/app/js/controllers/type.js +0 -24
  123. data/lib/api_browser/app/js/directives/attribute_description.js +0 -56
  124. data/lib/api_browser/app/js/directives/attribute_table.js +0 -28
  125. data/lib/api_browser/app/js/directives/conditional_requirements.js +0 -13
  126. data/lib/api_browser/app/js/directives/fixed_if_fits.js +0 -38
  127. data/lib/api_browser/app/js/directives/highlight.js +0 -14
  128. data/lib/api_browser/app/js/directives/menu_item.js +0 -59
  129. data/lib/api_browser/app/js/directives/no_container.js +0 -8
  130. data/lib/api_browser/app/js/directives/readable_list.js +0 -87
  131. data/lib/api_browser/app/js/directives/request_examples.js +0 -31
  132. data/lib/api_browser/app/js/directives/type_placeholder.js +0 -30
  133. data/lib/api_browser/app/js/directives/url.js +0 -15
  134. data/lib/api_browser/app/js/factories/Configuration.js +0 -12
  135. data/lib/api_browser/app/js/factories/Documentation.js +0 -61
  136. data/lib/api_browser/app/js/factories/Example.js +0 -51
  137. data/lib/api_browser/app/js/factories/PageInfo.js +0 -9
  138. data/lib/api_browser/app/js/factories/normalize_attributes.js +0 -20
  139. data/lib/api_browser/app/js/factories/prepare_template.js +0 -15
  140. data/lib/api_browser/app/js/factories/template_for.js +0 -128
  141. data/lib/api_browser/app/js/filters/attribute_name.js +0 -10
  142. data/lib/api_browser/app/js/filters/friendly_json.js +0 -5
  143. data/lib/api_browser/app/js/filters/has_requirement.js +0 -14
  144. data/lib/api_browser/app/js/filters/header_info.js +0 -9
  145. data/lib/api_browser/app/js/filters/is_empty.js +0 -8
  146. data/lib/api_browser/app/js/filters/markdown.js +0 -6
  147. data/lib/api_browser/app/js/filters/resource_name.js +0 -5
  148. data/lib/api_browser/app/js/filters/tag_requirement.js +0 -13
  149. data/lib/api_browser/app/sass/modules/_body.scss +0 -40
  150. data/lib/api_browser/app/sass/modules/_cloke.scss +0 -8
  151. data/lib/api_browser/app/sass/modules/_header.scss +0 -10
  152. data/lib/api_browser/app/sass/modules/_nav.scss +0 -7
  153. data/lib/api_browser/app/sass/modules/_sidebar.scss +0 -134
  154. data/lib/api_browser/app/sass/modules/_switch.scss +0 -55
  155. data/lib/api_browser/app/sass/modules/_table.scss +0 -13
  156. data/lib/api_browser/app/sass/praxis.scss +0 -70
  157. data/lib/api_browser/app/sass/variables/_bootstrap-variables.scss +0 -774
  158. data/lib/api_browser/app/views/action.html +0 -97
  159. data/lib/api_browser/app/views/builtin/field-selector.html +0 -24
  160. data/lib/api_browser/app/views/controller.html +0 -55
  161. data/lib/api_browser/app/views/directives/attribute_description.html +0 -2
  162. data/lib/api_browser/app/views/directives/attribute_description/default.html +0 -2
  163. data/lib/api_browser/app/views/directives/attribute_description/example.html +0 -13
  164. data/lib/api_browser/app/views/directives/attribute_description/headers.html +0 -8
  165. data/lib/api_browser/app/views/directives/attribute_description/member_options.html +0 -4
  166. data/lib/api_browser/app/views/directives/attribute_description/values.html +0 -14
  167. data/lib/api_browser/app/views/directives/attribute_table.html +0 -17
  168. data/lib/api_browser/app/views/directives/menu_item.html +0 -8
  169. data/lib/api_browser/app/views/directives/url.html +0 -3
  170. data/lib/api_browser/app/views/examples/general.html +0 -26
  171. data/lib/api_browser/app/views/home.html +0 -5
  172. data/lib/api_browser/app/views/layout.html +0 -8
  173. data/lib/api_browser/app/views/menu.html +0 -42
  174. data/lib/api_browser/app/views/navbar.html +0 -9
  175. data/lib/api_browser/app/views/trait.html +0 -13
  176. data/lib/api_browser/app/views/type.html +0 -6
  177. data/lib/api_browser/app/views/type/details.html +0 -33
  178. data/lib/api_browser/app/views/types/embedded/array.html +0 -2
  179. data/lib/api_browser/app/views/types/embedded/default.html +0 -12
  180. data/lib/api_browser/app/views/types/embedded/field-selector.html +0 -13
  181. data/lib/api_browser/app/views/types/embedded/links.html +0 -11
  182. data/lib/api_browser/app/views/types/embedded/requirements.html +0 -6
  183. data/lib/api_browser/app/views/types/embedded/single_req.html +0 -9
  184. data/lib/api_browser/app/views/types/embedded/struct.html +0 -14
  185. data/lib/api_browser/app/views/types/label/link.html +0 -1
  186. data/lib/api_browser/app/views/types/label/primitive.html +0 -1
  187. data/lib/api_browser/app/views/types/label/primitive_collection.html +0 -1
  188. data/lib/api_browser/app/views/types/label/type.html +0 -1
  189. data/lib/api_browser/app/views/types/label/type_collection.html +0 -1
  190. data/lib/api_browser/app/views/types/main/array.html +0 -22
  191. data/lib/api_browser/app/views/types/main/default.html +0 -23
  192. data/lib/api_browser/app/views/types/main/hash.html +0 -23
  193. data/lib/api_browser/app/views/types/standalone/array.html +0 -3
  194. data/lib/api_browser/app/views/types/standalone/default.html +0 -18
  195. data/lib/api_browser/app/views/types/standalone/struct.html +0 -2
  196. data/lib/api_browser/bower_template.json +0 -41
  197. data/lib/api_browser/package-lock.json +0 -7110
  198. data/lib/api_browser/package.json +0 -43
  199. data/lib/praxis/docs/generator.rb +0 -243
  200. data/lib/praxis/docs/link_builder.rb +0 -30
  201. data/lib/praxis/links.rb +0 -135
  202. data/lib/praxis/types/multipart.rb +0 -109
  203. data/spec/api_browser/directives/type_placeholder_spec.js +0 -134
  204. data/spec/api_browser/factories/configuration_spec.js +0 -32
  205. data/spec/api_browser/factories/documentation_spec.js +0 -100
  206. data/spec/api_browser/factories/normalize_attributes_spec.js +0 -92
  207. data/spec/api_browser/factories/template_for_spec.js +0 -67
  208. data/spec/api_browser/filters/attribute_name_spec.js +0 -23
  209. data/spec/praxis/types/multipart_spec.rb +0 -112
  210. data/tasks/thor/templates/generator/empty_app/.rspec +0 -1
  211. data/tasks/thor/templates/generator/empty_app/Guardfile +0 -3
  212. data/tasks/thor/templates/generator/empty_app/config/rainbows.rb +0 -57
  213. data/tasks/thor/templates/generator/empty_app/docs/app.js +0 -1
  214. data/tasks/thor/templates/generator/empty_app/docs/styles.scss +0 -3
@@ -44,7 +44,11 @@ describe Praxis::Bootloader do
44
44
  expect(bootloader.before(stage.name)).to eq('before!')
45
45
  end
46
46
 
47
- it "raises when given an invalid stage name"
47
+ it "raises when given an invalid stage name" do
48
+ expect{
49
+ bootloader.before('nope!')
50
+ }.to raise_error(Praxis::Exceptions::StageNotFound,/Error running a before block for stage nope!/)
51
+ end
48
52
  end
49
53
 
50
54
  context ".after" do
@@ -54,7 +58,11 @@ describe Praxis::Bootloader do
54
58
  expect(bootloader.after(stage.name)).to eq('after!')
55
59
  end
56
60
 
57
- it "raises when given an invalid stage name"
61
+ it "raises when given an invalid stage name" do
62
+ expect{
63
+ bootloader.after('nope!')
64
+ }.to raise_error(Praxis::Exceptions::StageNotFound,/Error running an after block for stage nope!/)
65
+ end
58
66
  end
59
67
 
60
68
  context ".use" do
@@ -51,19 +51,6 @@ describe Praxis::Collection do
51
51
  its(:identifier) { should eq Person.identifier + "; type=collection" }
52
52
  end
53
53
 
54
- context '.views' do
55
- subject(:views) { collection.views }
56
- its(:keys) { should match_array(member_type.views.keys)}
57
-
58
- it 'generates CollectionViews from the member views' do
59
- collection.views.each do |name, view|
60
- expect(view.name).to be name
61
- expect(view.schema).to be member_type
62
- expect(view.contents).to eq member_type.views[name].contents
63
- end
64
- end
65
- end
66
-
67
54
  context '.load' do
68
55
  let(:volume_data) do
69
56
  {
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
+
4
+ describe Praxis::ConfigHash do
5
+ subject(:instance) { Praxis::ConfigHash.new(hash, &block) }
6
+ let(:hash) { { one: ['existing'], two: 'dos' } }
7
+ let(:block) do
8
+ proc { 'abc' }
9
+ end
10
+
11
+ context 'initialization' do
12
+ it 'saves the passed hash' do
13
+ expect(subject.hash).to be(hash)
14
+ end
15
+ end
16
+
17
+ context '.from' do
18
+ subject(:instance) { Praxis::ConfigHash.from(hash, &block) }
19
+ it 'returns an instance' do
20
+ expect(subject).to be_kind_of(Praxis::ConfigHash)
21
+ expect(subject.hash).to be(hash)
22
+ end
23
+ end
24
+
25
+ context '#to_hash' do
26
+ let(:block) do
27
+ proc { hash['i_was'] = 'here' }
28
+ end
29
+ it 'evaluates the block and returns the resulting hash' do
30
+ expect(subject.to_hash).to eq(subject.hash)
31
+ expect(subject.hash['i_was']).to eq('here')
32
+ end
33
+ end
34
+
35
+ context '#method_missing' do
36
+ context 'when keys do not exist in the hash key' do
37
+ it 'sets a single value to the hash' do
38
+ subject.some_name 'someval'
39
+ expect(subject.hash[:some_name]).to eq('someval')
40
+ end
41
+ it 'sets a multiple values to the hash key' do
42
+ subject.some_name 'someval', 'other1', 'other2'
43
+ expect(subject.hash[:some_name]).to include('someval', 'other1', 'other2')
44
+ end
45
+ end
46
+ context 'when keys already exist in the hash key' do
47
+ it 'adds one value to the hash' do
48
+ subject.one'newval'
49
+ expect(subject.hash[:one]).to match_array(%w(existing newval))
50
+ end
51
+ it 'adds multiple values to the hash key' do
52
+ subject.one 'newval', 'other1', 'other2'
53
+ expect(subject.hash[:one]).to match_array(%w(existing newval other1 other2))
54
+ end
55
+ context 'when passing a value and a block' do
56
+ let(:my_block) { proc {} }
57
+ it 'adds the tuple to the hash key' do
58
+ subject.one 'val', &my_block
59
+ expect(subject.hash[:one]).to include(['val', my_block])
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Praxis::ResourceDefinition do
4
- subject(:resource_definition) { PeopleResource }
3
+ describe Praxis::EndpointDefinition do
4
+ subject(:endpoint_definition) { PeopleResource }
5
5
 
6
6
  its(:description) { should eq('People resource') }
7
7
  its(:media_type) { should eq(Person) }
@@ -14,10 +14,10 @@ describe Praxis::ResourceDefinition do
14
14
  its(:metadata) { should_not have_key(:doc_visibility) }
15
15
 
16
16
  context '.describe' do
17
- subject(:describe) { resource_definition.describe }
17
+ subject(:describe) { endpoint_definition.describe }
18
18
 
19
- its([:description]) { should eq(resource_definition.description) }
20
- its([:media_type]) { should eq(resource_definition.media_type.describe(true)) }
19
+ its([:description]) { should eq(endpoint_definition.description) }
20
+ its([:media_type]) { should eq(endpoint_definition.media_type.describe(true)) }
21
21
 
22
22
  its([:actions]) { should have(2).items }
23
23
  its([:metadata]) { should be_kind_of(Hash) }
@@ -25,7 +25,7 @@ describe Praxis::ResourceDefinition do
25
25
  it { should_not have_key(:parent)}
26
26
 
27
27
  context 'for a resource with a parent' do
28
- let(:resource_definition) { ApiResources::VolumeSnapshots}
28
+ let(:endpoint_definition) { ApiResources::VolumeSnapshots}
29
29
 
30
30
  its([:parent]) { should eq ApiResources::Volumes.id }
31
31
  end
@@ -34,29 +34,29 @@ describe Praxis::ResourceDefinition do
34
34
 
35
35
 
36
36
  context '.routing_prefix' do
37
- subject(:resource_definition) { ApiResources::VolumeSnapshots }
37
+ subject(:endpoint_definition) { ApiResources::VolumeSnapshots }
38
38
  it do
39
- expect(resource_definition.routing_prefix).to eq('/clouds/:cloud_id/volumes/:volume_id/snapshots')
39
+ expect(endpoint_definition.routing_prefix).to eq('/clouds/:cloud_id/volumes/:volume_id/snapshots')
40
40
  end
41
41
  end
42
42
 
43
43
  context '.parent_prefix' do
44
- subject(:resource_definition) { ApiResources::VolumeSnapshots }
44
+ subject(:endpoint_definition) { ApiResources::VolumeSnapshots }
45
45
  let(:base_path){ Praxis::ApiDefinition.instance.info.base_path }
46
46
  its(:parent_prefix){ should eq('/clouds/:cloud_id/volumes/:volume_id') }
47
47
  it do
48
- expect(resource_definition.parent_prefix).to_not match(/^#{base_path}/)
48
+ expect(endpoint_definition.parent_prefix).to_not match(/^#{base_path}/)
49
49
  end
50
50
  end
51
51
 
52
52
 
53
53
  context '.action' do
54
54
  it 'requires a block' do
55
- expect { resource_definition.action(:something)
55
+ expect { endpoint_definition.action(:something)
56
56
  }.to raise_error(ArgumentError)
57
57
  end
58
58
  it 'creates an ActionDefinition for actions' do
59
- index = resource_definition.actions[:index]
59
+ index = endpoint_definition.actions[:index]
60
60
  expect(index).to be_kind_of(Praxis::ActionDefinition)
61
61
  expect(index.description).to eq("index description")
62
62
  end
@@ -64,7 +64,7 @@ describe Praxis::ResourceDefinition do
64
64
  it 'complains if action names are not symbols' do
65
65
  expect do
66
66
  Class.new do
67
- include Praxis::ResourceDefinition
67
+ include Praxis::EndpointDefinition
68
68
  action "foo" do
69
69
  end
70
70
  end
@@ -73,9 +73,9 @@ describe Praxis::ResourceDefinition do
73
73
  end
74
74
 
75
75
  context 'action_defaults' do
76
- let(:resource_definition) do
76
+ let(:endpoint_definition) do
77
77
  Class.new do
78
- include Praxis::ResourceDefinition
78
+ include Praxis::EndpointDefinition
79
79
  media_type Person
80
80
 
81
81
  version '1.0'
@@ -135,13 +135,13 @@ describe Praxis::ResourceDefinition do
135
135
  end
136
136
 
137
137
  it 'are applied to actions' do
138
- action = resource_definition.actions[:show]
138
+ action = endpoint_definition.actions[:show]
139
139
  expect(action.params.attributes).to have_key(:id)
140
140
  expect(action.route.path.to_s).to eq '/api/:base_param/people/:id'
141
141
  end
142
142
 
143
143
  context 'includes base_params from the APIDefinition' do
144
- let(:show_action_params){ resource_definition.actions[:show].params }
144
+ let(:show_action_params){ endpoint_definition.actions[:show].params }
145
145
 
146
146
  it 'including globally defined' do
147
147
  expect(show_action_params.attributes).to have_key(:base_param)
@@ -159,15 +159,15 @@ describe Praxis::ResourceDefinition do
159
159
  end
160
160
 
161
161
  context 'setting other values' do
162
- subject(:resource_definition) { Class.new {include Praxis::ResourceDefinition } }
162
+ subject(:endpoint_definition) { Class.new {include Praxis::EndpointDefinition } }
163
163
 
164
164
  let(:some_proc) { Proc.new {} }
165
165
  let(:some_hash) { Hash.new }
166
166
 
167
167
  it 'accepts a string as media_type' do
168
- resource_definition.media_type('Something')
169
- expect(resource_definition.media_type).to be_kind_of(Praxis::SimpleMediaType)
170
- expect(resource_definition.media_type.identifier).to eq('Something')
168
+ endpoint_definition.media_type('Something')
169
+ expect(endpoint_definition.media_type).to be_kind_of(Praxis::SimpleMediaType)
170
+ expect(endpoint_definition.media_type.identifier).to eq('Something')
171
171
  end
172
172
 
173
173
  its(:version_options){ should be_kind_of(Hash) }
@@ -176,57 +176,30 @@ describe Praxis::ResourceDefinition do
176
176
 
177
177
 
178
178
  context '.trait' do
179
- subject(:resource_definition) { Class.new {include Praxis::ResourceDefinition } }
179
+ subject(:endpoint_definition) { Class.new {include Praxis::EndpointDefinition } }
180
180
  it 'raises an error for missing traits' do
181
- expect { resource_definition.use(:stuff) }.to raise_error(Praxis::Exceptions::InvalidTrait)
181
+ expect { endpoint_definition.trait(:stuff) }.to raise_error(Praxis::Exceptions::InvalidTrait)
182
182
  end
183
- it 'has a spec for actually using a trait'
184
- end
185
-
186
-
187
- context 'deprecated action methods' do
188
- subject(:resource_definition) do
189
- Class.new do
190
- include Praxis::ResourceDefinition
191
-
192
- def self.name
193
- 'FooBar'
194
- end
183
+ it 'adds it to its list when it is available in the APIDefinition instance' do
184
+ trait_name = :test
185
+ expect(endpoint_definition.traits).to_not include(trait_name)
195
186
 
196
- silence_warnings do
197
- payload { attribute :inherited_payload, String }
198
- headers { key "Inherited-Header", String }
199
- params { attribute :inherited_params, String }
200
- response :not_found
201
- end
202
-
203
- action :index do
204
- end
205
- end
187
+ endpoint_definition.trait(trait_name)
188
+ expect(endpoint_definition.traits).to include(trait_name)
206
189
  end
207
-
208
- let(:action) { resource_definition.actions[:index] }
209
-
210
- it 'are applied to the action' do
211
- expect(action.payload.attributes).to have_key(:inherited_payload)
212
- expect(action.headers.attributes).to have_key("Inherited-Header")
213
- expect(action.params.attributes).to have_key(:inherited_params)
214
- expect(action.responses).to have_key(:not_found)
215
- end
216
-
217
190
  end
218
191
 
219
192
  context 'with nodoc! called' do
220
193
  before do
221
- resource_definition.nodoc!
194
+ endpoint_definition.nodoc!
222
195
  end
223
196
 
224
197
  it 'has the :doc_visibility option set' do
225
- expect(resource_definition.metadata[:doc_visibility]).to be(:none)
198
+ expect(endpoint_definition.metadata[:doc_visibility]).to be(:none)
226
199
  end
227
200
 
228
201
  it 'is exposed in the describe' do
229
- expect(resource_definition.describe[:metadata][:doc_visibility]).to be(:none)
202
+ expect(endpoint_definition.describe[:metadata][:doc_visibility]).to be(:none)
230
203
  end
231
204
 
232
205
  end
@@ -238,14 +211,14 @@ describe Praxis::ResourceDefinition do
238
211
  end
239
212
  it 'cannot be done if already been defined' do
240
213
  expect{
241
- resource_definition.canonical_path :reset
214
+ endpoint_definition.canonical_path :reset
242
215
  }.to raise_error(/'canonical_path' can only be defined once./)
243
216
  end
244
217
  end
245
218
  context 'if none specified' do
246
- subject(:resource_definition) do
219
+ subject(:endpoint_definition) do
247
220
  Class.new do
248
- include Praxis::ResourceDefinition
221
+ include Praxis::EndpointDefinition
249
222
  action :show do
250
223
  end
251
224
  end
@@ -255,9 +228,9 @@ describe Praxis::ResourceDefinition do
255
228
  end
256
229
  end
257
230
  context 'with an undefined action' do
258
- subject(:resource_definition) do
231
+ subject(:endpoint_definition) do
259
232
  Class.new do
260
- include Praxis::ResourceDefinition
233
+ include Praxis::EndpointDefinition
261
234
  canonical_path :non_existent
262
235
  end
263
236
  end
@@ -275,7 +248,7 @@ describe Praxis::ResourceDefinition do
275
248
  end
276
249
  end
277
250
  context '#parse_href' do
278
- let(:parsed){ resource_definition.parse_href("/people/1") }
251
+ let(:parsed){ endpoint_definition.parse_href("/people/1") }
279
252
  it 'accesses the path expansion functions of the primary route' do
280
253
  expect(parsed).to have_key(:id)
281
254
  end
@@ -220,16 +220,14 @@ describe Praxis::Extensions::AttributeFiltering::ActiveRecordFilterQueryBuilder
220
220
 
221
221
  context 'uses fully qualified names for conditions (disambiguate fields)' do
222
222
  context 'when we have a join table condition that has the same field' do
223
- COMMON_SQL_PREFIX = <<~SQL
224
- SELECT "active_books".* FROM "active_books"
225
- INNER JOIN "active_categories" ON "active_categories"."uuid" = "active_books"."category_uuid"
226
- INNER JOIN "active_books" "#{PREF}/category/books" ON "#{PREF}/category/books"."category_uuid" = "active_categories"."uuid"
227
- SQL
228
223
  let(:filters_string) { 'name=Book1&category.books.name=Book3' }
229
224
  it_behaves_like 'subject_equivalent_to', ActiveBook.joins(category: :books)
230
225
  .where('simple_name': 'Book1')
231
226
  .where('books_active_categories.simple_name': 'Book3')
232
- it_behaves_like 'subject_matches_sql', COMMON_SQL_PREFIX + <<~SQL
227
+ it_behaves_like 'subject_matches_sql', <<~SQL
228
+ SELECT "active_books".* FROM "active_books"
229
+ INNER JOIN "active_categories" ON "active_categories"."uuid" = "active_books"."category_uuid"
230
+ INNER JOIN "active_books" "#{PREF}/category/books" ON "#{PREF}/category/books"."category_uuid" = "active_categories"."uuid"
233
231
  WHERE ("#{PREF}/category/books"."simple_name" = 'Book3')
234
232
  AND ("active_books"."simple_name" = 'Book1')
235
233
  SQL
@@ -24,8 +24,7 @@ describe Praxis::Extensions::FieldExpansion do
24
24
 
25
25
  let(:request_params) do
26
26
  double('params',
27
- fields: Praxis::Extensions::FieldSelection::FieldSelector.for(Person).load(fields),
28
- view: view
27
+ fields: Praxis::Extensions::FieldSelection::FieldSelector.for(Person).load(fields)
29
28
  )
30
29
  end
31
30
 
@@ -33,7 +32,6 @@ describe Praxis::Extensions::FieldExpansion do
33
32
  let(:media_type) { Person }
34
33
 
35
34
  let(:fields) { nil }
36
- let(:view) { nil }
37
35
 
38
36
  let(:test_attributes) { }
39
37
  let(:test_params) { double('test_params', attributes: test_attributes) }
@@ -43,43 +41,26 @@ describe Praxis::Extensions::FieldExpansion do
43
41
  context '#expanded_fields' do
44
42
 
45
43
  context 'with fields and view params defined' do
46
- let(:test_attributes) { {view: true, fields: true} }
44
+ let(:test_attributes) { {} }
47
45
 
48
- context 'and no fields provided' do
46
+ context 'with no fields provided' do
49
47
  it 'returns the fields for the default view' do
50
48
  expect(expansion).to eq({id: true, name: true})
51
49
  end
52
-
53
- pending 'and a view'
54
50
  end
55
51
 
56
52
  context 'with a set of fields provided' do
57
53
  let(:fields) { 'id,name,owner{name}' }
58
- it 'returns the subset of fields for the default view' do
54
+ it 'returns the subset of fields' do
59
55
  expected = {id: true, name: true }
60
56
  expect(expansion).to eq expected
61
57
  end
62
-
63
- pending 'and a view'
64
58
  end
65
59
  end
66
60
 
67
- context 'with only a view param defined' do
68
- let(:test_attributes) { {view: true} }
69
-
70
- it 'returns the fields for the default view' do
71
- expect(expansion).to eq({id: true, name: true})
72
- end
73
-
74
- pending 'and a view'
75
- end
76
-
77
-
78
61
  context 'with an action with no params' do
79
62
  let(:test_params) { nil }
80
- let(:fields){ nil }
81
- let(:view){ nil }
82
- it 'ignores incoming parameters and expands for the default view' do
63
+ it 'ignores incoming parameters and expands for the default fieldset' do
83
64
  expect(expansion).to eq({id: true, name: true})
84
65
  end
85
66
  end
@@ -0,0 +1,149 @@
1
+ # frozen_string_literal: true
2
+ require 'spec_helper'
3
+
4
+ describe Praxis::FieldExpander do
5
+ let(:field_expander) { Praxis::FieldExpander.new }
6
+ let(:expanded_person_default_fieldset) do
7
+ # The only ones that are not already leaves is the full name, which can expand to first, last
8
+ PersonBlueprint.default_fieldset.merge!(full_name: {first: true, last: true})
9
+ end
10
+ let(:expanded_address_default_fieldset) do
11
+ # AddressBlueprint' default fieldset already has all attributes as leaves
12
+ AddressBlueprint.default_fieldset
13
+ end
14
+
15
+ context 'expanding attributes of a PersonBlueprint blueprint' do
16
+ it 'with fields=true, expands all fields on the default fieldset' do
17
+ expect(field_expander.expand(PersonBlueprint, true)).to eq(expanded_person_default_fieldset)
18
+ end
19
+
20
+ it 'expands for a subset of the direct fields' do
21
+ expect(field_expander.expand(PersonBlueprint, name: true)).to eq(name: true)
22
+ end
23
+
24
+ it 'raises when trying to expand fields that do not exist in the type' do
25
+ expect do
26
+ field_expander.expand(PersonBlueprint, name: true, foobar: true)
27
+ end.to raise_error(/.*:foobar.*do not exist in PersonBlueprint/)
28
+ end
29
+
30
+ context 'given inlined struct attribtue' do
31
+ it 'expands for a sub-struct' do
32
+ expect(field_expander.expand(PersonBlueprint, parents: true)).to eq(parents: { mother: true, father: true })
33
+ end
34
+
35
+ it 'expands for a subset of a substruct' do
36
+ expect(field_expander.expand(PersonBlueprint, parents: { mother: true })).to eq(parents: { mother: true })
37
+ end
38
+ end
39
+
40
+ context 'given a non-inlined struct sub attribute' do
41
+ it 'expands a specific subattribute of a struct' do
42
+ expect(field_expander.expand(PersonBlueprint, full_name: { first: true })).to eq(full_name: { first: true })
43
+ end
44
+
45
+ it 'fully expands collections properly by simply listing subfields' do
46
+ expect(field_expander.expand(PersonBlueprint, prior_addresses: true)).to eq(prior_addresses: expanded_address_default_fieldset)
47
+ end
48
+ end
49
+
50
+ context 'given a attribute that is also a blueprint' do
51
+ it 'expands to default fieldset for an attribute that is also blueprint' do
52
+ expect(field_expander.expand(PersonBlueprint, address: true)).to eq(address: expanded_address_default_fieldset)
53
+ end
54
+
55
+ it 'expands to default fieldset for a subset of fields of an attribute that is a blueprint' do
56
+ expect(field_expander.expand(PersonBlueprint, address: { resident: true })).to eq(address: { resident: expanded_person_default_fieldset })
57
+ end
58
+ end
59
+
60
+ context 'collection attributes' do
61
+ it 'expands subfields by simply listing subfields the same as structs/blueprints' do
62
+ expect(field_expander.expand(PersonBlueprint, prior_addresses: { state: true })).to eq(prior_addresses: { state: true })
63
+ end
64
+ end
65
+ end
66
+
67
+ it 'expands for for a primitive type' do
68
+ expect(field_expander.expand(String)).to eq(true)
69
+ end
70
+
71
+ it 'expands for an Attributor::Model' do
72
+ expect(field_expander.expand(FullName)).to eq(first: true, last: true)
73
+ end
74
+
75
+ it 'expands for a Blueprint' do
76
+ expect(field_expander.expand(PersonBlueprint, parents: true)).to eq(parents: { father: true, mother: true })
77
+ end
78
+
79
+ it 'expands for an Attributor::Collection of an Attrbutor::Model' do
80
+ expect(field_expander.expand(Attributor::Collection.of(FullName))).to eq(first: true, last: true)
81
+ end
82
+
83
+ it 'expands for an Attributor::Collection of a Blueprint' do
84
+ expected = { name: true, resident: { full_name: { first: true, last: true } } }
85
+ expect(field_expander.expand(Attributor::Collection.of(AddressBlueprint), name: true, resident: { full_name: true })).to eq(expected)
86
+ end
87
+
88
+ it 'also expands array-wrapped field hashes for collections' do
89
+ expected = { name: true, resident: { full_name: { first: true, last: true } } }
90
+ expect(field_expander.expand(Attributor::Collection.of(AddressBlueprint), name: true, resident: { full_name: true })).to eq(expected)
91
+ end
92
+
93
+ it 'expands for an Attributor::Collection of a primitive type' do
94
+ expect(field_expander.expand(Attributor::Collection.of(String))).to eq(true)
95
+ end
96
+
97
+ context 'expanding a two-dimensional collection' do
98
+ it 'expands the fields discarding the collection nexting nesting' do
99
+ matrix_type = Attributor::Collection.of(Attributor::Collection.of(FullName))
100
+ expect(field_expander.expand(matrix_type)).to eq(first: true, last: true)
101
+ end
102
+ end
103
+
104
+ context 'circular expansions' do
105
+ it 'preserve field object identity for circular references' do
106
+ result = field_expander.expand(PersonBlueprint, address: {resident: true}, work_address: {resident: true})
107
+ expect(result[:address][:resident]).to be(result[:work_address][:resident])
108
+ end
109
+
110
+ context 'with collections of Blueprints' do
111
+ it 'still preserves object identity' do
112
+ result = field_expander.expand(PersonBlueprint, address: {resident: true}, prior_addresses: {resident: true})
113
+ expect(result[:address][:resident]).to be(result[:prior_addresses][:resident])
114
+ end
115
+ end
116
+ end
117
+
118
+ it 'optimizes duplicate field expansions' do
119
+ expect(field_expander.expand(FullName, true)).to be(field_expander.expand(FullName, true))
120
+ end
121
+
122
+ context 'expanding hash attributes' do
123
+ let(:type) do
124
+ Class.new(Attributor::Model) do
125
+ attributes do
126
+ attribute :name, String
127
+ attribute :simple_hash, Hash
128
+ attribute :keyed_hash, Hash do
129
+ key :foo, String
130
+ key :bar, String
131
+ end
132
+ attribute :some_struct do
133
+ attribute :something, String
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ it 'expands' do
140
+ expected = {
141
+ name: true,
142
+ simple_hash: true,
143
+ keyed_hash: { foo: true, bar: true },
144
+ some_struct: { something: true }
145
+ }
146
+ expect(field_expander.expand(type, true)).to eq(expected)
147
+ end
148
+ end
149
+ end