snfoil-rails 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +17 -0
- data/LICENSE.txt +201 -0
- data/README.md +207 -0
- data/lib/generators/snfoil/all/USAGE +21 -0
- data/lib/generators/snfoil/all/all_generator.rb +64 -0
- data/lib/generators/snfoil/context/USAGE +14 -0
- data/lib/generators/snfoil/context/context_generator.rb +55 -0
- data/lib/generators/snfoil/context/templates/context.erb +9 -0
- data/lib/generators/snfoil/controller/USAGE +15 -0
- data/lib/generators/snfoil/controller/controller_generator.rb +66 -0
- data/lib/generators/snfoil/controller/templates/api_controller.erb +14 -0
- data/lib/generators/snfoil/controller/templates/controller.erb +14 -0
- data/lib/generators/snfoil/jsonapi_deserializer/USAGE +14 -0
- data/lib/generators/snfoil/jsonapi_deserializer/jsonapi_deserializer_generator.rb +55 -0
- data/lib/generators/snfoil/jsonapi_deserializer/templates/jsonapi_deserializer.erb +12 -0
- data/lib/generators/snfoil/jsonapi_serializer/USAGE +14 -0
- data/lib/generators/snfoil/jsonapi_serializer/jsonapi_serializer_generator.rb +55 -0
- data/lib/generators/snfoil/jsonapi_serializer/templates/jsonapi_serializer.erb +20 -0
- data/lib/generators/snfoil/policy/USAGE +14 -0
- data/lib/generators/snfoil/policy/policy_generator.rb +55 -0
- data/lib/generators/snfoil/policy/templates/policy.erb +22 -0
- data/lib/generators/snfoil/searcher/USAGE +14 -0
- data/lib/generators/snfoil/searcher/searcher_generator.rb +55 -0
- data/lib/generators/snfoil/searcher/templates/searcher.erb +7 -0
- data/lib/snfoil/rails/api/create.rb +56 -0
- data/lib/snfoil/rails/api/destroy.rb +54 -0
- data/lib/snfoil/rails/api/index.rb +53 -0
- data/lib/snfoil/rails/api/show.rb +53 -0
- data/lib/snfoil/rails/api/update.rb +60 -0
- data/lib/snfoil/rails/api_controller.rb +33 -0
- data/lib/snfoil/rails/concerns/inject_deserialized.rb +31 -0
- data/lib/snfoil/rails/concerns/inject_id.rb +33 -0
- data/lib/snfoil/rails/concerns/inject_include.rb +35 -0
- data/lib/snfoil/rails/concerns/inject_request_id.rb +31 -0
- data/lib/snfoil/rails/concerns/inject_request_params.rb +34 -0
- data/lib/snfoil/rails/concerns/process_pagination.rb +71 -0
- data/lib/snfoil/rails/engine.rb +38 -0
- data/lib/snfoil/rails/searcher.rb +127 -0
- data/lib/snfoil/rails/version.rb +21 -0
- data/lib/snfoil/rails.rb +7 -0
- data/lib/tasks/snfoil/rails_tasks.rake +5 -0
- data/snfoil-rails.gemspec +53 -0
- metadata +79 -23
@@ -0,0 +1,15 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a SnFoil::Controller Scaffold
|
3
|
+
|
4
|
+
Arguments:
|
5
|
+
model: *required* The name of the model
|
6
|
+
|
7
|
+
Options:
|
8
|
+
path: The path the files will be created at. default: app/controllers
|
9
|
+
type: The type of controller to create (base, api). default: base
|
10
|
+
|
11
|
+
Example:
|
12
|
+
rails generate snfoil:controller User
|
13
|
+
|
14
|
+
This will create:
|
15
|
+
app/controllers/users_controller.rb
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
class ControllerGenerator < Rails::Generators::Base
|
19
|
+
source_root File.expand_path('templates', __dir__)
|
20
|
+
|
21
|
+
argument :model, type: :string
|
22
|
+
|
23
|
+
class_option :type, desc: 'Generate Base or API', type: :string, default: 'base'
|
24
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/controllers'
|
25
|
+
|
26
|
+
def add_app_file
|
27
|
+
template(template_name, "#{options[:path]}/#{file_name}_controller.rb")
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def file_name
|
33
|
+
@file_name ||= if modules.length.zero?
|
34
|
+
name
|
35
|
+
else
|
36
|
+
"#{modules.join('/')}/#{name}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def template_name
|
41
|
+
@template_name ||= if options[:type] == 'api'
|
42
|
+
'api_controller.erb'
|
43
|
+
else
|
44
|
+
'controller.erb'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def name
|
49
|
+
@name ||= model.split('/').last.underscore.pluralize
|
50
|
+
end
|
51
|
+
|
52
|
+
def class_name
|
53
|
+
@class_name ||= name.camelize
|
54
|
+
end
|
55
|
+
|
56
|
+
def modules
|
57
|
+
@modules ||= model.split('/')[0..-2].map(&:underscore)
|
58
|
+
end
|
59
|
+
|
60
|
+
def class_modules
|
61
|
+
return if modules.length.zero?
|
62
|
+
|
63
|
+
@class_modules ||= "#{modules.map(&:camelize).join('::')}::"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Controller < SnFoil::Controller::API
|
4
|
+
context <%= class_name.singularize %>Context
|
5
|
+
serializer <%= class_name.singularize %>JsonapiSerializer
|
6
|
+
deserializer <%= class_name.singularize %>JsonapiDeserializer
|
7
|
+
|
8
|
+
# SnFoil::Controller requires current_entity or current_user defined to properly scope
|
9
|
+
# all queries, otherwise data leaks could occur
|
10
|
+
#
|
11
|
+
# def current_entity
|
12
|
+
# # logic to find authenticated user
|
13
|
+
# end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Controller < SnFoil::Controller::Base
|
4
|
+
context <%= name.singularize.camelcase %>Context
|
5
|
+
serializer <%= name.singularize.camelcase %>JsonapiSerializer
|
6
|
+
deserializer <%= name.singularize.camelcase %>JsonapiDeserializer
|
7
|
+
|
8
|
+
# SnFoil::Controller requires current_entity or current_user defined to properly scope
|
9
|
+
# all queries, otherwise data leaks could occur
|
10
|
+
#
|
11
|
+
# def current_entity
|
12
|
+
# # logic to find authenticated user
|
13
|
+
# end
|
14
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a SnFoil::JsonapiDeSerializer Scaffold
|
3
|
+
|
4
|
+
Arguments:
|
5
|
+
model: *required* The name of the model
|
6
|
+
|
7
|
+
Options:
|
8
|
+
path: The path the files will be created at. default: app/jsonapi_deserializers
|
9
|
+
|
10
|
+
Example:
|
11
|
+
rails generate snfoil:jsonapi_deserializer User
|
12
|
+
|
13
|
+
This will create:
|
14
|
+
app/jsonapi_deserializers/user_jsonapi_deserializer.rb
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
class JsonapiDeserializerGenerator < Rails::Generators::Base
|
19
|
+
source_root File.expand_path('templates', __dir__)
|
20
|
+
|
21
|
+
argument :model, type: :string
|
22
|
+
|
23
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/jsonapi_deserializers'
|
24
|
+
|
25
|
+
def add_app_file
|
26
|
+
file_name = if modules.length.zero?
|
27
|
+
name
|
28
|
+
else
|
29
|
+
"#{modules.join('/')}/#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
template('jsonapi_deserializer.erb', "#{options[:path]}/#{file_name}_jsonapi_deserializer.rb")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def name
|
38
|
+
@name ||= model.split('/').last.underscore.singularize
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
@class_name ||= name.camelize
|
43
|
+
end
|
44
|
+
|
45
|
+
def modules
|
46
|
+
@modules ||= model.split('/')[0..-2].map(&:underscore)
|
47
|
+
end
|
48
|
+
|
49
|
+
def class_modules
|
50
|
+
return if modules.length.zero?
|
51
|
+
|
52
|
+
@class_modules ||= "#{modules.map(&:camelize).join('::')}::"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>JsonapiDeserializer
|
4
|
+
include SnFoil::JsonapiDeserializer
|
5
|
+
|
6
|
+
# Add attributes of the model you want to serializer with the following syntax
|
7
|
+
attributes :id
|
8
|
+
|
9
|
+
# Add relationships with the following syntax
|
10
|
+
# belongs_to :store, serializer: Jsonapi::StoreJsonapiSerializer
|
11
|
+
# has_many :locations, serializer: Jsonapi::LocationJsonapiSerializer
|
12
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a SnFoil::JsonapiSerializer Scaffold
|
3
|
+
|
4
|
+
Arguments:
|
5
|
+
model: *required* The name of the model
|
6
|
+
|
7
|
+
Options:
|
8
|
+
path: The path the files will be created at. default: app/jsonapi_serializers
|
9
|
+
|
10
|
+
Example:
|
11
|
+
rails generate snfoil:jsonapi_serializer User
|
12
|
+
|
13
|
+
This will create:
|
14
|
+
app/jsonapi_serializers/user_jsonapi_serializer.rb
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
class JsonapiSerializerGenerator < Rails::Generators::Base
|
19
|
+
source_root File.expand_path('templates', __dir__)
|
20
|
+
|
21
|
+
argument :model, type: :string
|
22
|
+
|
23
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/jsonapi_serializers'
|
24
|
+
|
25
|
+
def add_app_file
|
26
|
+
file_name = if modules.length.zero?
|
27
|
+
name
|
28
|
+
else
|
29
|
+
"#{modules.join('/')}/#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
template('jsonapi_serializer.erb', "#{options[:path]}/#{file_name}_jsonapi_serializer.rb")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def name
|
38
|
+
@name ||= model.split('/').last.underscore.singularize
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
@class_name ||= name.camelize
|
43
|
+
end
|
44
|
+
|
45
|
+
def modules
|
46
|
+
@modules ||= model.split('/')[0..-2].map(&:underscore)
|
47
|
+
end
|
48
|
+
|
49
|
+
def class_modules
|
50
|
+
return if modules.length.zero?
|
51
|
+
|
52
|
+
@class_modules ||= "#{modules.map(&:camelize).join('::')}::"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>JsonapiSerializer
|
4
|
+
include SnFoil::JsonapiSerializer
|
5
|
+
|
6
|
+
set_id :id
|
7
|
+
set_type :<%= name.pluralize.dasherize %>
|
8
|
+
|
9
|
+
# SnFoil::JsonapiSerializer is just a wrapper for jsonapi-serializer (https://github.com/jsonapi-serializer/jsonapi-serializer)
|
10
|
+
# with some defaults added in
|
11
|
+
|
12
|
+
# Add attributes of the model you want to serializer with the following syntax
|
13
|
+
# attributes :name
|
14
|
+
# :description
|
15
|
+
# :logo_url
|
16
|
+
|
17
|
+
# Add relationships with the following syntax
|
18
|
+
# belongs_to :store, serializer: Jsonapi::StoreJsonapiSerializer
|
19
|
+
# has_many :locations, serializer: Jsonapi::LocationJsonapiSerializer
|
20
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a SnFoil::Policy Scaffold
|
3
|
+
|
4
|
+
Arguments:
|
5
|
+
model: *required* The name of the model
|
6
|
+
|
7
|
+
Options:
|
8
|
+
path: The path the files will be created at. default: app/policies
|
9
|
+
|
10
|
+
Example:
|
11
|
+
rails generate snfoil:policy User
|
12
|
+
|
13
|
+
This will create:
|
14
|
+
app/policies/user_policy.rb
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
class PolicyGenerator < Rails::Generators::Base
|
19
|
+
source_root File.expand_path('templates', __dir__)
|
20
|
+
|
21
|
+
argument :model, type: :string
|
22
|
+
|
23
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/policies'
|
24
|
+
|
25
|
+
def add_app_file
|
26
|
+
file_name = if modules.length.zero?
|
27
|
+
name
|
28
|
+
else
|
29
|
+
"#{modules.join('/')}/#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
template('policy.erb', "#{options[:path]}/#{file_name}_policy.rb")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def name
|
38
|
+
@name ||= model.split('/').last.underscore.singularize
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
@class_name ||= name.camelize
|
43
|
+
end
|
44
|
+
|
45
|
+
def modules
|
46
|
+
@modules ||= model.split('/')[0..-2].map(&:underscore)
|
47
|
+
end
|
48
|
+
|
49
|
+
def class_modules
|
50
|
+
return if modules.length.zero?
|
51
|
+
|
52
|
+
@class_modules ||= "#{modules.map(&:camelize).join('::')}::"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Policy
|
4
|
+
include SnFoil::Policy
|
5
|
+
# A SnFoil::Policy is just a Pundit policy (https://github.com/varvet/pundit)
|
6
|
+
# with some defaults
|
7
|
+
# Available methods: show?, create?, update?, destroy?, index?, associate?
|
8
|
+
|
9
|
+
# def show?
|
10
|
+
# true
|
11
|
+
# end
|
12
|
+
|
13
|
+
# class Scope
|
14
|
+
# # available read-only attributes
|
15
|
+
# # - scope: the default scope passed into the policy (ex: an ActiveRecord::Relation)
|
16
|
+
# # - entity: the object that the scope is build around (ex: a User)
|
17
|
+
|
18
|
+
# def resolve
|
19
|
+
# # the new default scope
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Description:
|
2
|
+
Creates a SnFoil::Searcher Scaffold
|
3
|
+
|
4
|
+
Arguments:
|
5
|
+
model: *required* The name of the model
|
6
|
+
|
7
|
+
Options:
|
8
|
+
path: The path the files will be created at. default: app/searcher
|
9
|
+
|
10
|
+
Example:
|
11
|
+
rails generate snfoil:searcher User
|
12
|
+
|
13
|
+
This will create:
|
14
|
+
app/searchers/users_searcher.rb
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
module SnFoil
|
18
|
+
class SearcherGenerator < Rails::Generators::Base
|
19
|
+
source_root File.expand_path('templates', __dir__)
|
20
|
+
|
21
|
+
argument :model, type: :string
|
22
|
+
|
23
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/searchers'
|
24
|
+
|
25
|
+
def add_app_file
|
26
|
+
file_name = if modules.length.zero?
|
27
|
+
name
|
28
|
+
else
|
29
|
+
"#{modules.join('/')}/#{name}"
|
30
|
+
end
|
31
|
+
|
32
|
+
template('searcher.erb', "#{options[:path]}/#{file_name}_searcher.rb")
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def name
|
38
|
+
@name ||= model.split('/').last.underscore.pluralize
|
39
|
+
end
|
40
|
+
|
41
|
+
def class_name
|
42
|
+
@class_name ||= name.camelize
|
43
|
+
end
|
44
|
+
|
45
|
+
def modules
|
46
|
+
@modules ||= model.split('/')[0..-2].map(&:underscore)
|
47
|
+
end
|
48
|
+
|
49
|
+
def class_modules
|
50
|
+
return if modules.length.zero?
|
51
|
+
|
52
|
+
@class_modules ||= "#{modules.map(&:camelize).join('::')}::"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative '../concerns/inject_deserialized'
|
18
|
+
require_relative '../concerns/inject_request_params'
|
19
|
+
require_relative '../concerns/inject_request_id'
|
20
|
+
require 'snfoil/controller'
|
21
|
+
|
22
|
+
module SnFoil
|
23
|
+
module Rails
|
24
|
+
module API
|
25
|
+
module Create
|
26
|
+
extend ActiveSupport::Concern
|
27
|
+
|
28
|
+
included do
|
29
|
+
include SnFoil::Controller
|
30
|
+
|
31
|
+
include InjectRequestId
|
32
|
+
include InjectDeserialized
|
33
|
+
include InjectInclude
|
34
|
+
include InjectRequestParams
|
35
|
+
|
36
|
+
endpoint :create, with: :render_create
|
37
|
+
|
38
|
+
setup_create with: :inject_request_id
|
39
|
+
setup_create with: :inject_request_params
|
40
|
+
setup_create with: :inject_deserialized
|
41
|
+
setup_create with: :inject_include
|
42
|
+
|
43
|
+
process_create with: :run_context
|
44
|
+
|
45
|
+
def render_create(**options)
|
46
|
+
if options[:object].errors.empty?
|
47
|
+
render json: serialize(options[:object], **options), status: :created
|
48
|
+
else
|
49
|
+
render json: options[:object].errors, status: :unprocessable_entity
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative '../concerns/inject_id'
|
18
|
+
require_relative '../concerns/inject_request_params'
|
19
|
+
require_relative '../concerns/inject_request_id'
|
20
|
+
require 'snfoil/controller'
|
21
|
+
|
22
|
+
module SnFoil
|
23
|
+
module Rails
|
24
|
+
module API
|
25
|
+
module Destroy
|
26
|
+
extend ActiveSupport::Concern
|
27
|
+
|
28
|
+
included do
|
29
|
+
include SnFoil::Controller
|
30
|
+
|
31
|
+
include InjectRequestId
|
32
|
+
include InjectId
|
33
|
+
include InjectRequestParams
|
34
|
+
|
35
|
+
endpoint :destroy, with: :render_destroy
|
36
|
+
|
37
|
+
setup_destroy with: :inject_request_id
|
38
|
+
setup_destroy with: :inject_request_params
|
39
|
+
setup_destroy with: :inject_id
|
40
|
+
|
41
|
+
process_destroy with: :run_context
|
42
|
+
|
43
|
+
def render_destroy(**options)
|
44
|
+
if options[:object].errors.empty?
|
45
|
+
render json: {}, status: :no_content
|
46
|
+
else
|
47
|
+
render json: options[:object].errors, status: :unprocessable_entity
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright 2021 Matthew Howes
|
4
|
+
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
|
+
# you may not use this file except in compliance with the License.
|
7
|
+
# You may obtain a copy of the License at
|
8
|
+
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10
|
+
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14
|
+
# See the License for the specific language governing permissions and
|
15
|
+
# limitations under the License.
|
16
|
+
|
17
|
+
require_relative '../concerns/inject_include'
|
18
|
+
require_relative '../concerns/inject_request_params'
|
19
|
+
require_relative '../concerns/inject_request_id'
|
20
|
+
require_relative '../concerns/process_pagination'
|
21
|
+
require 'snfoil/controller'
|
22
|
+
|
23
|
+
module SnFoil
|
24
|
+
module Rails
|
25
|
+
module API
|
26
|
+
module Index
|
27
|
+
extend ActiveSupport::Concern
|
28
|
+
|
29
|
+
included do
|
30
|
+
include SnFoil::Controller
|
31
|
+
|
32
|
+
include InjectRequestId
|
33
|
+
include InjectInclude
|
34
|
+
include InjectRequestParams
|
35
|
+
include ProcessPagination
|
36
|
+
|
37
|
+
endpoint :index, with: :render_index
|
38
|
+
|
39
|
+
setup_index with: :inject_request_id
|
40
|
+
setup_index with: :inject_request_params
|
41
|
+
setup_index with: :inject_include
|
42
|
+
|
43
|
+
process_index with: :run_context
|
44
|
+
process_index with: :process_pagination
|
45
|
+
|
46
|
+
def render_index(**options)
|
47
|
+
render json: serialize(options[:object], **options), status: :ok
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|