grape-scaffold 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60dd30d57edc0adcb25d2e1d0f1d313d385f5a48
4
+ data.tar.gz: 7160dbd560b6805a5ef0da6de90c442949e3fc3a
5
+ SHA512:
6
+ metadata.gz: fffe1673c00d08142d2d7aa86b3c9a8c30ef831a4ba57798440b1cf3d13b6153b6661839df34ac332122962057e748f0b16bf4d8d9da7f624435fcb1e39d61f8
7
+ data.tar.gz: a6174842988dec728e4710d90e89ee8839055d659ba120f29d38a549cb60925b2b4ccd90d374e026749c44e3d0d9ce3f54bf10810f702c8e9eb37ac17cfa7043
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Creates a Grape controller.
3
+
4
+ Example:
5
+ rails generate grape:controller MODEL
6
+
7
+ This will create:
8
+ A controller with the model name.
@@ -0,0 +1,33 @@
1
+ module Grape
2
+ class ControllerGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ argument :model_name, type: :string
6
+
7
+ class_option :controller_path, desc: 'Set default controller path', type: :string, defaut: 'app/controllers/api/v1'
8
+ class_option :skip_controller_tests, :desc => 'Skip generated controller tests', :type => :boolean
9
+
10
+ def generate_controller
11
+ template 'controller.erb', "app/controllers/api/v1/#{model_name.pluralize.underscore}.rb"
12
+ end
13
+
14
+ def generate_controller_specs
15
+ unless options[:skip_controller_tests]
16
+ template 'controller_spec.erb', "spec/requests/#{model_name.singularize.underscore}_spec.rb"
17
+ end
18
+ end
19
+
20
+ protected
21
+ def attribute_name(attribute)
22
+ attribute.split(':').first
23
+ end
24
+
25
+ def attribute_is_id?(attribute)
26
+ attribute.split(':').first.last(3) == "_id"
27
+ end
28
+
29
+ def attribute_without_id(attribute)
30
+ attribute.split(':').first[0..-4]
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,69 @@
1
+ module API
2
+ module V1
3
+ class <%= model_name.camelize.pluralize %> < Grape::API
4
+
5
+ resource :<%= model_name.pluralize.underscore %> do
6
+ # Get a list of <%= model_name.pluralize.underscore %>
7
+ #
8
+ # Example Request:
9
+ # GET /v1/<%= model_name.pluralize.underscore %>
10
+ get do
11
+ <%= model_name.camelize.singularize %>.all
12
+ end
13
+
14
+ # Get a single <%= model_name.underscore.singularize %>
15
+ #
16
+ # Parameters:
17
+ # id (required) - The ID of a <%= model_name.underscore.singularize %>
18
+ # Example Request:
19
+ # GET /v1/<%= model_name.pluralize.underscore %>/:id
20
+ params do
21
+ requires :id
22
+ end
23
+ get ':id' do
24
+ <%= model_name.camelize.singularize %>.find(params[:id])
25
+ end
26
+
27
+ # Create a new <%= model_name.underscore.singularize %>
28
+ #
29
+ # Example Request:
30
+ # POST /v1/<%= model_name.pluralize.underscore %>
31
+ post do
32
+ <%= model_name.underscore.singularize %> = <%= model_name.camelize.singularize %>.new(params)
33
+ if <%= model_name.underscore.singularize %>.save
34
+ <%= model_name.underscore.singularize %>
35
+ else
36
+ <%= model_name.underscore.singularize %>.errors
37
+ end
38
+ end
39
+
40
+ # Delete a <%= model_name.underscore.singularize %>
41
+ #
42
+ # Example Request:
43
+ # DELETE /v1/<%= model_name.pluralize.underscore %>/:id
44
+ params do
45
+ requires :id
46
+ end
47
+ delete ':id' do
48
+ <%= model_name.camelize.singularize %>.destroy(params[:id])
49
+ end
50
+
51
+ # Edit a <%= model_name.underscore.singularize %>
52
+ #
53
+ # Example Request:
54
+ # PUT /v1/<%= model_name.pluralize.underscore %>/:id
55
+ params do
56
+ requires :id
57
+ end
58
+ put ':id' do
59
+ <%= model_name.underscore.singularize %> = <%= model_name.camelize.singularize %>.find(params[:id])
60
+ if <%= model_name.underscore.singularize %>.update(params)
61
+ <%= model_name.underscore.singularize %>
62
+ else
63
+ <%= model_name.underscore.singularize %>.errors
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe '<%= model_name.camelize.pluralize %>' do
4
+ describe 'GET /v1/<%= model_name.pluralize.underscore %>' do
5
+ describe 'should return <%= model_name.underscore.singularize %> list' do
6
+ before do
7
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
8
+ get '/v1/<%= model_name.pluralize.underscore %>'
9
+ end
10
+
11
+ it 'returns 200 success status' do
12
+ expect(response.response_code).to eq 200
13
+ end
14
+
15
+ it 'returns a <%= model_name.underscore.singularize %>' do
16
+ expect(json.length).to eq 1
17
+ end
18
+
19
+ it 'returns the same <%= model_name.underscore.singularize %>' do
20
+ pending
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'GET /v1/<%= model_name.pluralize.underscore %>/:id' do
26
+ describe 'should return <%= model_name.underscore.singularize %> details' do
27
+ before do
28
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
29
+ get "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
30
+ end
31
+
32
+ it 'returns 200 success status' do
33
+ expect(response.response_code).to eq 200
34
+ end
35
+
36
+ it 'returns a <%= model_name.underscore.singularize %>' do
37
+ pending
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'POST /v1/<%= model_name.pluralize.underscore %>' do
43
+ describe 'should save parameters' do
44
+ before do
45
+ post '/v1/<%= model_name.pluralize.underscore %>'
46
+ end
47
+
48
+ it 'returns 201 success status' do
49
+ expect(response.response_code).to eq 201
50
+ end
51
+
52
+ it 'saves the <%= model_name.underscore.singularize %>' do
53
+ expect(<%= model_name.camelize.singularize %>.all.count).to eq 1
54
+ end
55
+
56
+ it 'saves the <%= model_name.underscore.singularize %> with correct attributes' do
57
+ pending
58
+ end
59
+ end
60
+ end
61
+
62
+ describe 'PUT /v1/<%= model_name.pluralize.underscore %>/:id' do
63
+ describe 'should save parameters' do
64
+ before do
65
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
66
+ put "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
67
+ end
68
+
69
+ it 'returns 200 success status' do
70
+ expect(response.response_code).to eq 200
71
+ end
72
+
73
+ it 'updates the <%= model_name.underscore.singularize %>' do
74
+ pending
75
+ end
76
+ end
77
+ end
78
+
79
+ describe 'DELETE /v1/<%= model_name.pluralize.underscore %>/:id' do
80
+ describe 'should delete <%= model_name.underscore.singularize %>' do
81
+ before(:each) do
82
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
83
+ delete "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
84
+ end
85
+
86
+ it 'returns 200 success status' do
87
+ expect(response.response_code).to eq 200
88
+ end
89
+
90
+ it 'deletes the <%= model_name.underscore.singularize %>' do
91
+ expect(<%= model_name.camelize.singularize %>.all.count).to eq 0
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Scaffolds Grape API resource including model, controller, and specs.
3
+
4
+ Example:
5
+ rails generate grape:scaffold MODEL ATTRIBUTES
6
+
7
+ This will create:
8
+ The model, controller, and specs.
@@ -0,0 +1,45 @@
1
+ module Grape
2
+ class ScaffoldGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ argument :model_name, type: :string
6
+ argument :attributes, type: :array, default: []
7
+
8
+ class_option :controller_path, desc: 'Set default controller path', type: :string, defaut: 'app/controllers/api/v1'
9
+ class_option :skip_model_tests, :desc => 'Skip generated model tests', :type => :boolean
10
+ class_option :skip_controller_tests, :desc => 'Skip generated controller tests', :type => :boolean
11
+
12
+ def generate_model
13
+ generate 'model', "#{model_name} #{attributes.join(' ')}"
14
+ end
15
+
16
+ def generate_model_specs
17
+ unless options[:skip_model_tests]
18
+ template 'model_spec.erb', "spec/models/#{model_name.singularize.underscore}_spec.rb"
19
+ end
20
+ end
21
+
22
+ def generate_controller
23
+ template 'controller.erb', "#{options[:controller_path]}/#{model_name.pluralize.underscore}.rb"
24
+ end
25
+
26
+ def generate_controller_specs
27
+ unless options[:skip_controller_tests]
28
+ template 'controller_spec.erb', "spec/requests/#{model_name.singularize.underscore}_spec.rb"
29
+ end
30
+ end
31
+
32
+ protected
33
+ def attribute_name(attribute)
34
+ attribute.split(':').first
35
+ end
36
+
37
+ def attribute_is_id?(attribute)
38
+ attribute.split(':').first.last(3) == "_id"
39
+ end
40
+
41
+ def attribute_without_id(attribute)
42
+ attribute.split(':').first[0..-4]
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,69 @@
1
+ module API
2
+ module V1
3
+ class <%= model_name.camelize.pluralize %> < Grape::API
4
+
5
+ resource :<%= model_name.pluralize.underscore %> do
6
+ # Get a list of <%= model_name.pluralize.underscore %>
7
+ #
8
+ # Example Request:
9
+ # GET /v1/<%= model_name.pluralize.underscore %>
10
+ get do
11
+ <%= model_name.camelize.singularize %>.all
12
+ end
13
+
14
+ # Get a single <%= model_name.underscore.singularize %>
15
+ #
16
+ # Parameters:
17
+ # id (required) - The ID of a <%= model_name.underscore.singularize %>
18
+ # Example Request:
19
+ # GET /v1/<%= model_name.pluralize.underscore %>/:id
20
+ params do
21
+ requires :id
22
+ end
23
+ get ':id' do
24
+ <%= model_name.camelize.singularize %>.find(params[:id])
25
+ end
26
+
27
+ # Create a new <%= model_name.underscore.singularize %>
28
+ #
29
+ # Example Request:
30
+ # POST /v1/<%= model_name.pluralize.underscore %>
31
+ post do
32
+ <%= model_name.underscore.singularize %> = <%= model_name.camelize.singularize %>.new(params)
33
+ if <%= model_name.underscore.singularize %>.save
34
+ <%= model_name.underscore.singularize %>
35
+ else
36
+ <%= model_name.underscore.singularize %>.errors
37
+ end
38
+ end
39
+
40
+ # Delete a <%= model_name.underscore.singularize %>
41
+ #
42
+ # Example Request:
43
+ # DELETE /v1/<%= model_name.pluralize.underscore %>/:id
44
+ params do
45
+ requires :id
46
+ end
47
+ delete ':id' do
48
+ <%= model_name.camelize.singularize %>.destroy(params[:id])
49
+ end
50
+
51
+ # Edit a <%= model_name.underscore.singularize %>
52
+ #
53
+ # Example Request:
54
+ # PUT /v1/<%= model_name.pluralize.underscore %>/:id
55
+ params do
56
+ requires :id
57
+ end
58
+ put ':id' do
59
+ <%= model_name.underscore.singularize %> = <%= model_name.camelize.singularize %>.find(params[:id])
60
+ if <%= model_name.underscore.singularize %>.update(params)
61
+ <%= model_name.underscore.singularize %>
62
+ else
63
+ <%= model_name.underscore.singularize %>.errors
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,95 @@
1
+ require 'spec_helper'
2
+
3
+ describe '<%= model_name.camelize.pluralize %>' do
4
+ describe 'GET /v1/<%= model_name.pluralize.underscore %>' do
5
+ describe 'should return <%= model_name.underscore.singularize %> list' do
6
+ before do
7
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
8
+ get '/v1/<%= model_name.pluralize.underscore %>'
9
+ end
10
+
11
+ it 'returns 200 success status' do
12
+ expect(response.response_code).to eq 200
13
+ end
14
+
15
+ it 'returns a <%= model_name.underscore.singularize %>' do
16
+ expect(json.length).to eq 1
17
+ end
18
+
19
+ it 'returns the same <%= model_name.underscore.singularize %>' do
20
+ pending
21
+ end
22
+ end
23
+ end
24
+
25
+ describe 'GET /v1/<%= model_name.pluralize.underscore %>/:id' do
26
+ describe 'should return <%= model_name.underscore.singularize %> details' do
27
+ before do
28
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
29
+ get "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
30
+ end
31
+
32
+ it 'returns 200 success status' do
33
+ expect(response.response_code).to eq 200
34
+ end
35
+
36
+ it 'returns a <%= model_name.underscore.singularize %>' do
37
+ pending
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'POST /v1/<%= model_name.pluralize.underscore %>' do
43
+ describe 'should save parameters' do
44
+ before do
45
+ post '/v1/<%= model_name.pluralize.underscore %>'
46
+ end
47
+
48
+ it 'returns 201 success status' do
49
+ expect(response.response_code).to eq 201
50
+ end
51
+
52
+ it 'saves the <%= model_name.underscore.singularize %>' do
53
+ expect(<%= model_name.camelize.singularize %>.all.count).to eq 1
54
+ end
55
+
56
+ it 'saves the <%= model_name.underscore.singularize %> with correct attributes' do
57
+ pending
58
+ end
59
+ end
60
+ end
61
+
62
+ describe 'PUT /v1/<%= model_name.pluralize.underscore %>/:id' do
63
+ describe 'should save parameters' do
64
+ before do
65
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
66
+ put "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
67
+ end
68
+
69
+ it 'returns 200 success status' do
70
+ expect(response.response_code).to eq 200
71
+ end
72
+
73
+ it 'updates the <%= model_name.underscore.singularize %>' do
74
+ pending
75
+ end
76
+ end
77
+ end
78
+
79
+ describe 'DELETE /v1/<%= model_name.pluralize.underscore %>/:id' do
80
+ describe 'should delete <%= model_name.underscore.singularize %>' do
81
+ before(:each) do
82
+ @<%= model_name.underscore.singularize %> = create(:<%= model_name.underscore.singularize %>)
83
+ delete "/v1/<%= model_name.pluralize.underscore %>/#{@<%= model_name.underscore.singularize %>.id}"
84
+ end
85
+
86
+ it 'returns 200 success status' do
87
+ expect(response.response_code).to eq 200
88
+ end
89
+
90
+ it 'deletes the <%= model_name.underscore.singularize %>' do
91
+ expect(<%= model_name.camelize.singularize %>.all.count).to eq 0
92
+ end
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= model_name.camelize.singularize %> do
4
+ let(:<%= model_name.underscore.singularize %>) { create(:<%= model_name.underscore.singularize %>) }
5
+
6
+ context '#object' do
7
+ it 'should be valid' do
8
+ <%= model_name.underscore.singularize %>.should be_valid
9
+ end
10
+ end
11
+
12
+ context '#associations' do
13
+ <%- attributes.each do |attribute| -%>
14
+ <%- if attribute_is_id?(attribute) -%>
15
+ it { should belong_to(:<%= attribute_without_id(attribute) -%>) }
16
+ <%- end -%>
17
+ <%- end -%>
18
+ pending
19
+ end
20
+
21
+ context '#values' do
22
+ <%- attributes.each do |attribute| -%>
23
+ <%- unless attribute_is_id?(attribute) -%>
24
+ it { should respond_to(:<%= attribute_name(attribute) %>) }
25
+ <%- end -%>
26
+ <%- end -%>
27
+ end
28
+
29
+ context '#scopes' do
30
+ pending
31
+ end
32
+
33
+ context '#validations' do
34
+ pending
35
+ end
36
+
37
+ context '#methods' do
38
+ pending
39
+ end
40
+
41
+ context '#callbacks' do
42
+ pending
43
+ end
44
+ end
@@ -0,0 +1,4 @@
1
+ require "grape_scaffold/version"
2
+
3
+ module GrapeScaffold
4
+ end
@@ -0,0 +1,3 @@
1
+ module GrapeScaffold
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape-scaffold
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Shrivara K S
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 3.2.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 3.2.0
27
+ description: An API scaffold generator for Grape which generates controller, models
28
+ and specs
29
+ email:
30
+ - shrivara.ks@icicletech.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - lib/generators/grape/controller/USAGE
36
+ - lib/generators/grape/controller/controller_generator.rb
37
+ - lib/generators/grape/controller/templates/controller.erb
38
+ - lib/generators/grape/controller/templates/controller_spec.erb
39
+ - lib/generators/grape/scaffold/USAGE
40
+ - lib/generators/grape/scaffold/scaffold_generator.rb
41
+ - lib/generators/grape/scaffold/templates/controller.erb
42
+ - lib/generators/grape/scaffold/templates/controller_spec.erb
43
+ - lib/generators/grape/scaffold/templates/model_spec.erb
44
+ - lib/grape_scaffold.rb
45
+ - lib/grape_scaffold/version.rb
46
+ homepage: https://github.com/icicletech/grape_scaffold
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.2.2
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: An API scaffold generator for Grape
70
+ test_files: []