snfoil-rails 0.5.3 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2e314fff30b93ada5ce0fcecbfbc7ee6798df23bdc990311dd167937861b335
4
- data.tar.gz: 11362362a7feefaa15cb8ccc27eaba14f49c0bc3cdc81fdf6b7d26b32062b72b
3
+ metadata.gz: b91f3f4a14656e627fe9b82d091c81e1fff178536da8866993f548734dda1738
4
+ data.tar.gz: cf0ffd14498657a1b8611ad71f4c0d07d1d888e72d595660734ca5cba598e169
5
5
  SHA512:
6
- metadata.gz: 000b0f05e6295477bdb55dd6b26edecd7b236078ed80fef5c5e1e93696ec5eea1c63ad9efba69e88a14b4b51e4e67bce23e7022ae3330c733b19df1d72d9925c
7
- data.tar.gz: ad0fc2f266dc5fa51224b3e4e764bc6d5c0b9279ba95cd717f766c738aff812dbb4bbe05df351ce7d7e9189288e0d30740700c39dd5efa51bd16ee75254b300d
6
+ metadata.gz: 86c3cfef031f22074928729d25bc07b856f635463758b9f66a2582c540896e02312234146d784f4d5bd5bdaaec854c671dfa097372468372e1bcc46a55687def
7
+ data.tar.gz: 61a52228ccc3a4dba8941a8a01a85ce939fb1885cd65ce8c1cc108dd893d1e5103da52f1aef4cabef5e7432a97e421b41eeff63b40d6242a5f4f7b519ed6eda8
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class AllGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option(:type, desc: 'Generate Base or API', type: :string, default: 'base')
10
+ class_option(:skip_model, desc: 'Skip Model Creation', type: :boolean, default: false)
11
+
12
+ def add_model
13
+ rails_command "generate model #{call_args.join(' ')}", call_options
14
+ end
15
+
16
+ def add_policy
17
+ rails_command "generate sn_foil:policy #{call_args.join(' ')}", call_options
18
+ end
19
+
20
+ def add_searcher
21
+ rails_command "generate sn_foil:searcher #{call_args.join(' ')}", call_options
22
+ end
23
+
24
+ def add_context
25
+ rails_command "generate sn_foil:context #{call_args.join(' ')}", call_options
26
+ end
27
+
28
+ def add_jsonapi_serializer
29
+ rails_command "generate sn_foil:jsonapi_serializer #{call_args.join(' ')}", call_options
30
+ end
31
+
32
+ def add_jsonapi_deserializer
33
+ rails_command "generate sn_foil:jsonapi_deserializer #{call_args.join(' ')}", call_options
34
+ end
35
+
36
+ def add_controller
37
+ rails_command "generate sn_foil:controller #{call_args.join(' ')}", call_options
38
+ end
39
+
40
+ private
41
+
42
+ def call_args
43
+ @call_args ||= [model].concat(args)
44
+ end
45
+
46
+ def call_options
47
+ @call_options ||= options.deep_symbolize_keys
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class ContextGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/contexts'
10
+
11
+ def add_app_file
12
+ file_name = if modules.length.zero?
13
+ name
14
+ else
15
+ modules.join('/') + '/' + name
16
+ end
17
+
18
+ template('context.erb', "#{options[:path]}/#{file_name}_context.rb")
19
+ end
20
+
21
+ private
22
+
23
+ def name
24
+ @name ||= model.split('/').last.underscore.singularize
25
+ end
26
+
27
+ def class_name
28
+ @class_name ||= name.camelize
29
+ end
30
+
31
+ def modules
32
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
33
+ end
34
+
35
+ def class_modules
36
+ return if modules.length.zero?
37
+
38
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
39
+ end
40
+ end
41
+ 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,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class ControllerGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :type, desc: 'Generate Base or API', type: :string, default: 'base'
10
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/controllers'
11
+
12
+ def add_app_file
13
+ template(template_name, "#{options[:path]}/#{file_name}_controller.rb")
14
+ end
15
+
16
+ private
17
+
18
+ def file_name
19
+ @file_name ||= if modules.length.zero?
20
+ name
21
+ else
22
+ modules.join('/') + '/' + name
23
+ end
24
+ end
25
+
26
+ def template_name
27
+ @template_name ||= if options[:type] == 'api'
28
+ 'api_controller.erb'
29
+ else
30
+ 'controller.erb'
31
+ end
32
+ end
33
+
34
+ def name
35
+ @name ||= model.split('/').last.underscore.pluralize
36
+ end
37
+
38
+ def class_name
39
+ @class_name ||= name.camelize
40
+ end
41
+
42
+ def modules
43
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
44
+ end
45
+
46
+ def class_modules
47
+ return if modules.length.zero?
48
+
49
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
50
+ end
51
+ end
52
+ 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,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class JsonapiDeserializerGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/jsonapi_deserializers'
10
+
11
+ def add_app_file
12
+ file_name = if modules.length.zero?
13
+ name
14
+ else
15
+ modules.join('/') + '/' + name
16
+ end
17
+
18
+ template('jsonapi_deserializer.erb', "#{options[:path]}/#{file_name}_jsonapi_deserializer.rb")
19
+ end
20
+
21
+ private
22
+
23
+ def name
24
+ @name ||= model.split('/').last.underscore.singularize
25
+ end
26
+
27
+ def class_name
28
+ @class_name ||= name.camelize
29
+ end
30
+
31
+ def modules
32
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
33
+ end
34
+
35
+ def class_modules
36
+ return if modules.length.zero?
37
+
38
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
39
+ end
40
+ end
41
+ 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,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class JsonapiSerializerGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/jsonapi_serializers'
10
+
11
+ def add_app_file
12
+ file_name = if modules.length.zero?
13
+ name
14
+ else
15
+ modules.join('/') + '/' + name
16
+ end
17
+
18
+ template('jsonapi_serializer.erb', "#{options[:path]}/#{file_name}_jsonapi_serializer.rb")
19
+ end
20
+
21
+ private
22
+
23
+ def name
24
+ @name ||= model.split('/').last.underscore.singularize
25
+ end
26
+
27
+ def class_name
28
+ @class_name ||= name.camelize
29
+ end
30
+
31
+ def modules
32
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
33
+ end
34
+
35
+ def class_modules
36
+ return if modules.length.zero?
37
+
38
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
39
+ end
40
+ end
41
+ 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,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class PolicyGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/policies'
10
+
11
+ def add_app_file
12
+ file_name = if modules.length.zero?
13
+ name
14
+ else
15
+ modules.join('/') + '/' + name
16
+ end
17
+
18
+ template('policy.erb', "#{options[:path]}/#{file_name}_policy.rb")
19
+ end
20
+
21
+ private
22
+
23
+ def name
24
+ @name ||= model.split('/').last.underscore.singularize
25
+ end
26
+
27
+ def class_name
28
+ @class_name ||= name.camelize
29
+ end
30
+
31
+ def modules
32
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
33
+ end
34
+
35
+ def class_modules
36
+ return if modules.length.zero?
37
+
38
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
39
+ end
40
+ end
41
+ 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,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ class SearcherGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('templates', __dir__)
6
+
7
+ argument :model, type: :string
8
+
9
+ class_option :path, desc: 'Base path for file', type: :string, default: 'app/searchers'
10
+
11
+ def add_app_file
12
+ file_name = if modules.length.zero?
13
+ name
14
+ else
15
+ modules.join('/') + '/' + name
16
+ end
17
+
18
+ template('searcher.erb', "#{options[:path]}/#{file_name}_searcher.rb")
19
+ end
20
+
21
+ private
22
+
23
+ def name
24
+ @name ||= model.split('/').last.underscore.pluralize
25
+ end
26
+
27
+ def class_name
28
+ @class_name ||= name.camelize
29
+ end
30
+
31
+ def modules
32
+ @modules ||= model.split('/')[0..-2].map(&:underscore)
33
+ end
34
+
35
+ def class_modules
36
+ return if modules.length.zero?
37
+
38
+ @class_modules ||= "#{modules.map(&:camelize).join('::')}::"
39
+ end
40
+ end
41
+ 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.5.3'
5
+ VERSION = '0.8.0'
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.5.3
4
+ version: 0.8.0
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-06-30 00:00:00.000000000 Z
12
+ date: 2021-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.5'
48
+ version: '0.8'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0.5'
55
+ version: '0.8'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: bundler
58
58
  requirement: !ruby/object:Gem::Requirement
@@ -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
@@ -227,7 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
240
  - !ruby/object:Gem::Version
228
241
  version: '0'
229
242
  requirements: []
230
- rubygems_version: 3.0.3
243
+ rubygems_version: 3.1.4
231
244
  signing_key:
232
245
  specification_version: 4
233
246
  summary: Additional functionality gem for using SnFoil with Rails
@@ -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