snfoil-rails 1.1.2 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/generators/snfoil/all/all_generator.rb +1 -1
- data/lib/generators/snfoil/context/context_generator.rb +1 -1
- data/lib/generators/snfoil/context/templates/context.erb +15 -1
- data/lib/generators/snfoil/controller/controller_generator.rb +1 -1
- data/lib/generators/snfoil/controller/templates/controller.erb +4 -4
- data/lib/generators/snfoil/{jsonapi_deserializer/jsonapi_deserializer_generator.rb → deserializer/deserializer_generator.rb} +16 -5
- data/lib/generators/snfoil/deserializer/templates/json.erb +15 -0
- data/lib/generators/snfoil/deserializer/templates/jsonapi.erb +15 -0
- data/lib/generators/snfoil/policy/policy_generator.rb +1 -1
- data/lib/generators/snfoil/policy/templates/policy.erb +10 -13
- data/lib/generators/snfoil/searcher/searcher_generator.rb +1 -1
- data/lib/generators/snfoil/searcher/templates/searcher.erb +6 -2
- data/lib/generators/snfoil/{jsonapi_serializer/jsonapi_serializer_generator.rb → serializer/serializer_generator.rb} +5 -5
- data/lib/generators/snfoil/serializer/templates/serializer.erb +18 -0
- data/lib/snfoil/rails/api/create.rb +1 -1
- data/lib/snfoil/rails/api/destroy.rb +1 -1
- data/lib/snfoil/rails/api/index.rb +1 -1
- data/lib/snfoil/rails/api/show.rb +1 -1
- data/lib/snfoil/rails/api/update.rb +1 -1
- data/lib/snfoil/rails/api_controller.rb +1 -1
- data/lib/snfoil/rails/concerns/inject_deserialized.rb +1 -1
- data/lib/snfoil/rails/concerns/inject_id.rb +1 -1
- data/lib/snfoil/rails/concerns/inject_include.rb +1 -1
- data/lib/snfoil/rails/concerns/inject_request_id.rb +1 -1
- data/lib/snfoil/rails/concerns/inject_request_params.rb +1 -1
- data/lib/snfoil/rails/concerns/process_pagination.rb +1 -1
- data/lib/snfoil/rails/engine.rb +1 -1
- data/lib/snfoil/rails/searcher.rb +1 -1
- data/lib/snfoil/rails/version.rb +2 -2
- metadata +8 -8
- data/lib/generators/snfoil/controller/templates/api_controller.erb +0 -14
- data/lib/generators/snfoil/jsonapi_deserializer/templates/jsonapi_deserializer.erb +0 -12
- data/lib/generators/snfoil/jsonapi_serializer/templates/jsonapi_serializer.erb +0 -20
- /data/lib/generators/snfoil/{jsonapi_deserializer → deserializer}/USAGE +0 -0
- /data/lib/generators/snfoil/{jsonapi_serializer → serializer}/USAGE +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a0c8626c47b7239591baba976eecbad1f19b3945c20d494f7c696c51c864c1b
|
4
|
+
data.tar.gz: 71d34c2b2a60a1cf30f7bd7e03bb9863a14a35de2172866f9f9b9b3ec197b885
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdd56a1d2f025ebbb8badc3ebbbf411359d3bd7a5aa35e95adedfcacb147bc5754fb82a09d8a07135d9e33878df947bc11d4b6491ca2eda3c87124333a5441b7
|
7
|
+
data.tar.gz: 350c57c9e5617ef2336a2dd1e85e25dd3526937bec46d546116849db1ed2b84e062d02d02473c0e1d781fb7963c1f7760651d47b0d55f0aaf6314d37ad72fb77
|
@@ -1,9 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class <%= class_modules %><%= class_name %>Context
|
4
|
-
include SnFoil::Context
|
4
|
+
include SnFoil::CRUD::Context
|
5
5
|
|
6
6
|
model <%= class_name %>
|
7
7
|
searcher <%= class_name.pluralize %>Searcher
|
8
8
|
policy <%= class_name %>Policy
|
9
|
+
|
10
|
+
# #create #update #destroy #show #index are already defined by SnFoil::CRUD::Context
|
11
|
+
# you can override their behavior by editing their hooks. Example:
|
12
|
+
# after_create do |options|
|
13
|
+
# # do something after create
|
14
|
+
# options # return options in hooks
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# or you can define your own workflows
|
18
|
+
# action :expire do |options|
|
19
|
+
# # expire the object
|
20
|
+
#
|
21
|
+
# options
|
22
|
+
# end
|
9
23
|
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
class <%= class_modules %><%= class_name %>Controller < SnFoil::
|
4
|
-
context <%=
|
5
|
-
serializer <%=
|
6
|
-
deserializer <%=
|
3
|
+
class <%= class_modules %><%= class_name %>Controller < SnFoil::Rails::APIController
|
4
|
+
context <%= class_name.singularize %>Context
|
5
|
+
serializer <%= class_name.singularize %>Serializer
|
6
|
+
deserializer <%= class_name.singularize %>Deserializer
|
7
7
|
|
8
8
|
# SnFoil::Controller requires current_entity or current_user defined to properly scope
|
9
9
|
# all queries, otherwise data leaks could occur
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2025 Matthew Howes
|
4
4
|
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
6
|
# you may not use this file except in compliance with the License.
|
@@ -15,17 +15,18 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module SnFoil
|
18
|
-
class
|
18
|
+
class DeserializerGenerator < ::Rails::Generators::Base
|
19
19
|
def self.base_name
|
20
20
|
'snfoil'
|
21
21
|
end
|
22
22
|
|
23
|
-
namespace 'snfoil:
|
23
|
+
namespace 'snfoil:deserializer'
|
24
24
|
source_root File.expand_path('templates', __dir__)
|
25
25
|
|
26
26
|
argument :model, type: :string
|
27
27
|
|
28
|
-
class_option :
|
28
|
+
class_option :type, desc: 'Generate JSON or JSONAPI deserializer', type: :string, default: 'json'
|
29
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/deserializers'
|
29
30
|
|
30
31
|
def add_app_file
|
31
32
|
file_name = if modules.empty?
|
@@ -34,7 +35,7 @@ module SnFoil
|
|
34
35
|
"#{modules.join('/')}/#{name}"
|
35
36
|
end
|
36
37
|
|
37
|
-
template('
|
38
|
+
template('deserializer.erb', "#{options[:path]}/#{file_name}_deserializer.rb")
|
38
39
|
end
|
39
40
|
|
40
41
|
private
|
@@ -43,6 +44,16 @@ module SnFoil
|
|
43
44
|
@name ||= model.split('/').last.underscore.singularize
|
44
45
|
end
|
45
46
|
|
47
|
+
def template_name
|
48
|
+
@template_name ||= if options[:type] == 'json'
|
49
|
+
'json.erb'
|
50
|
+
elsif options[:type] == 'jsonapi'
|
51
|
+
'jsonapi.erb'
|
52
|
+
else
|
53
|
+
raise ArgumentError, "Unknown deserializer type: #{options[:type]}"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
46
57
|
def class_name
|
47
58
|
@class_name ||= name.camelize
|
48
59
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Deserializer
|
4
|
+
include SnFoil::Deserializer::JSON
|
5
|
+
|
6
|
+
# Underline the keys in the payload
|
7
|
+
key_transform :underscore
|
8
|
+
|
9
|
+
# Add attributes of the model you want to serializer with the following syntax
|
10
|
+
attributes :id
|
11
|
+
|
12
|
+
# Add relationships with the following syntax
|
13
|
+
# belongs_to :store, serializer: StoreSerializer
|
14
|
+
# has_many :locations, serializer: LocationSerializer
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Deserializer
|
4
|
+
include SnFoil::Deserializer::JSONAPI
|
5
|
+
|
6
|
+
# Underline the keys in the payload
|
7
|
+
key_transform :underscore
|
8
|
+
|
9
|
+
# Add attributes of the model you want to serializer with the following syntax
|
10
|
+
attributes :id
|
11
|
+
|
12
|
+
# Add relationships with the following syntax
|
13
|
+
# belongs_to :store, serializer: StoreSerializer
|
14
|
+
# has_many :locations, serializer: LocationSerializer
|
15
|
+
end
|
@@ -2,21 +2,18 @@
|
|
2
2
|
|
3
3
|
class <%= class_modules %><%= class_name %>Policy
|
4
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
5
|
|
9
|
-
#
|
10
|
-
#
|
11
|
-
# end
|
6
|
+
# Example policy for <%= class_name %> entity.
|
7
|
+
# permission :create?, with: :active_and_owner?
|
12
8
|
|
13
|
-
#
|
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)
|
9
|
+
# permission :update?, with: :active_and_owner?
|
17
10
|
|
18
|
-
#
|
19
|
-
#
|
20
|
-
#
|
11
|
+
# permission :destroy? do
|
12
|
+
# entity.archived_at.nil? &&
|
13
|
+
# (record.user_id == entity.id || entity.is_admin?)
|
14
|
+
# end
|
15
|
+
|
16
|
+
# def active_and_owner?
|
17
|
+
# entity.archived_at.nil? && record.user_id == entity.id
|
21
18
|
# end
|
22
19
|
end
|
@@ -1,7 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class <%= class_modules %><%= class_name %>Searcher
|
4
|
-
include SnFoil::Searcher
|
4
|
+
include SnFoil::Rails::Searcher
|
5
5
|
|
6
6
|
model <%= class_name.singularize %>
|
7
|
-
|
7
|
+
|
8
|
+
# filter do |scope, params|
|
9
|
+
# scope.where(archived_at: nil)
|
10
|
+
# end
|
11
|
+
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2025 Matthew Howes
|
4
4
|
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
6
|
# you may not use this file except in compliance with the License.
|
@@ -15,17 +15,17 @@
|
|
15
15
|
# limitations under the License.
|
16
16
|
|
17
17
|
module SnFoil
|
18
|
-
class
|
18
|
+
class SerializerGenerator < ::Rails::Generators::Base
|
19
19
|
def self.base_name
|
20
20
|
'snfoil'
|
21
21
|
end
|
22
22
|
|
23
|
-
namespace 'snfoil:
|
23
|
+
namespace 'snfoil:serializer'
|
24
24
|
source_root File.expand_path('templates', __dir__)
|
25
25
|
|
26
26
|
argument :model, type: :string
|
27
27
|
|
28
|
-
class_option :path, desc: 'Base path for file', type: :string, default: 'app/
|
28
|
+
class_option :path, desc: 'Base path for file', type: :string, default: 'app/serializers'
|
29
29
|
|
30
30
|
def add_app_file
|
31
31
|
file_name = if modules.empty?
|
@@ -34,7 +34,7 @@ module SnFoil
|
|
34
34
|
"#{modules.join('/')}/#{name}"
|
35
35
|
end
|
36
36
|
|
37
|
-
template('
|
37
|
+
template('serializer.erb', "#{options[:path]}/#{file_name}_serializer.rb")
|
38
38
|
end
|
39
39
|
|
40
40
|
private
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class <%= class_modules %><%= class_name %>Serializer
|
4
|
+
# SnFoil no longer ships with a serializer, it is recommended to use your favorite and implement
|
5
|
+
# the serialize method in your controller.
|
6
|
+
#
|
7
|
+
# example: Blueprinter
|
8
|
+
# class <%= class_modules %><%= class_name %>Serializer < Blueprinter::Base
|
9
|
+
# identifier :id
|
10
|
+
# fields :name, :description, :created_at, :updated_at
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# example: Alba
|
14
|
+
# class <%= class_modules %><%= class_name %>Serializer
|
15
|
+
# include Alba::Resource
|
16
|
+
# attributes :id, :name, :description, :created_at, :updated_at
|
17
|
+
# end
|
18
|
+
end
|
data/lib/snfoil/rails/engine.rb
CHANGED
data/lib/snfoil/rails/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
# Copyright
|
3
|
+
# Copyright 2025 Matthew Howes
|
4
4
|
|
5
5
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
6
6
|
# you may not use this file except in compliance with the License.
|
@@ -16,6 +16,6 @@
|
|
16
16
|
|
17
17
|
module SnFoil
|
18
18
|
module Rails
|
19
|
-
VERSION = '1.1.
|
19
|
+
VERSION = '1.1.4'
|
20
20
|
end
|
21
21
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: snfoil-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Howes
|
@@ -87,20 +87,20 @@ files:
|
|
87
87
|
- lib/generators/snfoil/context/templates/context.erb
|
88
88
|
- lib/generators/snfoil/controller/USAGE
|
89
89
|
- lib/generators/snfoil/controller/controller_generator.rb
|
90
|
-
- lib/generators/snfoil/controller/templates/api_controller.erb
|
91
90
|
- lib/generators/snfoil/controller/templates/controller.erb
|
92
|
-
- lib/generators/snfoil/
|
93
|
-
- lib/generators/snfoil/
|
94
|
-
- lib/generators/snfoil/
|
95
|
-
- lib/generators/snfoil/
|
96
|
-
- lib/generators/snfoil/jsonapi_serializer/jsonapi_serializer_generator.rb
|
97
|
-
- lib/generators/snfoil/jsonapi_serializer/templates/jsonapi_serializer.erb
|
91
|
+
- lib/generators/snfoil/deserializer/USAGE
|
92
|
+
- lib/generators/snfoil/deserializer/deserializer_generator.rb
|
93
|
+
- lib/generators/snfoil/deserializer/templates/json.erb
|
94
|
+
- lib/generators/snfoil/deserializer/templates/jsonapi.erb
|
98
95
|
- lib/generators/snfoil/policy/USAGE
|
99
96
|
- lib/generators/snfoil/policy/policy_generator.rb
|
100
97
|
- lib/generators/snfoil/policy/templates/policy.erb
|
101
98
|
- lib/generators/snfoil/searcher/USAGE
|
102
99
|
- lib/generators/snfoil/searcher/searcher_generator.rb
|
103
100
|
- lib/generators/snfoil/searcher/templates/searcher.erb
|
101
|
+
- lib/generators/snfoil/serializer/USAGE
|
102
|
+
- lib/generators/snfoil/serializer/serializer_generator.rb
|
103
|
+
- lib/generators/snfoil/serializer/templates/serializer.erb
|
104
104
|
- lib/snfoil/rails.rb
|
105
105
|
- lib/snfoil/rails/api/create.rb
|
106
106
|
- lib/snfoil/rails/api/destroy.rb
|
@@ -1,14 +0,0 @@
|
|
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
|
@@ -1,12 +0,0 @@
|
|
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
|
@@ -1,20 +0,0 @@
|
|
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
|
File without changes
|
File without changes
|