power_api 0.1.0
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 +7 -0
- data/.coveralls.yml +1 -0
- data/.gitignore +9 -0
- data/.hound.yml +4 -0
- data/.rspec +3 -0
- data/.rubocop.yml +479 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +310 -0
- data/Guardfile +15 -0
- data/LICENSE.txt +21 -0
- data/README.md +904 -0
- data/Rakefile +10 -0
- data/app/assets/config/power_api_manifest.js +2 -0
- data/app/assets/images/power_api/.keep +0 -0
- data/app/assets/javascripts/power_api/application.js +13 -0
- data/app/assets/stylesheets/power_api/application.css +15 -0
- data/app/controllers/concerns/api/deprecated.rb +23 -0
- data/app/controllers/concerns/api/error.rb +37 -0
- data/app/controllers/concerns/api/filtered.rb +15 -0
- data/app/controllers/concerns/api/versioned.rb +36 -0
- data/app/controllers/power_api/base_controller.rb +14 -0
- data/app/helpers/power_api/application_helper.rb +4 -0
- data/app/jobs/power_api/application_job.rb +4 -0
- data/app/mailers/power_api/application_mailer.rb +6 -0
- data/app/models/power_api/application_record.rb +5 -0
- data/app/responders/api_responder.rb +13 -0
- data/app/views/layouts/power_api/application.html.erb +14 -0
- data/bin/rails +14 -0
- data/config/routes.rb +2 -0
- data/lib/generators/power_api/controller/USAGE +5 -0
- data/lib/generators/power_api/controller/controller_generator.rb +152 -0
- data/lib/generators/power_api/install/USAGE +5 -0
- data/lib/generators/power_api/install/install_generator.rb +67 -0
- data/lib/generators/power_api/version/USAGE +5 -0
- data/lib/generators/power_api/version/version_generator.rb +54 -0
- data/lib/power_api.rb +35 -0
- data/lib/power_api/engine.rb +28 -0
- data/lib/power_api/errors.rb +4 -0
- data/lib/power_api/generator_helper/active_record_resource.rb +192 -0
- data/lib/power_api/generator_helper/ams_helper.rb +43 -0
- data/lib/power_api/generator_helper/controller_helper.rb +184 -0
- data/lib/power_api/generator_helper/pagination_helper.rb +48 -0
- data/lib/power_api/generator_helper/resource_helper.rb +33 -0
- data/lib/power_api/generator_helper/routes_helper.rb +91 -0
- data/lib/power_api/generator_helper/rubocop_helper.rb +11 -0
- data/lib/power_api/generator_helper/simple_token_auth_helper.rb +124 -0
- data/lib/power_api/generator_helper/swagger_helper.rb +463 -0
- data/lib/power_api/generator_helper/template_builder_helper.rb +23 -0
- data/lib/power_api/generator_helper/version_helper.rb +16 -0
- data/lib/power_api/generator_helpers.rb +25 -0
- data/lib/power_api/version.rb +3 -0
- data/lib/tasks/power_api_tasks.rake +4 -0
- data/power_api.gemspec +44 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +5 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/concerns/api/deprecated_spec.rb +37 -0
- data/spec/dummy/app/controllers/concerns/api/error_spec.rb +97 -0
- data/spec/dummy/app/controllers/concerns/api/filtered_spec.rb +42 -0
- data/spec/dummy/app/controllers/concerns/api/versioned_spec.rb +64 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/models/application_record.rb +3 -0
- data/spec/dummy/app/models/blog.rb +5 -0
- data/spec/dummy/app/models/portfolio.rb +5 -0
- data/spec/dummy/app/models/user.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +38 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +26 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +54 -0
- data/spec/dummy/config/environments/production.rb +91 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +14 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +56 -0
- data/spec/dummy/config/routes.rb +12 -0
- data/spec/dummy/config/secrets.yml +32 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/db/migrate/20190322205209_create_blogs.rb +10 -0
- data/spec/dummy/db/migrate/20200215225917_create_users.rb +9 -0
- data/spec/dummy/db/migrate/20200227150449_create_portfolios.rb +10 -0
- data/spec/dummy/db/migrate/20200227150548_add_portfolio_id_blogs.rb +5 -0
- data/spec/dummy/db/schema.rb +38 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/spec/assets/image.png +0 -0
- data/spec/dummy/spec/assets/video.mp4 +0 -0
- data/spec/dummy/spec/factories/blogs.rb +7 -0
- data/spec/dummy/spec/factories/portfolios.rb +6 -0
- data/spec/dummy/spec/factories/users.rb +5 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/ams_helper_spec.rb +87 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/controller_helper_spec.rb +361 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/pagination_helper_spec.rb +56 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/resource_helper_spec.rb +31 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/routes_helper_spec.rb +179 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/simple_token_auth_helper_spec.rb +164 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/swagger_helper_spec.rb +451 -0
- data/spec/dummy/spec/lib/power_api/generator_helper/version_helper_spec.rb +55 -0
- data/spec/dummy/spec/support/shared_examples/active_record_resource.rb +101 -0
- data/spec/dummy/spec/support/shared_examples/active_record_resource_atrributes.rb +164 -0
- data/spec/dummy/spec/support/test_generator_helpers.rb +29 -0
- data/spec/dummy/spec/support/test_helpers.rb +11 -0
- data/spec/rails_helper.rb +49 -0
- data/spec/spec_helper.rb +9 -0
- metadata +602 -0
data/Rakefile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
begin
|
|
2
|
+
require "bundler/setup"
|
|
3
|
+
rescue LoadError
|
|
4
|
+
puts "You must `gem install bundler` and `bundle install` to run rake tasks"
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
|
8
|
+
load "rails/tasks/engine.rake"
|
|
9
|
+
load "rails/tasks/statistics.rake"
|
|
10
|
+
Bundler::GemHelper.install_tasks
|
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
|
2
|
+
// listed below.
|
|
3
|
+
//
|
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
|
6
|
+
//
|
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
|
9
|
+
//
|
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
|
11
|
+
// about supported directives.
|
|
12
|
+
//
|
|
13
|
+
//= require_tree .
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
|
3
|
+
* listed below.
|
|
4
|
+
*
|
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
|
7
|
+
*
|
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
|
11
|
+
* It is generally better to create a new file per style scope.
|
|
12
|
+
*
|
|
13
|
+
*= require_tree .
|
|
14
|
+
*= require_self
|
|
15
|
+
*/
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Api::Deprecated
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
class_methods do
|
|
5
|
+
attr_accessor :deprecated_actions
|
|
6
|
+
|
|
7
|
+
def deprecate(*actions)
|
|
8
|
+
self.deprecated_actions ||= {}
|
|
9
|
+
self.deprecated_actions[to_s] = actions
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
included do
|
|
14
|
+
after_action :deprecate_actions
|
|
15
|
+
|
|
16
|
+
def deprecate_actions
|
|
17
|
+
return unless self.class.deprecated_actions
|
|
18
|
+
|
|
19
|
+
deprecated_actions = self.class.deprecated_actions[self.class.to_s]
|
|
20
|
+
response.headers["Deprecated"] = true if deprecated_actions.include?(params[:action].to_sym)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module Api::Error
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
included do
|
|
5
|
+
rescue_from "Exception" do |exception|
|
|
6
|
+
logger.error exception.message
|
|
7
|
+
logger.error exception.backtrace.join("\n")
|
|
8
|
+
respond_api_error(:internal_server_error, message: "server_error",
|
|
9
|
+
type: exception.class.to_s,
|
|
10
|
+
detail: exception.message)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
rescue_from "ActiveRecord::RecordNotFound" do |exception|
|
|
14
|
+
respond_api_error(:not_found, message: "record_not_found",
|
|
15
|
+
detail: exception.message)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
rescue_from "ActiveModel::ForbiddenAttributesError" do |exception|
|
|
19
|
+
respond_api_error(:bad_request, message: "protected_attributes",
|
|
20
|
+
detail: exception.message)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
rescue_from "ActiveRecord::RecordInvalid" do |exception|
|
|
24
|
+
respond_api_error(:bad_request, message: "invalid_attributes",
|
|
25
|
+
errors: exception.record.errors)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
rescue_from "PowerApi::InvalidVersion" do |exception|
|
|
29
|
+
respond_api_error(:bad_request, message: "invalid_api_version",
|
|
30
|
+
errors: exception.message)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def respond_api_error(status, error = {})
|
|
35
|
+
render json: error, status: status
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Api::Filtered
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
def filtered_collection(collection)
|
|
5
|
+
collection.ransack(query_string_filters).result
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def query_string_filters
|
|
11
|
+
return {} if params[:q].blank?
|
|
12
|
+
|
|
13
|
+
params[:q]&.to_unsafe_h || {}
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module Api::Versioned
|
|
2
|
+
extend ActiveSupport::Concern
|
|
3
|
+
|
|
4
|
+
DEFAULT_API_VERSIONS = 1
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
before_action :check_api_version!
|
|
8
|
+
after_action :add_version_to_content_type
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def available_api_versions
|
|
14
|
+
ENV.fetch("AVAILABLE_API_VERSIONS", DEFAULT_API_VERSIONS)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def check_api_version!
|
|
18
|
+
return if (1..available_api_versions) === version_number
|
|
19
|
+
|
|
20
|
+
raise ::PowerApi::InvalidVersion.new("invalid API version")
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def version_number
|
|
24
|
+
@version_number ||= begin
|
|
25
|
+
v = request.headers["Accept"].to_s.match(/version=(\d+)/)&.captures&.first.to_i
|
|
26
|
+
v.zero? ? DEFAULT_API_VERSIONS : v
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def add_version_to_content_type
|
|
31
|
+
content_type_header = response.headers["Content-Type"]
|
|
32
|
+
parts = !content_type_header ? [] : content_type_header.to_s.split(";").map(&:strip)
|
|
33
|
+
parts << "version=#{@version_number}"
|
|
34
|
+
response.headers["Content-Type"] = parts.join("; ")
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module PowerApi
|
|
2
|
+
class BaseController < ApplicationController
|
|
3
|
+
include Rails::Pagination
|
|
4
|
+
|
|
5
|
+
include Api::Error
|
|
6
|
+
include Api::Deprecated
|
|
7
|
+
include Api::Versioned
|
|
8
|
+
include Api::Filtered
|
|
9
|
+
|
|
10
|
+
self.responder = ApiResponder
|
|
11
|
+
|
|
12
|
+
respond_to :json
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class ApiResponder < ActionController::Responder
|
|
2
|
+
def api_behavior
|
|
3
|
+
raise MissingRenderer.new(format) unless has_renderer?
|
|
4
|
+
|
|
5
|
+
if delete?
|
|
6
|
+
head :no_content
|
|
7
|
+
elsif post?
|
|
8
|
+
display resource, status: :created
|
|
9
|
+
else
|
|
10
|
+
display resource
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<title>Power api</title>
|
|
5
|
+
<%= stylesheet_link_tag "power_api/application", media: "all" %>
|
|
6
|
+
<%= javascript_include_tag "power_api/application" %>
|
|
7
|
+
<%= csrf_meta_tags %>
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
|
|
11
|
+
<%= yield %>
|
|
12
|
+
|
|
13
|
+
</body>
|
|
14
|
+
</html>
|
data/bin/rails
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
|
3
|
+
# installed from the root of your application.
|
|
4
|
+
|
|
5
|
+
ENGINE_ROOT = File.expand_path('../..', __FILE__)
|
|
6
|
+
ENGINE_PATH = File.expand_path('../../lib/power_api/engine', __FILE__)
|
|
7
|
+
APP_PATH = File.expand_path('../../spec/dummy/config/application', __FILE__)
|
|
8
|
+
|
|
9
|
+
# Set up gems listed in the Gemfile.
|
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
|
|
11
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
|
12
|
+
|
|
13
|
+
require 'rails/all'
|
|
14
|
+
require 'rails/engine/commands'
|
data/config/routes.rb
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
class PowerApi::ControllerGenerator < Rails::Generators::NamedBase
|
|
2
|
+
source_root File.expand_path('templates', __dir__)
|
|
3
|
+
|
|
4
|
+
class_option(
|
|
5
|
+
:attributes,
|
|
6
|
+
type: 'array',
|
|
7
|
+
default: [],
|
|
8
|
+
aliases: '-a',
|
|
9
|
+
desc: 'attributes to show in serializer'
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
class_option(
|
|
13
|
+
:version_number,
|
|
14
|
+
type: 'numeric',
|
|
15
|
+
default: 1,
|
|
16
|
+
aliases: '-v',
|
|
17
|
+
desc: 'the API version number you want to add this controller'
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
class_option(
|
|
21
|
+
:use_paginator,
|
|
22
|
+
type: 'boolean',
|
|
23
|
+
default: false,
|
|
24
|
+
desc: 'to indicate whether the controller will use pager or not'
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
class_option(
|
|
28
|
+
:allow_filters,
|
|
29
|
+
type: 'boolean',
|
|
30
|
+
default: false,
|
|
31
|
+
desc: 'to indicate whether the controller will allow query string filters or not'
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
class_option(
|
|
35
|
+
:authenticate_with,
|
|
36
|
+
type: 'string',
|
|
37
|
+
default: nil,
|
|
38
|
+
desc: "to indicate if authorization is required to access the controller"
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
class_option(
|
|
42
|
+
:parent_resource,
|
|
43
|
+
type: 'string',
|
|
44
|
+
default: nil,
|
|
45
|
+
desc: "to indicate if the current resource is nested in another"
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
class_option(
|
|
49
|
+
:owned_by_authenticated_resource,
|
|
50
|
+
type: 'boolean',
|
|
51
|
+
default: false,
|
|
52
|
+
desc: "to indicate if the resource's owner must be the authorized resource"
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
def create_controller
|
|
56
|
+
create_file(
|
|
57
|
+
helper.resource_controller_path,
|
|
58
|
+
helper.resource_controller_tpl
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
helper.format_ruby_file(helper.resource_controller_path)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def add_routes
|
|
65
|
+
if helper.parent_resource?
|
|
66
|
+
add_normal_route(actions: ["show", "update", "destroy"])
|
|
67
|
+
add_nested_route
|
|
68
|
+
else
|
|
69
|
+
add_normal_route
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
helper.format_ruby_file(helper.routes_path)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def create_serializer
|
|
76
|
+
create_file(
|
|
77
|
+
helper.ams_serializer_path,
|
|
78
|
+
helper.ams_serializer_tpl
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
helper.format_ruby_file(helper.ams_serializer_path)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
def configure_swagger
|
|
85
|
+
create_file(
|
|
86
|
+
helper.swagger_resource_schema_path,
|
|
87
|
+
helper.swagger_schema_tpl
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
helper.format_ruby_file(helper.swagger_resource_schema_path)
|
|
91
|
+
|
|
92
|
+
insert_into_file(
|
|
93
|
+
helper.swagger_version_definition_path,
|
|
94
|
+
helper.swagger_definition_entry,
|
|
95
|
+
after: helper.swagger_definition_line_to_inject_schema
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
create_file(
|
|
99
|
+
helper.swagger_resource_spec_path,
|
|
100
|
+
helper.swagger_resource_spec_tpl
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
helper.format_ruby_file(helper.swagger_resource_spec_path)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
private
|
|
107
|
+
|
|
108
|
+
def add_nested_route
|
|
109
|
+
line_to_replace = helper.parent_resource_routes_line_regex
|
|
110
|
+
nested_resource_line = helper.resource_route_tpl(actions: ['index', 'create'])
|
|
111
|
+
add_nested_parent_route unless helper.parent_route_exist?
|
|
112
|
+
|
|
113
|
+
if helper.parent_route_already_have_children?
|
|
114
|
+
add_route(line_to_replace) { |match| "#{match}\n#{nested_resource_line}" }
|
|
115
|
+
else
|
|
116
|
+
add_route(line_to_replace) do |match|
|
|
117
|
+
"#{match.delete_suffix('\n')} do\n#{nested_resource_line}\nend\n"
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def add_normal_route(actions: [])
|
|
123
|
+
add_route(helper.api_version_routes_line_regex) do |match|
|
|
124
|
+
"#{match}\n#{helper.resource_route_tpl(actions: actions)}"
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def add_nested_parent_route
|
|
129
|
+
add_route(helper.api_version_routes_line_regex) do |match|
|
|
130
|
+
"#{match}\n#{helper.resource_route_tpl(is_parent: true)}"
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def add_route(line_to_replace, &block)
|
|
135
|
+
gsub_file helper.routes_path, line_to_replace do |match|
|
|
136
|
+
block.call(match)
|
|
137
|
+
end
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def helper
|
|
141
|
+
@helper ||= PowerApi::GeneratorHelpers.new(
|
|
142
|
+
version_number: options[:version_number],
|
|
143
|
+
resource: file_name,
|
|
144
|
+
authenticated_resource: options[:authenticate_with],
|
|
145
|
+
parent_resource: options[:parent_resource],
|
|
146
|
+
owned_by_authenticated_resource: options[:owned_by_authenticated_resource],
|
|
147
|
+
resource_attributes: options[:attributes],
|
|
148
|
+
use_paginator: options[:use_paginator],
|
|
149
|
+
allow_filters: options[:allow_filters]
|
|
150
|
+
)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
class PowerApi::InstallGenerator < 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 create_api_base_controller
|
|
12
|
+
create_file(helper.api_base_controller_path, helper.api_base_controller_tpl)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def create_ams_initializer
|
|
16
|
+
create_file(helper.ams_initializer_path, helper.ams_initializer_tpl)
|
|
17
|
+
end
|
|
18
|
+
|
|
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
|
+
def install_api_pagination
|
|
35
|
+
create_file(
|
|
36
|
+
helper.api_pagination_initializer_path,
|
|
37
|
+
helper.api_pagination_initializer_tpl,
|
|
38
|
+
force: true
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
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
|
+
private
|
|
61
|
+
|
|
62
|
+
def helper
|
|
63
|
+
@helper ||= PowerApi::GeneratorHelpers.new(
|
|
64
|
+
authenticated_resources: options[:authenticated_resources]
|
|
65
|
+
)
|
|
66
|
+
end
|
|
67
|
+
end
|