power_api 0.2.0 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +104 -0
  3. data/.circleci/setup-rubygems.sh +3 -0
  4. data/.rubocop.yml +0 -1
  5. data/.ruby-version +1 -1
  6. data/CHANGELOG.md +19 -1
  7. data/Gemfile +2 -0
  8. data/Gemfile.lock +189 -166
  9. data/README.md +365 -103
  10. data/app/controllers/power_api/base_controller.rb +0 -1
  11. data/app/helpers/power_api/application_helper.rb +57 -0
  12. data/bin/clean_test_app +2 -0
  13. data/lib/generators/power_api/controller/controller_generator.rb +27 -15
  14. data/lib/generators/power_api/exposed_api_config/USAGE +5 -0
  15. data/lib/generators/power_api/exposed_api_config/exposed_api_config_generator.rb +58 -0
  16. data/lib/generators/power_api/install/install_generator.rb +2 -44
  17. data/lib/generators/power_api/internal_api_config/USAGE +5 -0
  18. data/lib/generators/power_api/internal_api_config/internal_api_config_generator.rb +31 -0
  19. data/lib/generators/power_api/version/version_generator.rb +2 -2
  20. data/lib/power_api/engine.rb +6 -1
  21. data/lib/power_api/errors.rb +2 -0
  22. data/lib/power_api/generator_helper/active_record_resource.rb +10 -6
  23. data/lib/power_api/generator_helper/ams_helper.rb +5 -11
  24. data/lib/power_api/generator_helper/api_helper.rb +61 -0
  25. data/lib/power_api/generator_helper/controller_helper.rb +45 -15
  26. data/lib/power_api/generator_helper/routes_helper.rb +22 -7
  27. data/lib/power_api/generator_helper/rspec_controller_helper.rb +306 -0
  28. data/lib/power_api/generator_helper/swagger_helper.rb +14 -24
  29. data/lib/power_api/generator_helpers.rb +2 -1
  30. data/lib/power_api/version.rb +1 -1
  31. data/power_api.gemspec +3 -2
  32. data/spec/dummy/Rakefile +1 -1
  33. data/spec/dummy/app/assets/config/manifest.js +0 -2
  34. data/spec/dummy/app/controllers/api/base_controller.rb +2 -0
  35. data/spec/dummy/app/controllers/api/internal/base_controller.rb +5 -0
  36. data/spec/dummy/app/controllers/api/internal/blogs_controller.rb +36 -0
  37. data/spec/dummy/app/controllers/application_controller.rb +0 -1
  38. data/spec/dummy/app/{assets/javascripts → javascript/packs}/application.js +2 -0
  39. data/spec/dummy/app/jobs/application_job.rb +5 -0
  40. data/spec/dummy/app/serializers/api/internal/blog_serializer.rb +12 -0
  41. data/spec/dummy/app/views/layouts/application.html.erb +3 -2
  42. data/spec/dummy/bin/rails +2 -2
  43. data/spec/dummy/bin/rake +2 -2
  44. data/spec/dummy/bin/setup +7 -12
  45. data/spec/dummy/config/application.rb +12 -16
  46. data/spec/dummy/config/boot.rb +1 -1
  47. data/spec/dummy/config/cable.yml +2 -2
  48. data/spec/dummy/config/database.yml +8 -16
  49. data/spec/dummy/config/environment.rb +1 -1
  50. data/spec/dummy/config/environments/development.rb +28 -6
  51. data/spec/dummy/config/environments/production.rb +45 -16
  52. data/spec/dummy/config/environments/test.rb +24 -7
  53. data/spec/dummy/config/initializers/active_model_serializers.rb +1 -0
  54. data/spec/dummy/config/initializers/api_pagination.rb +32 -0
  55. data/spec/dummy/config/initializers/assets.rb +0 -2
  56. data/spec/dummy/config/initializers/backtrace_silencers.rb +4 -3
  57. data/spec/dummy/config/initializers/content_security_policy.rb +28 -0
  58. data/spec/dummy/config/initializers/filter_parameter_logging.rb +3 -1
  59. data/spec/dummy/config/initializers/permissions_policy.rb +11 -0
  60. data/spec/dummy/config/locales/en.yml +1 -1
  61. data/spec/dummy/config/puma.rb +14 -27
  62. data/spec/dummy/config/routes.rb +2 -7
  63. data/spec/dummy/config/storage.yml +34 -0
  64. data/spec/dummy/config.ru +2 -1
  65. data/spec/dummy/spec/helpers/power_api/application_helper_spec.rb +171 -0
  66. data/spec/dummy/spec/lib/power_api/generator_helper/ams_helper_spec.rb +50 -12
  67. data/spec/dummy/spec/lib/power_api/generator_helper/api_helper_spec.rb +115 -0
  68. data/spec/dummy/spec/lib/power_api/generator_helper/controller_helper_spec.rb +126 -34
  69. data/spec/dummy/spec/lib/power_api/generator_helper/routes_helper_spec.rb +29 -5
  70. data/spec/dummy/spec/lib/power_api/generator_helper/rspec_controller_helper_spec.rb +559 -0
  71. data/spec/dummy/spec/lib/power_api/generator_helper/swagger_helper_spec.rb +10 -20
  72. data/spec/dummy/spec/support/shared_examples/active_record_resource_atrributes.rb +22 -3
  73. metadata +114 -87
  74. data/.hound.yml +0 -4
  75. data/.travis.yml +0 -16
  76. data/app/controllers/concerns/api/versioned.rb +0 -36
  77. data/lib/power_api/generator_helper/version_helper.rb +0 -16
  78. data/spec/dummy/app/assets/javascripts/cable.js +0 -13
  79. data/spec/dummy/app/controllers/concerns/api/versioned_spec.rb +0 -64
  80. data/spec/dummy/bin/bundle +0 -3
  81. data/spec/dummy/bin/update +0 -29
  82. data/spec/dummy/bin/yarn +0 -11
  83. data/spec/dummy/config/secrets.yml +0 -32
  84. data/spec/dummy/config/spring.rb +0 -6
  85. data/spec/dummy/spec/lib/power_api/generator_helper/version_helper_spec.rb +0 -55
@@ -4,7 +4,6 @@ module PowerApi
4
4
 
5
5
  include Api::Error
6
6
  include Api::Deprecated
7
- include Api::Versioned
8
7
  include Api::Filtered
9
8
 
10
9
  self.responder = ApiResponder
@@ -1,4 +1,61 @@
1
1
  module PowerApi
2
2
  module ApplicationHelper
3
+ VALID_SERIALIZER_OUTPUT_FORMATS = %i{json hash}
4
+
5
+ def serialize_resource(resource, options = {})
6
+ load_default_serializer_options(options)
7
+ serializable = ActiveModelSerializers::SerializableResource.new(resource, options)
8
+ serialized_data = serializable.serializable_hash
9
+ render_serialized_data(serialized_data, options)
10
+ rescue NoMethodError => e
11
+ if e.message.include?("undefined method `serializable_hash'")
12
+ raise ::PowerApi::InvalidSerializableResource.new(
13
+ "Invalid #{resource.class} resource given. Must be ActiveRecord instance or collection"
14
+ )
15
+ else
16
+ raise e
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def render_serialized_data(serialized_data, options)
23
+ output_format = options.delete(:output_format)
24
+ serialized_data = serialized_data[:root] if options[:root] == :root
25
+ return serialized_data if output_format == :hash
26
+
27
+ serialized_data.to_json
28
+ end
29
+
30
+ def load_default_serializer_options(options)
31
+ options[:namespace] ||= "Api::Internal"
32
+ options[:key_transform] ||= :camel_lower
33
+ options[:include_root] ||= false
34
+ options[:output_format] = format_serializer_output_format!(options[:output_format])
35
+ options[:key_transform] = :unaltered if options[:output_format] == :hash
36
+
37
+ load_root_option(options)
38
+ options
39
+ end
40
+
41
+ def load_root_option(options)
42
+ return if !!options.delete(:include_root)
43
+
44
+ options[:root] = :root
45
+ end
46
+
47
+ def format_serializer_output_format!(output_format)
48
+ return :json if output_format.blank?
49
+
50
+ output_format = output_format.to_s.to_sym
51
+
52
+ if !VALID_SERIALIZER_OUTPUT_FORMATS.include?(output_format)
53
+ raise ::PowerApi::InvalidSerializerOutputFormat.new(
54
+ "Only #{VALID_SERIALIZER_OUTPUT_FORMATS} values are allowed."
55
+ )
56
+ end
57
+
58
+ output_format
59
+ end
3
60
  end
4
61
  end
@@ -0,0 +1,2 @@
1
+ git checkout spec/dummy/*.rb
2
+ git status --porcelain | cut -c 3-10000000 | grep 'dummy' | xargs rm -rf
@@ -23,9 +23,10 @@ class PowerApi::ControllerGenerator < Rails::Generators::NamedBase
23
23
  class_option(
24
24
  :version_number,
25
25
  type: 'numeric',
26
- default: 1,
26
+ default: nil,
27
27
  aliases: '-v',
28
- desc: 'the API version number you want to add this controller'
28
+ desc: 'the API version number you want to add this controller. '\
29
+ 'Omitting this attribute will create a controller for the internal api.'
29
30
  )
30
31
 
31
32
  class_option(
@@ -95,29 +96,40 @@ class PowerApi::ControllerGenerator < Rails::Generators::NamedBase
95
96
  end
96
97
 
97
98
  def configure_swagger
98
- create_file(
99
- helper.swagger_resource_schema_path,
100
- helper.swagger_schema_tpl
101
- )
99
+ return unless helper.versioned_api?
100
+
101
+ create_swagger_schema
102
+ add_swagger_schema_to_definition
103
+ create_swagger_resource_spec
104
+ end
102
105
 
106
+ def add_rspec_tests
107
+ return if helper.versioned_api?
108
+
109
+ create_file(helper.resource_spec_path, helper.resource_spec_tpl)
110
+ helper.format_ruby_file(helper.resource_spec_path)
111
+ end
112
+
113
+ private
114
+
115
+ def create_swagger_schema
116
+ create_file(helper.swagger_resource_schema_path, helper.swagger_schema_tpl)
103
117
  helper.format_ruby_file(helper.swagger_resource_schema_path)
118
+ end
104
119
 
120
+ def add_swagger_schema_to_definition
105
121
  insert_into_file(
106
122
  helper.swagger_version_definition_path,
107
123
  helper.swagger_definition_entry,
108
124
  after: helper.swagger_definition_line_to_inject_schema
109
125
  )
126
+ end
110
127
 
111
- create_file(
112
- helper.swagger_resource_spec_path,
113
- helper.swagger_resource_spec_tpl
114
- )
115
-
128
+ def create_swagger_resource_spec
129
+ create_file(helper.swagger_resource_spec_path, helper.swagger_resource_spec_tpl)
116
130
  helper.format_ruby_file(helper.swagger_resource_spec_path)
117
131
  end
118
132
 
119
- private
120
-
121
133
  def add_nested_route
122
134
  line_to_replace = helper.parent_resource_routes_line_regex
123
135
  nested_resource_line = helper.resource_route_tpl(
@@ -136,13 +148,13 @@ class PowerApi::ControllerGenerator < Rails::Generators::NamedBase
136
148
 
137
149
  def add_normal_route(actions:)
138
150
  actions_for_only_option = actions.sort == self.class.valid_actions.sort ? [] : actions
139
- add_route(helper.api_version_routes_line_regex) do |match|
151
+ add_route(helper.api_current_route_namespace_line_regex) do |match|
140
152
  "#{match}\n#{helper.resource_route_tpl(actions: actions_for_only_option)}"
141
153
  end
142
154
  end
143
155
 
144
156
  def add_nested_parent_route
145
- add_route(helper.api_version_routes_line_regex) do |match|
157
+ add_route(helper.api_current_route_namespace_line_regex) do |match|
146
158
  "#{match}\n#{helper.resource_route_tpl(is_parent: true)}"
147
159
  end
148
160
  end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Configure exposed API in host app.
3
+
4
+ Example:
5
+ rails generate power_api:exposed_api_config
@@ -0,0 +1,58 @@
1
+ class PowerApi::ExposedApiConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ class_option(
5
+ :authenticated_resources,
6
+ type: 'array',
7
+ default: [],
8
+ desc: 'define which model or models will be token authenticatable'
9
+ )
10
+
11
+ def add_base_controller
12
+ create_file(
13
+ helper.exposed_base_controller_path,
14
+ helper.exposed_base_controller_tpl
15
+ )
16
+ end
17
+
18
+ def install_rswag
19
+ generate "rswag:ui:install"
20
+ generate "rswag:api:install"
21
+ generate "rswag:specs:install"
22
+
23
+ create_file(helper.rswag_ui_initializer_path, helper.rswag_ui_initializer_tpl, force: true)
24
+ create_file(helper.swagger_helper_path, helper.swagger_helper_tpl, force: true)
25
+ create_file(helper.spec_swagger_path)
26
+ create_file(helper.spec_integration_path)
27
+ end
28
+
29
+ def install_first_version
30
+ generate "power_api:version 1"
31
+ end
32
+
33
+ def install_simple_token_auth
34
+ create_file(
35
+ helper.simple_token_auth_initializer_path,
36
+ helper.simple_token_auth_initializer_tpl,
37
+ force: true
38
+ )
39
+
40
+ helper.authenticated_resources.each do |resource|
41
+ generate resource.authenticated_resource_migration
42
+
43
+ insert_into_file(
44
+ resource.path,
45
+ helper.simple_token_auth_method,
46
+ after: resource.class_definition_line
47
+ )
48
+ end
49
+ end
50
+
51
+ private
52
+
53
+ def helper
54
+ @helper ||= PowerApi::GeneratorHelpers.new(
55
+ authenticated_resources: options[:authenticated_resources]
56
+ )
57
+ end
58
+ end
@@ -1,36 +1,14 @@
1
1
  class PowerApi::InstallGenerator < Rails::Generators::Base
2
2
  source_root File.expand_path('templates', __dir__)
3
3
 
4
- class_option(
5
- :authenticated_resources,
6
- type: 'array',
7
- default: [],
8
- desc: 'define which model or models will be token authenticatable'
9
- )
10
-
11
4
  def create_api_base_controller
12
- create_file(helper.api_base_controller_path, helper.api_base_controller_tpl)
5
+ create_file(helper.api_main_base_controller_path, helper.api_main_base_controller_tpl)
13
6
  end
14
7
 
15
8
  def create_ams_initializer
16
9
  create_file(helper.ams_initializer_path, helper.ams_initializer_tpl)
17
10
  end
18
11
 
19
- def install_rswag
20
- generate "rswag:ui:install"
21
- generate "rswag:api:install"
22
- generate "rswag:specs:install"
23
-
24
- create_file(helper.rswag_ui_initializer_path, helper.rswag_ui_initializer_tpl, force: true)
25
- create_file(helper.swagger_helper_path, helper.swagger_helper_tpl, force: true)
26
- create_file(helper.spec_swagger_path)
27
- create_file(helper.spec_integration_path)
28
- end
29
-
30
- def install_first_version
31
- generate "power_api:version 1"
32
- end
33
-
34
12
  def install_api_pagination
35
13
  create_file(
36
14
  helper.api_pagination_initializer_path,
@@ -39,29 +17,9 @@ class PowerApi::InstallGenerator < Rails::Generators::Base
39
17
  )
40
18
  end
41
19
 
42
- def install_simple_token_auth
43
- create_file(
44
- helper.simple_token_auth_initializer_path,
45
- helper.simple_token_auth_initializer_tpl,
46
- force: true
47
- )
48
-
49
- helper.authenticated_resources.each do |resource|
50
- generate resource.authenticated_resource_migration
51
-
52
- insert_into_file(
53
- resource.path,
54
- helper.simple_token_auth_method,
55
- after: resource.class_definition_line
56
- )
57
- end
58
- end
59
-
60
20
  private
61
21
 
62
22
  def helper
63
- @helper ||= PowerApi::GeneratorHelpers.new(
64
- authenticated_resources: options[:authenticated_resources]
65
- )
23
+ @helper ||= PowerApi::GeneratorHelpers.new
66
24
  end
67
25
  end
@@ -0,0 +1,5 @@
1
+ Description:
2
+ Configure internal API in host app.
3
+
4
+ Example:
5
+ rails generate power_api:internal_api_config
@@ -0,0 +1,31 @@
1
+ class PowerApi::InternalApiConfigGenerator < Rails::Generators::Base
2
+ source_root File.expand_path('templates', __dir__)
3
+
4
+ def add_base_controller
5
+ create_file(
6
+ helper.internal_base_controller_path,
7
+ helper.internal_base_controller_tpl
8
+ )
9
+ end
10
+
11
+ def modify_routes
12
+ insert_into_file(
13
+ helper.routes_path,
14
+ after: helper.routes_first_line
15
+ ) do
16
+ helper.internal_route_tpl
17
+ end
18
+
19
+ helper.format_ruby_file(helper.routes_path)
20
+ end
21
+
22
+ def add_serializers_directory
23
+ create_file(helper.ams_serializers_path)
24
+ end
25
+
26
+ private
27
+
28
+ def helper
29
+ @helper ||= PowerApi::GeneratorHelpers.new
30
+ end
31
+ end
@@ -14,8 +14,8 @@ class PowerApi::VersionGenerator < Rails::Generators::NamedBase
14
14
 
15
15
  def add_base_controller
16
16
  create_file(
17
- helper.base_controller_path,
18
- helper.base_controller_tpl
17
+ helper.version_base_controller_path,
18
+ helper.version_base_controller_tpl
19
19
  )
20
20
  end
21
21
 
@@ -13,10 +13,11 @@ module PowerApi
13
13
  require_relative "./errors"
14
14
  require_relative "./generator_helper/controller_actions_helper"
15
15
  require_relative "./generator_helper/active_record_resource"
16
- require_relative "./generator_helper/version_helper"
16
+ require_relative "./generator_helper/api_helper"
17
17
  require_relative "./generator_helper/resource_helper"
18
18
  require_relative "./generator_helper/swagger_helper"
19
19
  require_relative "./generator_helper/ams_helper"
20
+ require_relative "./generator_helper/rspec_controller_helper"
20
21
  require_relative "./generator_helper/controller_helper"
21
22
  require_relative "./generator_helper/routes_helper"
22
23
  require_relative "./generator_helper/pagination_helper"
@@ -25,5 +26,9 @@ module PowerApi
25
26
  require_relative "./generator_helper/template_builder_helper"
26
27
  require_relative "./generator_helpers"
27
28
  end
29
+
30
+ initializer "power_api.view_helpers" do
31
+ ActiveSupport.on_load(:action_view) { include ::PowerApi::ApplicationHelper }
32
+ end
28
33
  end
29
34
  end
@@ -1,4 +1,6 @@
1
1
  module PowerApi
2
2
  class GeneratorError < StandardError; end
3
3
  class InvalidVersion < StandardError; end
4
+ class InvalidSerializerOutputFormat < StandardError; end
5
+ class InvalidSerializableResource < StandardError; end
4
6
  end
@@ -22,7 +22,10 @@ module PowerApi::GeneratorHelper::ActiveRecordResource
22
22
 
23
23
  def resource_attributes=(collection)
24
24
  attributes = format_attributes(collection)
25
- raise PowerApi::GeneratorError.new("at least one attribute must be added") if attributes.none?
25
+
26
+ if attributes.count == 1 && attributes.first[:name] == :id
27
+ raise PowerApi::GeneratorError.new("at least one attribute must be added")
28
+ end
26
29
 
27
30
  @resource_attributes = attributes
28
31
  end
@@ -85,12 +88,14 @@ module PowerApi::GeneratorHelper::ActiveRecordResource
85
88
 
86
89
  def permitted_attributes
87
90
  resource_attributes.reject do |attr|
88
- [:created_at, :updated_at].include?(attr[:name])
91
+ [:created_at, :updated_at, :id].include?(attr[:name])
89
92
  end
90
93
  end
91
94
 
92
- def required_resource_attributes
93
- permitted_attributes.select { |attr| attr[:required] }
95
+ def required_resource_attributes(include_id: false)
96
+ resource_attributes.select do |attr|
97
+ attr[:required] || (include_id && attr[:name] == :id)
98
+ end
94
99
  end
95
100
 
96
101
  def optional_resource_attributes
@@ -122,7 +127,6 @@ module PowerApi::GeneratorHelper::ActiveRecordResource
122
127
  def format_attributes(attrs)
123
128
  columns = resource_class.columns.inject([]) do |memo, col|
124
129
  col_name = col.name.to_sym
125
- next memo if col_name == :id
126
130
 
127
131
  memo << {
128
132
  name: col_name,
@@ -137,7 +141,7 @@ module PowerApi::GeneratorHelper::ActiveRecordResource
137
141
 
138
142
  return columns if attrs.blank?
139
143
 
140
- attrs = attrs.map(&:to_sym)
144
+ attrs = (attrs.map(&:to_sym) + [:id]).uniq
141
145
  columns.select { |col| attrs.include?(col[:name]) }
142
146
  end
143
147
 
@@ -2,7 +2,7 @@ module PowerApi::GeneratorHelper::AmsHelper
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  included do
5
- include PowerApi::GeneratorHelper::VersionHelper
5
+ include PowerApi::GeneratorHelper::ApiHelper
6
6
  include PowerApi::GeneratorHelper::ResourceHelper
7
7
  end
8
8
 
@@ -11,28 +11,22 @@ module PowerApi::GeneratorHelper::AmsHelper
11
11
  end
12
12
 
13
13
  def ams_serializer_path
14
- "app/serializers/api/v#{version_number}/#{resource.snake_case}_serializer.rb"
14
+ "app/serializers/#{api_file_path}/#{resource.snake_case}_serializer.rb"
15
15
  end
16
16
 
17
17
  def ams_serializers_path
18
- "app/serializers/api/v#{version_number}/.gitkeep"
18
+ "app/serializers/#{api_file_path}/.gitkeep"
19
19
  end
20
20
 
21
21
  def ams_initializer_tpl
22
22
  <<~INITIALIZER
23
- class ActiveModelSerializers::Adapter::JsonApi
24
- def self.default_key_transform
25
- :unaltered
26
- end
27
- end
28
-
29
- ActiveModelSerializers.config.adapter = :json_api
23
+ ActiveModelSerializers.config.adapter = :json
30
24
  INITIALIZER
31
25
  end
32
26
 
33
27
  def ams_serializer_tpl
34
28
  <<~SERIALIZER
35
- class Api::V#{version_number}::#{resource.camel}Serializer < ActiveModel::Serializer
29
+ class #{api_class}::#{resource.camel}Serializer < ActiveModel::Serializer
36
30
  type :#{resource.snake_case}
37
31
 
38
32
  attributes(
@@ -0,0 +1,61 @@
1
+ module PowerApi::GeneratorHelper::ApiHelper
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ attr_reader :version_number
6
+ end
7
+
8
+ def version_number=(value)
9
+ if value.blank?
10
+ @version_number = nil
11
+ return
12
+ end
13
+
14
+ @version_number = value.to_s.to_i
15
+ raise PowerApi::GeneratorError.new("invalid version number") if version_number < 1
16
+ end
17
+
18
+ def first_version?
19
+ version_number.to_i == 1
20
+ end
21
+
22
+ def versioned_api?
23
+ !!version_number
24
+ end
25
+
26
+ def api_file_path
27
+ return version_file_path if versioned_api?
28
+
29
+ internal_file_path
30
+ end
31
+
32
+ def version_file_path
33
+ "#{exposed_file_path}/v#{version_number}"
34
+ end
35
+
36
+ def internal_file_path
37
+ "api/internal"
38
+ end
39
+
40
+ def exposed_file_path
41
+ "api/exposed"
42
+ end
43
+
44
+ def api_class
45
+ return version_class if versioned_api?
46
+
47
+ internal_class
48
+ end
49
+
50
+ def version_class
51
+ "#{exposed_class}::V#{version_number}"
52
+ end
53
+
54
+ def internal_class
55
+ "Api::Internal"
56
+ end
57
+
58
+ def exposed_class
59
+ "Api::Exposed"
60
+ end
61
+ end
@@ -3,7 +3,7 @@ module PowerApi::GeneratorHelper::ControllerHelper
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- include PowerApi::GeneratorHelper::VersionHelper
6
+ include PowerApi::GeneratorHelper::ApiHelper
7
7
  include PowerApi::GeneratorHelper::ResourceHelper
8
8
  include PowerApi::GeneratorHelper::PaginationHelper
9
9
  include PowerApi::GeneratorHelper::SimpleTokenAuthHelper
@@ -13,30 +13,56 @@ module PowerApi::GeneratorHelper::ControllerHelper
13
13
  attr_accessor :allow_filters
14
14
  end
15
15
 
16
- def api_base_controller_path
16
+ def api_main_base_controller_path
17
17
  "app/controllers/api/base_controller.rb"
18
18
  end
19
19
 
20
- def base_controller_path
21
- "app/controllers/api/v#{version_number}/base_controller.rb"
20
+ def exposed_base_controller_path
21
+ "app/controllers/#{exposed_file_path}/base_controller.rb"
22
+ end
23
+
24
+ def internal_base_controller_path
25
+ "app/controllers/#{internal_file_path}/base_controller.rb"
26
+ end
27
+
28
+ def version_base_controller_path
29
+ "app/controllers/#{version_file_path}/base_controller.rb"
22
30
  end
23
31
 
24
32
  def resource_controller_path
25
- "app/controllers/api/v#{version_number}/#{resource.plural}_controller.rb"
33
+ "app/controllers/#{api_file_path}/#{resource.plural}_controller.rb"
26
34
  end
27
35
 
28
- def api_base_controller_tpl
36
+ def api_main_base_controller_tpl
29
37
  <<~CONTROLLER
30
38
  class Api::BaseController < PowerApi::BaseController
31
39
  end
32
40
  CONTROLLER
33
41
  end
34
42
 
35
- def base_controller_tpl
43
+ def exposed_base_controller_tpl
44
+ <<~CONTROLLER
45
+ class #{exposed_class}::BaseController < Api::BaseController
46
+ skip_before_action :verify_authenticity_token
47
+ end
48
+ CONTROLLER
49
+ end
50
+
51
+ def internal_base_controller_tpl
36
52
  <<~CONTROLLER
37
- class Api::V#{version_number}::BaseController < Api::BaseController
53
+ class #{internal_class}::BaseController < Api::BaseController
38
54
  before_action do
39
- self.namespace_for_serializer = ::Api::V#{version_number}
55
+ self.namespace_for_serializer = ::#{internal_class}
56
+ end
57
+ end
58
+ CONTROLLER
59
+ end
60
+
61
+ def version_base_controller_tpl
62
+ <<~CONTROLLER
63
+ class #{version_class}::BaseController < #{exposed_class}::BaseController
64
+ before_action do
65
+ self.namespace_for_serializer = ::#{version_class}
40
66
  end
41
67
  end
42
68
  CONTROLLER
@@ -45,7 +71,7 @@ module PowerApi::GeneratorHelper::ControllerHelper
45
71
  def resource_controller_tpl
46
72
  tpl_class(
47
73
  ctrl_tpl_class_definition_line,
48
- ctrl_tpl_acts_as_token_authentication_handler,
74
+ ctrl_tpl_authentication_code,
49
75
  ctrl_tpl_index,
50
76
  ctrl_tpl_show,
51
77
  ctrl_tpl_create,
@@ -62,15 +88,18 @@ module PowerApi::GeneratorHelper::ControllerHelper
62
88
  private
63
89
 
64
90
  def ctrl_tpl_class_definition_line
65
- "Api::V#{version_number}::#{resource.camel_plural}Controller < \
66
- Api::V#{version_number}::BaseController"
91
+ "#{api_class}::#{resource.camel_plural}Controller < #{api_class}::BaseController"
67
92
  end
68
93
 
69
- def ctrl_tpl_acts_as_token_authentication_handler
94
+ def ctrl_tpl_authentication_code
70
95
  return unless authenticated_resource?
71
96
 
72
- "acts_as_token_authentication_handler_for #{authenticated_resource.camel}, \
97
+ if versioned_api?
98
+ return "acts_as_token_authentication_handler_for #{authenticated_resource.camel}, \
73
99
  fallback: :exception\n"
100
+ end
101
+
102
+ "before_action :authenticate_#{authenticated_resource.snake_case}!\n"
74
103
  end
75
104
 
76
105
  def ctrl_tpl_index
@@ -96,7 +125,8 @@ fallback: :exception\n"
96
125
 
97
126
  concat_tpl_method(
98
127
  "update",
99
- "respond_with #{resource.snake_case}.update!(#{resource.snake_case}_params)"
128
+ "#{resource.snake_case}.update!(#{resource.snake_case}_params)",
129
+ "respond_with #{resource.snake_case}"
100
130
  )
101
131
  end
102
132