snfoil-rails 0.7.0 → 0.7.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c04a7e0dc66d406d1e758d1d4a6cc3d1626fa02b0beb0d4465ae2940f6f517cd
4
- data.tar.gz: 27ffc1f5e63c1e2211ac68c133c9b7ade926c597adfbb05b146cb743a4460d2e
3
+ metadata.gz: e70578dd091d98c88bb3c7fbf6868187f1549bdc4e1b7db17e4a6ec3114090ce
4
+ data.tar.gz: e0513e5ed7245a443ded275617b5d72b615e5796286e8361e4ac030e12941af8
5
5
  SHA512:
6
- metadata.gz: 574a8c564a5d08240c91cc13e365d02b5b16f5c22524ac4160b1a2b4b54c8415a0d3164cf24d0a255f9af59e35df94f1cb48c53fcdb9858c88bfa545cc41b39f
7
- data.tar.gz: 8ea4ad813675320a6bf96def1797127c287976daff9aa474a7fe3dc9b30bce24cbbe9b51d3e44a32541416e26ef0d9749397b10dff968a9a80835943c1751a03
6
+ metadata.gz: c51c1f966922efa1ebea92c123b0adf8893155e43bbf7e4921c73c10295e535812a66b4e1341a170f36931d662c32784b592fb9795e46ae133be598b079046ff
7
+ data.tar.gz: 30e5c4175d007e73c6d57d8116c7d6808eef9777af52eb3040303b4fc1af36550537c75c0d76033241caeaabd487da7f24d7acf6706748f2998acb441e718e3d
@@ -0,0 +1,46 @@
1
+ class SnFoil::AllGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :type, desc: "Generate Base or API", type: :string, default: 'base'
7
+ class_option :skip_model, desc: "Skip Model Creation", type: :boolean, default: false
8
+
9
+ def add_model
10
+ generate('model', *call_args, **call_options) unless File.file?("app/models/#{model.singularize.underscore}.rb") || options[:skip_model]
11
+ end
12
+
13
+ def add_policy
14
+ generate('sn_foil:policy', *call_args, **call_options)
15
+ end
16
+
17
+ def add_searcher
18
+ generate('sn_foil:searcher', *call_args, **call_options)
19
+ end
20
+
21
+ def add_context
22
+ generate('sn_foil:context', *call_args, **call_options)
23
+ end
24
+
25
+ def add_jsonapi_serializer
26
+ generate('sn_foil:jsonapi_serializer', *call_args, **call_options)
27
+ end
28
+
29
+ def add_jsonapi_deserializer
30
+ generate('sn_foil:jsonapi_deserializer', *call_args, **call_options)
31
+ end
32
+
33
+ def add_controller
34
+ generate('sn_foil:controller', *call_args, **call_options)
35
+ end
36
+
37
+ private
38
+
39
+ def call_args
40
+ @call_args ||= [model].concat(args)
41
+ end
42
+
43
+ def call_options
44
+ @call_options ||= options.deep_symbolize_keys
45
+ end
46
+ end
@@ -0,0 +1,37 @@
1
+ class SnFoil::ContextGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/contexts'
7
+
8
+ def add_app_file
9
+ file_name = if modules.length.zero?
10
+ name
11
+ else
12
+ modules.join('/') + '/' + name
13
+ end
14
+
15
+ template('context.erb', "#{options[:path]}/#{file_name}_context.rb")
16
+ end
17
+
18
+ private
19
+
20
+ def name
21
+ @name ||= model.split('/').last.underscore.singularize
22
+ end
23
+
24
+ def class_name
25
+ @class_name ||= name.camelize
26
+ end
27
+
28
+ def modules
29
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
30
+ end
31
+
32
+ def class_modules
33
+ return if modules.length.zero?
34
+
35
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
36
+ end
37
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= class_modules %><%= class_name %>Context
4
+ include SnFoil::Context
5
+
6
+ model <%= class_name %>
7
+ searcher <%= class_name.pluralize %>Searcher
8
+ policy <%= class_name %>Policy
9
+ end
@@ -0,0 +1,44 @@
1
+ class SnFoil::ControllerGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :type, desc: "Generate Base or API", type: :string, default: 'base'
7
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/controllers'
8
+
9
+ def add_app_file
10
+ file_name = if modules.length.zero?
11
+ name
12
+ else
13
+ modules.join('/') + '/' + name
14
+ end
15
+
16
+ template_name = if options[:type] == 'api'
17
+ 'api_controller.erb'
18
+ else
19
+ 'controller.erb'
20
+ end
21
+
22
+ template(template_name, "#{options[:path]}/#{file_name}_controller.rb")
23
+ end
24
+
25
+ private
26
+
27
+ def name
28
+ @name ||= model.split('/').last.underscore.pluralize
29
+ end
30
+
31
+ def class_name
32
+ @class_name ||= name.camelize
33
+ end
34
+
35
+ def modules
36
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
37
+ end
38
+
39
+ def class_modules
40
+ return if modules.length.zero?
41
+
42
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
43
+ end
44
+ 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,37 @@
1
+ class SnFoil::JsonapiDeserializerGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/jsonapi_deserializers'
7
+
8
+ def add_app_file
9
+ file_name = if modules.length.zero?
10
+ name
11
+ else
12
+ modules.join('/') + '/' + name
13
+ end
14
+
15
+ template('jsonapi_deserializer.erb', "#{options[:path]}/#{file_name}_jsonapi_deserializer.rb")
16
+ end
17
+
18
+ private
19
+
20
+ def name
21
+ @name ||= model.split('/').last.underscore.singularize
22
+ end
23
+
24
+ def class_name
25
+ @class_name ||= name.camelize
26
+ end
27
+
28
+ def modules
29
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
30
+ end
31
+
32
+ def class_modules
33
+ return if modules.length.zero?
34
+
35
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
36
+ end
37
+ 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,37 @@
1
+ class SnFoil::JsonapiSerializerGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/jsonapi_serializers'
7
+
8
+ def add_app_file
9
+ file_name = if modules.length.zero?
10
+ name
11
+ else
12
+ modules.join('/') + '/' + name
13
+ end
14
+
15
+ template('jsonapi_serializer.erb', "#{options[:path]}/#{file_name}_jsonapi_serializer.rb")
16
+ end
17
+
18
+ private
19
+
20
+ def name
21
+ @name ||= model.split('/').last.underscore.singularize
22
+ end
23
+
24
+ def class_name
25
+ @class_name ||= name.camelize
26
+ end
27
+
28
+ def modules
29
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
30
+ end
31
+
32
+ def class_modules
33
+ return if modules.length.zero?
34
+
35
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
36
+ end
37
+ 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,37 @@
1
+ class SnFoil::PolicyGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/policies'
7
+
8
+ def add_app_file
9
+ file_name = if modules.length.zero?
10
+ name
11
+ else
12
+ modules.join('/') + '/' + name
13
+ end
14
+
15
+ template('policy.erb', "#{options[:path]}/#{file_name}_policy.rb")
16
+ end
17
+
18
+ private
19
+
20
+ def name
21
+ @name ||= model.split('/').last.underscore.singularize
22
+ end
23
+
24
+ def class_name
25
+ @class_name ||= name.camelize
26
+ end
27
+
28
+ def modules
29
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
30
+ end
31
+
32
+ def class_modules
33
+ return if modules.length.zero?
34
+
35
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
36
+ end
37
+ 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,37 @@
1
+ class SnFoil::SearcherGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ argument :model, type: :string
5
+
6
+ class_option :path, desc: "Base path for file", type: :string, default: 'app/searchers'
7
+
8
+ def add_app_file
9
+ file_name = if modules.length.zero?
10
+ name
11
+ else
12
+ modules.join('/') + '/' + name
13
+ end
14
+
15
+ template('searcher.erb', "#{options[:path]}/#{file_name}_searcher.rb")
16
+ end
17
+
18
+ private
19
+
20
+ def name
21
+ @name ||= model.split('/').last.underscore.pluralize
22
+ end
23
+
24
+ def class_name
25
+ @class_name ||= name.camelize
26
+ end
27
+
28
+ def modules
29
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
30
+ end
31
+
32
+ def class_modules
33
+ return if modules.length.zero?
34
+
35
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= class_modules %><%= class_name %>Searcher
4
+ include SnFoil::Searcher
5
+
6
+ model <%= class_name.singularize %>
7
+ end
@@ -44,7 +44,7 @@ module SnFoil
44
44
  render json: serializer(**options).new(model,
45
45
  **options,
46
46
  params: (options[:controller_params] || options[:params] || {})
47
- .merge(current_entity: current_entity)).serializable_hash
47
+ .merge(current_entity: context_entity)).serializable_hash
48
48
  else
49
49
  render json: model.errors, status: :unprocessable_entity
50
50
  end
@@ -62,7 +62,7 @@ module SnFoil
62
62
  render json: serializer(**options).new(paginate(results, **options),
63
63
  **options,
64
64
  params: (options[:controller_params] || options[:params] || {})
65
- .merge(current_entity: current_entity),
65
+ .merge(current_entity: context_entity),
66
66
  meta: meta(results, **options))
67
67
  .serializable_hash
68
68
  end
@@ -71,7 +71,7 @@ module SnFoil
71
71
  render json: serializer(**options).new(model,
72
72
  **options,
73
73
  params: (options[:controller_params] || options[:params] || {})
74
- .merge(current_entity: current_entity)).serializable_hash
74
+ .merge(current_entity: context_entity)).serializable_hash
75
75
  end
76
76
 
77
77
  private
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SnFoil
4
4
  module Rails
5
- VERSION = '0.7.0'
5
+ VERSION = '0.7.1'
6
6
  end
7
7
  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: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-07-27 00:00:00.000000000 Z
12
+ date: 2020-09-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -191,7 +191,20 @@ files:
191
191
  - MIT-LICENSE
192
192
  - README.md
193
193
  - Rakefile
194
- - lib/generators/sn_foil/sn_foil_generator.rb
194
+ - lib/generators/sn_foil/all/all_generator.rb
195
+ - lib/generators/sn_foil/context/context_generator.rb
196
+ - lib/generators/sn_foil/context/templates/context.erb
197
+ - lib/generators/sn_foil/controller/controller_generator.rb
198
+ - lib/generators/sn_foil/controller/templates/api_controller.erb
199
+ - lib/generators/sn_foil/controller/templates/controller.erb
200
+ - lib/generators/sn_foil/jsonapi_deserializer/jsonapi_deserializer_generator.rb
201
+ - lib/generators/sn_foil/jsonapi_deserializer/templates/jsonapi_deserializer.erb
202
+ - lib/generators/sn_foil/jsonapi_serializer/jsonapi_serializer_generator.rb
203
+ - lib/generators/sn_foil/jsonapi_serializer/templates/jsonapi_serializer.erb
204
+ - lib/generators/sn_foil/policy/policy_generator.rb
205
+ - lib/generators/sn_foil/policy/templates/policy.erb
206
+ - lib/generators/sn_foil/searcher/searcher_generator.rb
207
+ - lib/generators/sn_foil/searcher/templates/searcher.erb
195
208
  - lib/sn_foil/controller/api.rb
196
209
  - lib/sn_foil/controller/base.rb
197
210
  - lib/sn_foil/controller/concerns/change_controller_concern.rb
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class SnFoilGenerator < Rails::Generators::Base
4
- source_root File.expand_path('templates', __dir__)
5
- argument :model, type: :string
6
-
7
- def generate_sn_foil
8
- generate_model
9
- generate_controller
10
- generate_searcher
11
- generate_serializer
12
- generate_deserializer
13
- generate_policy
14
- generate_context
15
- Rails.logger.info 'In order to expose your model, it must be added to the config/routes.rb file'
16
- end
17
-
18
- private
19
-
20
- def generate_context
21
- template 'context.erb', "app/contexts/#{model.singularize.underscore}_context.rb"
22
- end
23
-
24
- def generate_controller
25
- template 'controller.erb', "app/controllers/#{model.pluralize.underscore}_controller.rb"
26
- end
27
-
28
- def generate_deserializer
29
- template 'jsonapi_deserializer.erb', "app/deserializers/#{model.singularize.underscore}_deserializer.rb"
30
- end
31
-
32
- def generate_serializer
33
- template 'jsonapi_serializer.erb', "app/serializers/#{model.singularize.underscore}_jsonapi_serializer.rb"
34
- end
35
-
36
- def generate_model
37
- generate('model', model.underscore) unless File.file? "app/models/#{model.singularize.underscore}.rb"
38
- end
39
-
40
- def generate_searcher
41
- template 'searcher.erb', "app/searchers/#{model.pluralize.underscore}_searcher.rb"
42
- end
43
-
44
- def generate_policy
45
- template 'policy.erb', "app/policies/#{model.singularize.underscore}_policy.rb"
46
- end
47
- end