hoc_utils 0.1.5 → 0.1.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d322af731840a53efc077f1b06c37855bf762f56
4
- data.tar.gz: 55871f41c59f6ee90890b02c23210e65971cfc6b
3
+ metadata.gz: eab9dc88e9cf7cca2f292f4bd2a885ef9ba884bb
4
+ data.tar.gz: 9233f7c085dee9d939ba096eec056711e75eca1d
5
5
  SHA512:
6
- metadata.gz: 905e4b59fc1e3f2e4aabaa4e992bd45f5f4e713f5334ba2a85ee1c48943e8861b2107184a639ae0d9aa78dba209873291bbb1bdbaf59bd2760e6a06f93cd9b69
7
- data.tar.gz: f04a1678f3f7c778154f98a7a2ed18078b444a17bf0dfc1ff7d420b7fb3d7ebdbf143b278732e97007c919f7a6135257353d18775404264a8408399485b78ad5
6
+ metadata.gz: fbc551bfd8e4a9bf8169d832c17fcbdd9d8c765f0820f1faa9899fd7a9bee4fe8ceafa5ce07e2c3b574d395d359fc51899cc3ed3e643236a65a969edd813b9a4
7
+ data.tar.gz: 1beacf1d5b84ba667dd4bda929cfffdb0813550a4aac663975983d1b2f1d20e70b18db3bddf03c0053aad6d91325fcb8b91cafdcdf0bf8a316f83dfbbfa32a05
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # HocUtils
2
-
3
2
  This is a collection of utilities for a HoC API application
3
+
4
4
  ## Installation
5
5
 
6
6
  Add this line to your application's Gemfile:
@@ -17,10 +17,11 @@ Or install it yourself as:
17
17
 
18
18
  $ gem install hoc_utils
19
19
 
20
+ ## TODO
21
+ * Routes for nested resources
22
+ * Generate client code
23
+ * DSL for specifying models and layout of admin pages
20
24
 
21
- ## Contributing
22
-
23
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hoc_utils.
24
25
 
25
26
  ## License
26
27
 
@@ -15,9 +15,24 @@ module HocUtils
15
15
  class_option :admin, type: :boolean, default: true, desc: "Generate administration interface"
16
16
  class_option :routes, type: :boolean, default: true, desc: "Generate routes"
17
17
  class_option :api_version, type: :string, default: "v1", desc: "Version of namespace"
18
-
18
+ class_option :nested_to, type: :string, default: nil, desc: "Should be nested in"
19
19
  source_root File.expand_path('templates', __dir__)
20
20
 
21
+ def is_nested?
22
+ !options.nested_to.nil?
23
+ end
24
+
25
+ def singular_parent_name
26
+ options.nested_to.try(:downcase)
27
+ end
28
+
29
+ def plural_parent_name
30
+ options.nested_to.try(:pluralize)
31
+ end
32
+
33
+ def parent_class_name
34
+ singular_parent_name.try(:classify)
35
+ end
21
36
 
22
37
  def api_version
23
38
  options.api_version.downcase
@@ -44,12 +59,21 @@ ACTS
44
59
  # Scaffolds the api controller
45
60
  def generate_api_controller
46
61
  say "Generates app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb", :bold
47
- template "api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
62
+ if is_nested?
63
+ template "nested_api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
64
+ else
65
+ template "api_controller.rb.tt", "app/controllers/api/#{api_version}/#{plural_table_name}_controller.rb"
66
+ end
48
67
  end
49
68
 
50
69
  # Generates routes in config/routes.rb. Will namespace resources to api/[api_version].
51
70
  def generate_routes
52
71
  return unless options.routes?
72
+
73
+ if is_nested?
74
+ say "Sorry you have to generate the route manually. because I don't know how to do it when the resource is nested!", :yellow, :bold
75
+ return
76
+ end
53
77
  say "Generates routes. You may want to merge api/#{api_version} namespaces in config/routes.rb", :bold
54
78
  generate "resource_route api/#{api_version.downcase}/#{plural_table_name}"
55
79
  end
@@ -80,6 +104,14 @@ ACTS
80
104
  updated_at: { type: "string"},
81
105
  }
82
106
  },
107
+ # AUTO GENERATED STUB TODO: update with correct fields
108
+ #{plural_table_name}: {
109
+ type: 'object',
110
+ properties: {
111
+ meta: { "$ref": "#/definitions/meta" },
112
+ #{plural_table_name}: { type: 'array', items: { "$ref": "#/definitions/#{singular_table_name}" },},
113
+ }
114
+ },
83
115
  }
84
116
  end
85
117
  end
@@ -108,6 +140,7 @@ ACTS
108
140
  say("* Run 'rails rswag:specs:swaggerize' to update swagger.", :green)
109
141
  say("* Make sure any referenced models are updated with eg. has_many :#{plural_table_name}", :green)
110
142
  say("* Customize the table and form definition in 'app/admin/#{plural_table_name}_admin.rb'", :green)
143
+ say("* Setup nested route") if is_nested?
111
144
  say("* #beAwesome", :green)
112
145
  end
113
146
 
@@ -4,8 +4,7 @@ class Api::<%= api_version.upcase %>::<%= plural_table_name.camelize %>Controlle
4
4
 
5
5
  # GET /api/<%= api_version %><%= route_url %>
6
6
  def index
7
- @<%= plural_table_name %> = <%= class_name %>.all
8
- render_result({ <%= plural_table_name %>: @<%= plural_table_name %>.as_api_response(:basic) })
7
+ render_collection(<%= class_name %>.all, :<%= plural_table_name %>)
9
8
  end
10
9
 
11
10
  # GET /api/<%= api_version %><%= route_url %>/1
@@ -0,0 +1,54 @@
1
+ class Api::<%= api_version.upcase %>::<%= plural_table_name.camelize %>Controller < Api::<%= api_version.upcase %>::ApiController
2
+ before_action :set_<%= singular_parent_name %>
3
+ before_action :set_<%= singular_table_name %>, only: [:show, :update, :destroy]
4
+
5
+ # GET /api/<%= api_version %><%= route_url %>
6
+ def index
7
+ render_collection(@<%= singular_parent_name %>.<%= plural_table_name %>.all, :<%= plural_table_name %>)
8
+ end
9
+
10
+ # GET /api/<%= api_version %><%= route_url %>/1
11
+ def show
12
+ render_result(@<%= singular_table_name %>.as_api_response(:basic))
13
+ end
14
+
15
+ # POST /api/<%= api_version %><%= route_url %>
16
+ def create
17
+ @<%= "#{singular_table_name} = #{singular_parent_name}.#{plural_table_name}.new(#{singular_table_name}_params)" %>
18
+
19
+ if @<%= singular_table_name %>
20
+ render_result(@<%= singular_table_name %>.as_api_response(:basic), :created)
21
+ else
22
+ render_error(@<%= singular_table_name %>.errors.full_messages.first, :unprocessable_entity)
23
+ end
24
+ end
25
+
26
+ # PATCH/PUT /api/<%= api_version %><%= route_url %>/1
27
+ def update
28
+ if @<%= singular_table_name %>.update(<%= singular_table_name %>_params)
29
+ render_result(@<%= singular_table_name %>.as_api_response(:basic))
30
+ else
31
+ render_error(@<%= singular_table_name %>.errors.full_messages.first, :unprocessable_entity)
32
+ end
33
+ end
34
+
35
+ # DELETE /api/<%= api_version %><%= route_url %>/1
36
+ def destroy
37
+ @<%= singular_table_name %>.destroy
38
+ end
39
+
40
+ private
41
+
42
+ # Use callbacks to share common setup or constraints between actions.
43
+ def set_<%= singular_parent_name %>
44
+ @<%= singular_parent_name %> = <%= parent_class_name %>.find(params[:id])
45
+ end
46
+ def set_<%= singular_table_name %>
47
+ @<%= singular_table_name %> = @<%= singular_parent_name %>.<%= plural_table_name %>.find(params[:id])
48
+ end
49
+
50
+ # Only allow a trusted parameter "white list" through.
51
+ def <%= "#{singular_table_name}_params" %>
52
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
53
+ end
54
+ end
@@ -1,13 +1,16 @@
1
- # frozen_string_literal: true
2
1
  require 'swagger_helper'
3
2
  describe 'House of Code API' do
4
- path '/api/<%= "#{api_version}/#{plural_table_name}" %>' do
3
+ path '/api/<%= "#{api_version}/"%><%= "#{plural_parent_name}/:#{singular_parent_name}_id/" if is_nested?%><%= plural_table_name %>' do
5
4
  get 'Get <%= plural_table_name %>' do
6
5
  tags '<%= plural_table_name.camelize %>'
7
6
  description 'Gets <%= plural_table_name %>'
8
7
  produces 'application/json'
9
8
  consumes 'application/json'
9
+
10
+ parameter name: :all, in: :query, type: :boolean
11
+ parameter name: :page, in: :query, type: :integer
10
12
  parameter name: :Authorization, in: :header, required: true, type: :string
13
+ <%= "parameter name: :#{singular_parent_name}_id, in: :path, required:true, type: integer" if is_nested? %>
11
14
  response '200', '<%= plural_table_name.camelize %>' do
12
15
  schema '$ref' => '#/definitions/<%= plural_table_name %>'
13
16
  run_test!
@@ -23,6 +26,7 @@ describe 'House of Code API' do
23
26
  description 'Creates <%= singular_table_name %> with given input'
24
27
  produces 'application/json'
25
28
  consumes 'application/json'
29
+ <%= "parameter name: :#{singular_parent_name}_id, in: :path, required:true, type: integer" if is_nested? %>
26
30
  parameter name: :<%= singular_table_name %>, in: :body, schema: {
27
31
  '$ref' => '#/definitions/<%= singular_table_name %>_input'
28
32
  }
@@ -37,14 +41,14 @@ describe 'House of Code API' do
37
41
  end
38
42
  end
39
43
  end
40
-
41
- path '/api/<%= "#{api_version}/#{plural_table_name}" %>/{id}' do
44
+ path '/api/<%= "#{api_version}/"%><%= "#{plural_parent_name}/:#{singular_parent_name}_id/" if is_nested?%><%= plural_table_name %>/{id}' do
42
45
  get 'Get <%= singular_table_name %>' do
43
46
  tags '<%= plural_table_name.camelize %>'
44
47
  description 'Gets <%= singular_table_name %> with :id'
45
48
  produces 'application/json'
46
49
  consumes 'application/json'
47
- parameter name: :id, in: :path, type: :integer
50
+ <%= "parameter name: :#{singular_parent_name}_id, in: :path, required:true, type: integer" if is_nested? %>
51
+ parameter name: :id, in: :path, type: :integer, required: true
48
52
  parameter name: :Authorization, in: :header, required: true, type: :string
49
53
  response '200', '<%= singular_table_name.camelize %>' do
50
54
  schema '$ref' => '#/definitions/<%= singular_table_name %>'
@@ -61,7 +65,8 @@ describe 'House of Code API' do
61
65
  description 'Updates <%= singular_table_name %> with :id'
62
66
  produces 'application/json'
63
67
  consumes 'application/json'
64
- parameter name: :id, in: :path, type: :integer
68
+ <%= "parameter name: :#{singular_parent_name}_id, in: :path, required:true, type: integer" if is_nested? %>
69
+ parameter name: :id, in: :path, type: :integer, required: true
65
70
  parameter name: :Authorization, in: :header, required: true, type: :string
66
71
  response '200', '<%= singular_table_name.camelize %>' do
67
72
  schema '$ref' => '#/definitions/<%= singular_table_name %>_input'
@@ -78,7 +83,9 @@ describe 'House of Code API' do
78
83
  description 'Deletes <%= singular_table_name %> with :id'
79
84
  produces 'application/json'
80
85
  consumes 'application/json'
86
+ <%= "parameter name: :#{singular_parent_name}_id, in: :path, required:true, type: integer" if is_nested? %>
81
87
  parameter name: :Authorization, in: :header, required: true, type: :string
88
+ parameter name: :id, in: :path, type: :integer, required: true
82
89
  response '200', '<%= singular_table_name.camelize %> deleted' do
83
90
  run_test!
84
91
  end
@@ -1,3 +1,3 @@
1
1
  module HocUtils
2
- VERSION = "0.1.5"
2
+ VERSION = "0.1.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoc_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gert Lavsen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-09-13 00:00:00.000000000 Z
11
+ date: 2018-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -84,6 +84,7 @@ files:
84
84
  - lib/generators/hoc_utils/api_scaffold/USAGE
85
85
  - lib/generators/hoc_utils/api_scaffold/api_scaffold_generator.rb
86
86
  - lib/generators/hoc_utils/api_scaffold/templates/api_controller.rb.tt
87
+ - lib/generators/hoc_utils/api_scaffold/templates/nested_api_controller.rb.tt
87
88
  - lib/generators/hoc_utils/api_scaffold/templates/spec.rb.tt
88
89
  - lib/hoc_utils.rb
89
90
  - lib/hoc_utils/version.rb