api_scaffold 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +22 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +10 -0
- data/api_scaffold.gemspec +37 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/api_scaffold.rb +5 -0
- data/lib/api_scaffold/version.rb +3 -0
- data/lib/generators/api_scaffold/api_scaffold_generator.rb +55 -0
- data/lib/generators/api_scaffold/generator_helpers.rb +57 -0
- data/lib/generators/api_scaffold/templates/controllers/controller.rb +54 -0
- data/lib/generators/api_scaffold/templates/controllers/serializer_controller.rb +54 -0
- data/lib/generators/api_scaffold/templates/tests/rspec/controller_spec.rb +157 -0
- data/lib/generators/api_scaffold/templates/tests/test_unit/controller_spec.rb +44 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 322f5e4d40465bca51bdae9c6707795f740b050d7f2b3eff64fb7711484ab2d5
|
4
|
+
data.tar.gz: 2797937c81bd0b6b59377dbeee9c56f7183dcf93c50dc27180490f0c42d988ac
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70d5f7938fe118db3fade54e808e6234579af6bba650cece5019571f3a98d502f9339308d8903ed358723650c5c333702721045639ecef0ef87b8978e34ebc68
|
7
|
+
data.tar.gz: fef80142a9060a3df04c2f58b56186f27598625d7344f90f4794bfdd04902e912b2b9889d02bff6353a344ec756be35cdbd7f59578ae6212c2bea217ddf52a5d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
api_scaffold (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.11.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
api_scaffold!
|
17
|
+
bundler (~> 1.16)
|
18
|
+
minitest (~> 5.0)
|
19
|
+
rake (~> 10.0)
|
20
|
+
|
21
|
+
BUNDLED WITH
|
22
|
+
1.16.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Stefan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# ApiScaffold
|
2
|
+
|
3
|
+
A useful Rails API generator for scaffolding. Compatible with :
|
4
|
+
- Fast JSON API/Active Model Serializers
|
5
|
+
- Rspec/TestUnit/Fixtures/FactoryBot
|
6
|
+
- Api Pagination
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'api_scaffold'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install api_scaffold
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
Execute this line in your terminal from your rails directory:
|
27
|
+
|
28
|
+
$ rails g api_scaffold Book title description:text
|
29
|
+
|
30
|
+
If you wish to specify the api version:
|
31
|
+
|
32
|
+
$ rails g api_scaffold Book title description:text --api-version=2
|
33
|
+
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/stefatkins/api_scaffold.
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "api_scaffold/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "api_scaffold"
|
8
|
+
spec.version = ApiScaffold::VERSION
|
9
|
+
spec.authors = ["Stefan Atkinson"]
|
10
|
+
spec.email = ["stefan.atkinson69@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "A gem for scaffolding rails api templates"
|
13
|
+
spec.homepage = "https://github.com/stefatkins/api_scaffold"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
# Specify which files should be added to the gem when it is released.
|
26
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
27
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
28
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
29
|
+
end
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
37
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "api_scaffold"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/api_scaffold.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
|
2
|
+
require 'generators/api_scaffold/generator_helpers'
|
3
|
+
|
4
|
+
module ApiScaffold
|
5
|
+
module Generators
|
6
|
+
class ApiScaffoldGenerator < Rails::Generators::NamedBase
|
7
|
+
include Rails::Generators::ResourceHelpers
|
8
|
+
include ApiScaffold::Generators::GeneratorHelpers
|
9
|
+
namespace "api_scaffold"
|
10
|
+
argument :attributes, type: :array, default: [], banner: "field[:type][:index] field[:type][:index]"
|
11
|
+
attr_accessor :attributes
|
12
|
+
class_option :orm
|
13
|
+
class_option :api_version, type: :string, default: 1, desc: "api version"
|
14
|
+
source_root File.expand_path('../templates', __FILE__)
|
15
|
+
|
16
|
+
def initialize(args, *options) #:nodoc:
|
17
|
+
super
|
18
|
+
end
|
19
|
+
|
20
|
+
hook_for :orm, in: :rails, required: true, as: :model do |model_generator|
|
21
|
+
invoke model_generator
|
22
|
+
end
|
23
|
+
|
24
|
+
def create_controller_files
|
25
|
+
if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
|
26
|
+
template "controllers/serializer_controller.rb", File.join("app/controllers/", prefix, "#{controller_file_name}_controller.rb")
|
27
|
+
else
|
28
|
+
template "controllers/controller.rb", File.join("app/controllers/", prefix, "#{controller_file_name}_controller.rb")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_controller_test_files
|
33
|
+
if test_framework == :rspec
|
34
|
+
template "tests/rspec/controller_spec.rb", File.join("spec/controllers", prefix, "#{controller_file_name}_controller_spec.rb")
|
35
|
+
else
|
36
|
+
template "tests/test_unit/controller_spec.rb", File.join("test/controllers", prefix, "#{controller_file_name}_controller_test.rb")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_routes
|
41
|
+
route "
|
42
|
+
scope :api, defaults: { format: :json } do
|
43
|
+
namespace :#{prefix} do
|
44
|
+
resources :#{controller_name}, except: [:new, :edit]
|
45
|
+
end
|
46
|
+
end\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
def create_serializer_files
|
50
|
+
invoke "serializer" if gem_available?('fast_jsonapi') || gem_available?('active_model_serializers')
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module ApiScaffold
|
2
|
+
module Generators
|
3
|
+
module GeneratorHelpers
|
4
|
+
|
5
|
+
def prefix
|
6
|
+
"v#{options[:api_version]}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def prefixed_class_name
|
10
|
+
"#{prefix.capitalize}::#{class_name}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def prefixed_controller_class_name
|
14
|
+
"#{prefix.capitalize}::#{controller_class_name}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def prefixed_url(resource)
|
18
|
+
[prefix, resource, 'url'].join('_')
|
19
|
+
end
|
20
|
+
|
21
|
+
def gem_available?(gem_name)
|
22
|
+
Gem::Specification::find_all_by_name(gem_name).any?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_framework
|
26
|
+
Rails.application.config.generators.options[:rails][:test_framework]
|
27
|
+
end
|
28
|
+
|
29
|
+
def fixture_replacement
|
30
|
+
Rails.application.config.generators.options[:rails][:fixture_replacement]
|
31
|
+
end
|
32
|
+
|
33
|
+
def fixture_name
|
34
|
+
if mountable_engine?
|
35
|
+
(namespace_dirs + [table_name]).join("_")
|
36
|
+
else
|
37
|
+
table_name
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def attributes_string
|
42
|
+
attributes_hash.map { |k, v| "#{k}: #{v}" }.join(", ")
|
43
|
+
end
|
44
|
+
def attributes_hash
|
45
|
+
return {} if attributes_names.empty?
|
46
|
+
|
47
|
+
attributes_names.map do |name|
|
48
|
+
if %w(password password_confirmation).include?(name) && attributes.any?(&:password_digest?)
|
49
|
+
["#{name}", "'secret'"]
|
50
|
+
else
|
51
|
+
["#{name}", "@#{singular_table_name}.#{name}"]
|
52
|
+
end
|
53
|
+
end.sort.to_h
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= prefixed_controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
|
8
|
+
|
9
|
+
def index
|
10
|
+
@<%= plural_table_name %> = <%= 'paginate ' if gem_available?('api-pagination') %><%= orm_class.all(class_name) %>
|
11
|
+
render json: <%= "@#{plural_table_name}" %>
|
12
|
+
end
|
13
|
+
|
14
|
+
def show
|
15
|
+
render json: <%= "@#{singular_table_name}" %>
|
16
|
+
end
|
17
|
+
|
18
|
+
def create
|
19
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
20
|
+
|
21
|
+
if @<%= orm_instance.save %>
|
22
|
+
render json: <%= "@#{singular_table_name}" %>, status: :created, location: [:<%= prefix %>, <%= "@#{singular_table_name}" %>]
|
23
|
+
else
|
24
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
30
|
+
render json: <%= "@#{singular_table_name}" %>
|
31
|
+
else
|
32
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy
|
37
|
+
@<%= orm_instance.destroy %>
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def set_<%= singular_table_name %>
|
43
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
44
|
+
end
|
45
|
+
|
46
|
+
def <%= "#{singular_table_name}_params" %>
|
47
|
+
<%- if attributes_names.empty? -%>
|
48
|
+
params.fetch(:<%= singular_table_name %>, {})
|
49
|
+
<%- else -%>
|
50
|
+
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
51
|
+
<%- end -%>
|
52
|
+
end
|
53
|
+
end
|
54
|
+
<% end -%>
|
@@ -0,0 +1,54 @@
|
|
1
|
+
<% if namespaced? -%>
|
2
|
+
require_dependency "<%= namespaced_path %>/application_controller"
|
3
|
+
|
4
|
+
<% end -%>
|
5
|
+
<% module_namespacing do -%>
|
6
|
+
class <%= prefixed_controller_class_name %>Controller < ApplicationController
|
7
|
+
before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
|
8
|
+
|
9
|
+
def index
|
10
|
+
@<%= plural_table_name %> = <%= 'paginate ' if gem_available?('api-pagination') %><%= orm_class.all(class_name) %>
|
11
|
+
render json: <%= class_name %>Serializer.new(<%= "@#{plural_table_name}" %>)
|
12
|
+
end
|
13
|
+
|
14
|
+
def show
|
15
|
+
render json: <%= class_name %>Serializer.new(<%= "@#{singular_table_name}" %>)
|
16
|
+
end
|
17
|
+
|
18
|
+
def create
|
19
|
+
@<%= singular_table_name %> = <%= orm_class.build(class_name, "#{singular_table_name}_params") %>
|
20
|
+
|
21
|
+
if @<%= orm_instance.save %>
|
22
|
+
render json: <%= class_name %>Serializer.new(<%= "@#{singular_table_name}" %>), status: :created, location: [:<%= prefix %>, <%= "@#{singular_table_name}" %>]
|
23
|
+
else
|
24
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def update
|
29
|
+
if @<%= orm_instance.update("#{singular_table_name}_params") %>
|
30
|
+
render json: <%= class_name %>Serializer.new(<%= "@#{singular_table_name}" %>)
|
31
|
+
else
|
32
|
+
render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def destroy
|
37
|
+
@<%= orm_instance.destroy %>
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def set_<%= singular_table_name %>
|
43
|
+
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
|
44
|
+
end
|
45
|
+
|
46
|
+
def <%= "#{singular_table_name}_params" %>
|
47
|
+
<%- if attributes_names.empty? -%>
|
48
|
+
params.fetch(:<%= singular_table_name %>, {})
|
49
|
+
<%- else -%>
|
50
|
+
params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
|
51
|
+
<%- end -%>
|
52
|
+
end
|
53
|
+
end
|
54
|
+
<% end -%>
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
# This spec was generated by rspec-rails when you ran the scaffold generator.
|
4
|
+
# It demonstrates how one might use RSpec to specify the controller code that
|
5
|
+
# was generated by Rails when you ran the scaffold generator.
|
6
|
+
#
|
7
|
+
# It assumes that the implementation code is generated by the rails scaffold
|
8
|
+
# generator. If you are using any extension libraries to generate different
|
9
|
+
# controller code, this generated spec may or may not pass.
|
10
|
+
#
|
11
|
+
# It only uses APIs available in rails and/or rspec-rails. There are a number
|
12
|
+
# of tools you can use to make these specs even more expressive, but we're
|
13
|
+
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
14
|
+
#
|
15
|
+
# Compared to earlier versions of this generator, there is very limited use of
|
16
|
+
# stubs and message expectations in this spec. Stubs are only used when there
|
17
|
+
# is no simpler way to get a handle on the object needed for the example.
|
18
|
+
# Message expectations are only used when there is no simpler way to specify
|
19
|
+
# that an instance is receiving a specific message.
|
20
|
+
#
|
21
|
+
# Also compared to earlier versions of this generator, there are no longer any
|
22
|
+
# expectations of assigns and templates rendered. These features have been
|
23
|
+
# removed from Rails core in Rails 5, but can be added back in via the
|
24
|
+
# `rails-controller-testing` gem.
|
25
|
+
|
26
|
+
<% module_namespacing do -%>
|
27
|
+
RSpec.describe <%= prefixed_controller_class_name %>Controller, type: :controller do
|
28
|
+
# This should return the minimal set of attributes required to create a valid
|
29
|
+
# <%= class_name %>. As you add validations to <%= class_name %>, be sure to
|
30
|
+
# adjust the attributes here as well.
|
31
|
+
let(:valid_attributes) {
|
32
|
+
skip("Add a hash of attributes valid for your model")
|
33
|
+
}
|
34
|
+
let(:invalid_attributes) {
|
35
|
+
skip("Add a hash of attributes invalid for your model")
|
36
|
+
}
|
37
|
+
# This should return the minimal set of values that should be in the session
|
38
|
+
# in order to pass any filters (e.g. authentication) defined in
|
39
|
+
# <%= controller_class_name %>Controller. Be sure to keep this updated too.
|
40
|
+
let(:valid_session) { {} }
|
41
|
+
|
42
|
+
<% unless options[:singleton] -%>
|
43
|
+
describe "GET #index" do
|
44
|
+
it "returns a success response" do
|
45
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
46
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
47
|
+
get :index, {}, valid_session
|
48
|
+
<% else -%>
|
49
|
+
get :index, params: {}, session: valid_session
|
50
|
+
<% end -%>
|
51
|
+
expect(response).to be_successful
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
<% end -%>
|
56
|
+
describe "GET #show" do
|
57
|
+
it "returns a success response" do
|
58
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
59
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
60
|
+
get :show, {:id => <%= file_name %>.to_param}, valid_session
|
61
|
+
<% else -%>
|
62
|
+
get :show, params: {id: <%= file_name %>.to_param}, session: valid_session
|
63
|
+
<% end -%>
|
64
|
+
expect(response).to be_successful
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "POST #create" do
|
69
|
+
context "with valid params" do
|
70
|
+
it "creates a new <%= class_name %>" do
|
71
|
+
expect {
|
72
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
73
|
+
post :create, {:<%= file_name %> => valid_attributes}, valid_session
|
74
|
+
<% else -%>
|
75
|
+
post :create, params: {<%= file_name %>: valid_attributes}, session: valid_session
|
76
|
+
<% end -%>
|
77
|
+
}.to change(<%= class_name %>, :count).by(1)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "renders a JSON response with the new <%= file_name %>" do
|
81
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
82
|
+
post :create, {:<%= file_name %> => valid_attributes}, valid_session
|
83
|
+
<% else %>
|
84
|
+
post :create, params: {<%= file_name %>: valid_attributes}, session: valid_session
|
85
|
+
<% end -%>
|
86
|
+
expect(response).to have_http_status(:created)
|
87
|
+
expect(response.content_type).to eq('application/json')
|
88
|
+
expect(response.location).to eq(<%= prefix %>_<%= file_name %>_url(<%= class_name %>.last))
|
89
|
+
end
|
90
|
+
end
|
91
|
+
context "with invalid params" do
|
92
|
+
it "renders a JSON response with errors for the new <%= file_name %>" do
|
93
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
94
|
+
post :create, {:<%= file_name %> => invalid_attributes}, valid_session
|
95
|
+
<% else %>
|
96
|
+
post :create, params: {<%= file_name %>: invalid_attributes}, session: valid_session
|
97
|
+
<% end -%>
|
98
|
+
expect(response).to have_http_status(:unprocessable_entity)
|
99
|
+
expect(response.content_type).to eq('application/json')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
describe "PUT #update" do
|
104
|
+
context "with valid params" do
|
105
|
+
let(:new_attributes) {
|
106
|
+
skip("Add a hash of attributes valid for your model")
|
107
|
+
}
|
108
|
+
|
109
|
+
it "updates the requested <%= file_name %>" do
|
110
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
111
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
112
|
+
put :update, {:id => <%= file_name %>.to_param, :<%= file_name %> => new_attributes}, valid_session
|
113
|
+
<% else -%>
|
114
|
+
put :update, params: {id: <%= file_name %>.to_param, <%= file_name %>: new_attributes}, session: valid_session
|
115
|
+
<% end -%>
|
116
|
+
<%= file_name %>.reload
|
117
|
+
skip("Add assertions for updated state")
|
118
|
+
end
|
119
|
+
it "renders a JSON response with the <%= file_name %>" do
|
120
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
121
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
122
|
+
put :update, {:id => <%= file_name %>.to_param, :<%= file_name %> => valid_attributes}, valid_session
|
123
|
+
<% else %>
|
124
|
+
put :update, params: {id: <%= file_name %>.to_param, <%= file_name %>: valid_attributes}, session: valid_session
|
125
|
+
<% end -%>
|
126
|
+
expect(response).to have_http_status(:ok)
|
127
|
+
expect(response.content_type).to eq('application/json')
|
128
|
+
end
|
129
|
+
end
|
130
|
+
context "with invalid params" do
|
131
|
+
it "renders a JSON response with errors for the <%= file_name %>" do
|
132
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
133
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
134
|
+
put :update, {:id => <%= file_name %>.to_param, :<%= file_name %> => invalid_attributes}, valid_session
|
135
|
+
<% else %>
|
136
|
+
put :update, params: {id: <%= file_name %>.to_param, <%= file_name %>: invalid_attributes}, session: valid_session
|
137
|
+
<% end -%>
|
138
|
+
expect(response).to have_http_status(:unprocessable_entity)
|
139
|
+
expect(response.content_type).to eq('application/json')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
describe "DELETE #destroy" do
|
145
|
+
it "destroys the requested <%= file_name %>" do
|
146
|
+
<%= file_name %> = <%= class_name %>.create! valid_attributes
|
147
|
+
expect {
|
148
|
+
<% if RUBY_VERSION < '1.9.3' -%>
|
149
|
+
delete :destroy, {:id => <%= file_name %>.to_param}, valid_session
|
150
|
+
<% else -%>
|
151
|
+
delete :destroy, params: {id: <%= file_name %>.to_param}, session: valid_session
|
152
|
+
<% end -%>
|
153
|
+
}.to change(<%= class_name %>, :count).by(-1)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
<% end -%>
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
<% module_namespacing do -%>
|
4
|
+
class <%= prefixed_controller_class_name %>ControllerTest < ActionDispatch::IntegrationTest
|
5
|
+
<%- if mountable_engine? -%>
|
6
|
+
include Engine.routes.url_helpers
|
7
|
+
|
8
|
+
<%- end -%>
|
9
|
+
setup do
|
10
|
+
@<%= singular_table_name %> = <%= fixture_name %>(:one)
|
11
|
+
end
|
12
|
+
|
13
|
+
test "should get index" do
|
14
|
+
get <%= [prefix, index_helper].join('_') %>_url, as: :json
|
15
|
+
assert_response :success
|
16
|
+
end
|
17
|
+
|
18
|
+
test "should create <%= singular_table_name %>" do
|
19
|
+
assert_difference('<%= class_name %>.count') do
|
20
|
+
post <%= [prefix, index_helper].join('_') %>_url, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json
|
21
|
+
end
|
22
|
+
|
23
|
+
assert_response 201
|
24
|
+
end
|
25
|
+
|
26
|
+
test "should show <%= singular_table_name %>" do
|
27
|
+
get <%= [prefix, show_helper].join('_') %>, as: :json
|
28
|
+
assert_response :success
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should update <%= singular_table_name %>" do
|
32
|
+
patch <%= [prefix, show_helper].join('_') %>, params: { <%= "#{singular_table_name}: { #{attributes_string} }" %> }, as: :json
|
33
|
+
assert_response 200
|
34
|
+
end
|
35
|
+
|
36
|
+
test "should destroy <%= singular_table_name %>" do
|
37
|
+
assert_difference('<%= class_name %>.count', -1) do
|
38
|
+
delete <%= [prefix, show_helper].join('_') %>, as: :json
|
39
|
+
end
|
40
|
+
|
41
|
+
assert_response 204
|
42
|
+
end
|
43
|
+
end
|
44
|
+
<% end -%>
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_scaffold
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stefan Atkinson
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- stefan.atkinson69@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".travis.yml"
|
64
|
+
- Gemfile
|
65
|
+
- Gemfile.lock
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- api_scaffold.gemspec
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/api_scaffold.rb
|
73
|
+
- lib/api_scaffold/version.rb
|
74
|
+
- lib/generators/api_scaffold/api_scaffold_generator.rb
|
75
|
+
- lib/generators/api_scaffold/generator_helpers.rb
|
76
|
+
- lib/generators/api_scaffold/templates/controllers/controller.rb
|
77
|
+
- lib/generators/api_scaffold/templates/controllers/serializer_controller.rb
|
78
|
+
- lib/generators/api_scaffold/templates/tests/rspec/controller_spec.rb
|
79
|
+
- lib/generators/api_scaffold/templates/tests/test_unit/controller_spec.rb
|
80
|
+
homepage: https://github.com/stefatkins/api_scaffold
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.7.6
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A gem for scaffolding rails api templates
|
104
|
+
test_files: []
|