rails_api_documentation 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +9 -8
  3. data/app/assets/javascripts/table.js.coffee +10 -1
  4. data/app/assets/stylesheets/rails_api_doc/default.sass +39 -16
  5. data/app/assets/stylesheets/rails_api_doc/table.sass +21 -4
  6. data/app/controllers/rails_api_doc/api_docs_controller.rb +17 -20
  7. data/app/helpers/rails_api_doc/application_helper.rb +11 -0
  8. data/app/models/rails_api_doc/api_datum.rb +17 -0
  9. data/app/views/layouts/rails_api_doc/application.slim +2 -5
  10. data/app/views/rails_api_doc/api_docs/_request_api_table.slim +1 -1
  11. data/app/views/rails_api_doc/api_docs/_response_api_table.slim +4 -3
  12. data/app/views/rails_api_doc/api_docs/{index.slim → show.slim} +0 -0
  13. data/app/views/shared/_param_inputs.slim +2 -3
  14. data/app/views/shared/_table.slim +8 -6
  15. data/app/views/shared/_title.slim +8 -0
  16. data/config/routes.rb +1 -1
  17. data/lib/generators/rails_api_doc/templates/api_datum_migration.rb +4 -1
  18. data/lib/rails_api_doc.rb +16 -1
  19. data/lib/rails_api_doc/controller/headers.rb +59 -0
  20. data/lib/rails_api_doc/controller/param.rb +113 -0
  21. data/lib/rails_api_doc/controller/repo.rb +35 -0
  22. data/lib/rails_api_doc/controller/request/dsl.rb +1 -3
  23. data/lib/rails_api_doc/controller/request/factory.rb +29 -0
  24. data/lib/rails_api_doc/controller/request/headers.rb +11 -42
  25. data/lib/rails_api_doc/controller/request/param.rb +26 -33
  26. data/lib/rails_api_doc/controller/request/repository.rb +5 -26
  27. data/lib/rails_api_doc/controller/response/factory.rb +20 -2
  28. data/lib/rails_api_doc/controller/response/headers.rb +10 -21
  29. data/lib/rails_api_doc/controller/response/param.rb +38 -1
  30. data/lib/rails_api_doc/controller/response/rabl.rb +8 -19
  31. data/lib/rails_api_doc/controller/response/rabl_compiler.rb +7 -9
  32. data/lib/rails_api_doc/controller/response/repository.rb +38 -0
  33. data/lib/rails_api_doc/model/attribute_merger.rb +130 -0
  34. data/lib/rails_api_doc/model/attribute_parser.rb +23 -5
  35. data/lib/rails_api_doc/version.rb +1 -1
  36. metadata +10 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 32d21488a0b91b4da968d909a9988b67cc7ea427
4
- data.tar.gz: 72c1203f43307e203f499879d0739c0adf3fe237
3
+ metadata.gz: e94dad7cd2df856b0fa66bc552ba1b21719e2397
4
+ data.tar.gz: 00ee1ae3831826caea27f72422986ac45a49cb0a
5
5
  SHA512:
6
- metadata.gz: ad3a4d03dbb76aa738e0a7cc16fe832967d9db8b8b28fc14932398229ab6837bb3defb2d50fdea137a216b25d2ee62abe8f200ab7a9026ba2110ec67a6e32943
7
- data.tar.gz: fdc0c2b103f36ea0443c62399b0f027aa760fd4ec25dd6733708baecb7f3a75cbc236f4abc0804e07851a66db8ac4096b17e49eac46bcf6e88028b0ffd7ce261
6
+ metadata.gz: c80f87121f85a5f2757513b5612ac1baa3a2107e2d942b072450a0efa771118bab83cda7e768a13ab4be45ea84b7e4410aca0ada94ec489f91655c38c10a99c5
7
+ data.tar.gz: ac7a8147091a77af5f71f33ed8ba963db16ebfc69b4e153c3f5a70439827d4674304f4e2f0eac79201e0084f7384d815c95432c935599e81f9eddbafa0680be2
data/README.md CHANGED
@@ -44,15 +44,16 @@ To display api documentation on route '/api_doc' you need to:
44
44
  has_scope :article_id, :name
45
45
 
46
46
  # Define parameters with type and nested options
47
- parameter :age, type: Integer
48
- parameter :name, type: String, required: true
49
- parameter :articles, type: :model, model: Article do
50
- parameter :title, type: String
51
- parameter :body, type: String, required: true
47
+ # Article and Datum are usual ActiveRecord models
48
+ parameter :age, type: :integer
49
+ parameter :name, type: :string, required: true
50
+ parameter :articles_attributes, type: :ary_object, model: 'Article' do
51
+ parameter :title, type: :string
52
+ parameter :body, type: :string, required: true
52
53
  parameter :rating, type: :enum, enum: [1, 2, 3]
53
- parameter :data, type: :model, model: Datum do
54
- parameter :creation_date, type: DateTime
55
- parameter :comment, type: String
54
+ parameter :data_attributes, type: :object, model: 'Datum' do
55
+ parameter :creation_date, type: :datetime
56
+ parameter :comment, type: :string
56
57
  end
57
58
  end
58
59
  parameter :test, type: String, required: true
@@ -17,12 +17,21 @@ $ ->
17
17
  $('.flex-table input, .flex-table select').on 'click', (e) ->
18
18
  e.stopPropagation()
19
19
 
20
- $('.ico').on 'click', (e) ->
20
+ $('span.ico').on 'click', (e) ->
21
21
  self = $(this)
22
22
  e.preventDefault()
23
23
  e.stopPropagation()
24
24
  self.closest('.row').toggleClass('is-active')
25
25
 
26
+ $('.destroy').on 'click', (e) ->
27
+ self = $(this)
28
+
29
+ self.closest('.flex-line').toggleClass('destroyed')
30
+
31
+ $('form.flex-line').on 'submit', () ->
32
+ $(this).addClass('updated')
33
+ $(this).removeClass('is-active')
34
+
26
35
  # _header_height = $('.aside').offset().top
27
36
 
28
37
  _window = $(window)
@@ -2,30 +2,53 @@
2
2
  @import url(http://fonts.googleapis.com/css?family=Open+Sans:300,400,700)
3
3
 
4
4
  body
5
- font-family: 'Open Sans', sans-serif
6
- font-weight: 300
7
- line-height: 1.42em
8
- color: #A7A1AE
9
- background-color: #1F2739
5
+
6
+ font-family: 'Open Sans', sans-serif
7
+ font-weight: 300
8
+ line-height: 1.42em
9
+ color: #A7A1AE
10
+ background-color: #1F2739
10
11
 
11
12
  h1.header
12
- font-size: 3em
13
- font-weight: 300
14
- line-height: 1em
15
- text-align: center
13
+
14
+ font-size: 3em
15
+ font-weight: 300
16
+ line-height: 1em
17
+ text-align: center
18
+ color: #4DC3FA
19
+
20
+ .request-action-title
21
+
22
+ font-weight: 300
23
+ line-height: 1em
24
+ text-align: center
25
+ color: #4DC3FA
26
+
27
+ span
28
+ font-size: 0.85em
29
+
30
+ .blue
31
+
16
32
  color: #4DC3FA
17
33
 
34
+
35
+
18
36
  .blue
19
- color: #185875
37
+
38
+ color: #185875
20
39
 
21
40
  .yellow
22
- color: #FFF842
41
+
42
+ color: #FFF842
23
43
 
24
44
  .content
25
- display: flex
26
45
 
27
- nav
28
- flex: 1
46
+ display: flex
47
+
48
+ nav
49
+
50
+ flex: 1
51
+
52
+ .tables
29
53
 
30
- .tables
31
- flex: 5
54
+ flex: 5
@@ -52,10 +52,18 @@
52
52
  padding: 1% 2%
53
53
  position: absolute
54
54
  left: 2%
55
+ border-radius: 100%
56
+
57
+ &.destroy
58
+ left: 10%
59
+
60
+ color: #FB667A
61
+
62
+ text-decoration: none
55
63
 
56
64
  &:hover
57
- background-color: #FFF842
58
- color: #403E10
65
+ background-color: black//#FFF842
66
+ color: white
59
67
 
60
68
  transition-delay: 0s
61
69
  transition-duration: 0.4s
@@ -74,6 +82,15 @@
74
82
 
75
83
  background: lightgreen
76
84
 
85
+ &.created
86
+ border: 2px solid #1E90FF
87
+
88
+ &.destroyed
89
+ border: 2px solid #DC143C
90
+
91
+ &.updated
92
+ border: 2px solid #00FF00
93
+
77
94
  &.row
78
95
  display: flex
79
96
 
@@ -128,8 +145,8 @@
128
145
  div.flex-item
129
146
 
130
147
  &:hover
131
- background-color: #FFF842
132
- color: #403E10
148
+ background-color: #4DC3FA //#FFF842
149
+ color: white
133
150
 
134
151
  transition-delay: 0s
135
152
  transition-duration: 0.4s
@@ -3,50 +3,47 @@
3
3
  # :nodoc:
4
4
  class RailsApiDoc::ApiDocsController < RailsApiDoc::ApplicationController
5
5
 
6
- class NewRecord < ActiveRecord::Base
7
- end
8
-
9
- def index
6
+ def show
10
7
  # preload controllers for parameters to apply
11
8
  Dir.glob("#{Rails.root}/app/controllers/**/*.rb").each { |file| require_dependency file }
12
9
 
13
- @request_repository = RailsApiDoc::Controller::Request::Repository
10
+ @registered_controllers = RailsApiDoc::Controller::Request::Factory.registered_controllers
14
11
 
15
- @registered_controllers = @request_repository.registered_controllers
12
+ @request_repository = RailsApiDoc::Controller::Request::Factory.repo
16
13
 
17
14
  @response_repository = RailsApiDoc::Controller::Response::Factory.repo
18
15
  end
19
16
 
20
17
  def create
21
- attributes = RailsApiDoc::Controller::AttributeParser.parse_attributes(permitted_params)
18
+ attributes = RailsApiDoc::Model::AttributeParser.parse_attributes(params)
22
19
 
23
- RailsApiDoc::ApiDatum.create!(attributes)
20
+ @res = RailsApiDoc::ApiDatum.create!(attributes)
21
+
22
+ redirect_to api_doc_path
24
23
  end
25
24
 
26
25
  def destroy
27
- attributes = RailsApiDoc::Controller::AttributeParser.parse_attributes(permitted_params)
28
-
29
26
  if params[:id]
30
- RailsApiDoc::ApiDatum.find(params[:id]).update!(attributes)
27
+ @res = RailsApiDoc::ApiDatum.find(params[:id]).destroy!
31
28
  else
32
- RailsApiDoc::ApiDatum.create!(attributes)
29
+ attributes = RailsApiDoc::Model::AttributeParser.parse_attributes(params)
30
+
31
+ @res = RailsApiDoc::ApiDatum.create!(attributes)
33
32
  end
33
+
34
+ redirect_to api_doc_path
34
35
  end
35
36
 
36
37
  def update
37
- attributes = RailsApiDoc::Controller::AttributeParser.parse_attributes(permitted_params)
38
+ attributes = RailsApiDoc::Model::AttributeParser.parse_attributes(params)
38
39
 
39
40
  if params[:id]
40
- RailsApiDoc::ApiDatum.find(params[:id]).update!(attributes)
41
+ @res = RailsApiDoc::ApiDatum.find(params[:id]).update!(attributes)
41
42
  else
42
- RailsApiDoc::ApiDatum.create!(attributes)
43
+ @res = RailsApiDoc::ApiDatum.create!(attributes)
43
44
  end
44
- end
45
-
46
- private
47
45
 
48
- def permitted_params
49
- params.permit(:name, :type, :special, :desc)
46
+ redirect_to api_doc_path
50
47
  end
51
48
 
52
49
  end
@@ -1,4 +1,15 @@
1
1
  module RailsApiDoc
2
2
  module ApplicationHelper
3
+
4
+ def construct_destroy_param(row_values, init_params, nesting)
5
+ [
6
+ init_params,
7
+ row_values.store,
8
+ nesting: nesting,
9
+ id: row_values.param&.id,
10
+ name: row_values.name
11
+ ].reduce(&:merge)
12
+ end
13
+
3
14
  end
4
15
  end
@@ -3,6 +3,23 @@
3
3
  # :nodoc:
4
4
  class RailsApiDoc::ApiDatum < ActiveRecord::Base
5
5
 
6
+ #
7
+ # integer :api_type - request | response
8
+ #
9
+ # string :api_action - create | show | edit | ...
10
+ # string :action_type - destroy | update | create
11
+ # string :type - param data type
12
+ # string :name - param name
13
+ # string :special - special param data
14
+ # string :desc - description
15
+ #
16
+ # text :nesting - model nesting to display
17
+ #
18
+
19
+ self.inheritance_column = ''
20
+
6
21
  serialize :nesting, Array
7
22
 
23
+ enum api_type: [:request, :response]
24
+
8
25
  end
@@ -12,10 +12,7 @@ html lang="en"
12
12
  body.wrap
13
13
 
14
14
  h1.header
15
- span.blue &lt;
16
- span Rails
17
- span.blue &gt;
18
- span
19
- a.yellow[style="text-decoration: none" href="http://github.com/vshaveyko/rails_api_doc"] Api Documentation
15
+
16
+ = render 'shared/title', locals: { link: "http://github.com/vshaveyko/rails_api_doc", blue_title: "Rails", yellow_title: "Api Documentation" }
20
17
 
21
18
  == yield
@@ -2,4 +2,4 @@
2
2
 
3
3
  div[id="#{ctrl}.request" style="--row-count: #{request_headers.length}"]
4
4
 
5
- = render 'shared/table', model: ctrl, rows: repo[ctrl], headers: request_headers, nesting: [], type: :request
5
+ = render 'shared/table', model: ctrl, rows: repo[ctrl], headers: request_headers, nesting: [], params: { api_type: :request }
@@ -2,14 +2,15 @@
2
2
 
3
3
  div.response-table[id="#{ctrl}.response" style="--row-count: #{response_headers.length}"]
4
4
 
5
- - repo.map[ctrl][:actions].each do |action|
5
+ - repo[ctrl][:actions].each do |action|
6
6
  // TODO: Nest table in this div for easier jq toggling
7
7
  // add borders to this div
8
8
 
9
9
  - if route = repo.action_route(ctrl, action)
10
10
 
11
- div.request-action-title = route
11
+ div.request-action-title
12
+ = render 'shared/title', locals: { blue_title: route[:method], yellow_title: route[:url]}
12
13
 
13
14
  - if rows = repo.load_template(ctrl, action)
14
15
 
15
- = render 'shared/table', model: ctrl, rows: rows, headers: response_headers, nesting: [], type: :response
16
+ = render 'shared/table', model: ctrl, rows: rows, headers: response_headers, nesting: [], params: { api_action: action, api_type: :response }
@@ -1,8 +1,7 @@
1
- - value = try(:value)
2
1
  - placeholder = settings[:placeholder] || ''
3
2
 
4
3
  - case settings[:fill_type]
5
4
  - when :input
6
- = text_field_tag settings[:param], nil, placeholder: placeholder
5
+ = text_field_tag settings[:param], val, placeholder: placeholder
7
6
  - when :select
8
- = select_tag settings[:param], options_for_select(settings[:values], value)
7
+ = select_tag settings[:param], options_for_select(settings[:values], val), include_blank: true
@@ -3,22 +3,22 @@
3
3
  div.flex-table
4
4
 
5
5
  / header row
6
- = form_tag(api_doc_url(nesting: nesting, api_type: type), method: :post, remote: true, class: 'flex-line row') do
6
+ = form_tag(api_doc_url(params.merge(nesting: nesting)), class: 'flex-line row') do
7
7
 
8
8
  - headers.each.with_index do |(header, settings), index|
9
9
  h1.flex-item
10
10
  - if index.zero?
11
- span.add.ico +
11
+ span.ico +
12
12
  span.exit.ico ❌
13
13
  = submit_tag '✓'
14
14
 
15
- = render 'shared/param_inputs', settings: settings
15
+ = render 'shared/param_inputs', settings: settings, val: ''
16
16
 
17
17
  span.title = header
18
18
 
19
19
  - rows.each do |row_name, row_values|
20
20
 
21
- = form_tag(api_doc_url(nesting: nesting, api_type: type), method: :put, remote: true, class: 'flex-line row') do
21
+ = form_tag(api_doc_url(params.merge(nesting: nesting, id: row_values.param&.id)), method: :put, class: "flex-line row #{'destroyed' if row_values.destroyed?} #{'updated' if row_values.updated?} #{'created' if row_values.created?}") do
22
22
 
23
23
  - headers.each_value.with_index do |(header_settings), index|
24
24
 
@@ -28,13 +28,15 @@ div.flex-table
28
28
 
29
29
  - if index.zero?
30
30
  span.ico ✏
31
+ / = submit_tag ,name: 'destroy', class: 'ico destroy'
32
+ = link_to '❌' , api_doc_url(construct_destroy_param(row_values, params, nesting)), method: :delete, class: 'destroy ico'
31
33
  span.exit.ico ❌
32
34
  = submit_tag '✔'
33
35
 
34
- = render 'shared/param_inputs', settings: header_settings, value: param_value
36
+ = render 'shared/param_inputs', settings: header_settings, val: param_value
35
37
 
36
38
  span.title = param_value
37
39
 
38
40
  - if row_values.nested?
39
41
 
40
- = render 'shared/table', nesting: nesting, model: row_values[:model] || row_name, rows: row_values.nested, headers: headers, type: type
42
+ = render 'shared/table', nesting: nesting, model: row_values[:model] || row_name, rows: row_values.nested, headers: headers, params: params
@@ -0,0 +1,8 @@
1
+ span.blue &lt;
2
+ span = locals[:blue_title]
3
+ span.blue &gt;&nbsp;
4
+
5
+ - if locals[:link]
6
+ a.yellow[style="text-decoration: none" href="#{locals[:link]}"] = locals[:yellow_title]
7
+ - else
8
+ span.yellow = locals[:yellow_title]
data/config/routes.rb CHANGED
@@ -6,5 +6,5 @@
6
6
  RailsApiDoc::Engine.routes.draw do
7
7
  resource :api_doc
8
8
 
9
- root to: 'api_docs#index'
9
+ root to: 'api_docs#show'
10
10
  end
@@ -2,7 +2,10 @@ class GenerateRailsApiDocumentationAttributesTable < ActiveRecord::Migration[5.0
2
2
 
3
3
  def change
4
4
  create_table :rails_api_doc_api_data do |t|
5
- t.string :api_type
5
+ t.integer :api_type
6
+
7
+ t.string :action_type
8
+ t.string :api_action
6
9
  t.string :type
7
10
  t.string :name
8
11
  t.string :special
data/lib/rails_api_doc.rb CHANGED
@@ -1,21 +1,33 @@
1
1
  # author: Vadim Shaveiko <@vshaveyko>
2
2
  # frozen_string_literal: true
3
-
4
3
  module RailsApiDoc
4
+ NESTED_TYPES = [:ary_object, :object, :model].freeze
5
+
6
+ STRAIGHT_TYPES = [:bool, :string, :integer, :array, :datetime, :enum].freeze
7
+
8
+ ACCEPTED_TYPES = (NESTED_TYPES + STRAIGHT_TYPES).freeze
5
9
 
6
10
  module Controller
11
+ _dir = 'rails_api_doc/controller/'
12
+
13
+ autoload :Repo, _dir + 'repo'
14
+ autoload :Param, _dir + 'param'
15
+ autoload :Headers, _dir + 'headers'
16
+
7
17
  module Response
8
18
  _dir = 'rails_api_doc/controller/response/'
9
19
 
10
20
  autoload :Factory, _dir + 'factory'
11
21
  autoload :Headers, _dir + 'headers'
12
22
  autoload :Param, _dir + 'param'
23
+ autoload :Repository, _dir + 'repository'
13
24
  end
14
25
 
15
26
  module Request
16
27
  _dir = 'rails_api_doc/controller/request/'
17
28
 
18
29
  autoload :DSL, _dir + 'dsl'
30
+ autoload :Factory, _dir + 'factory'
19
31
  autoload :Param, _dir + 'param'
20
32
  autoload :Headers, _dir + 'headers'
21
33
  autoload :Repository, _dir + 'repository'
@@ -32,17 +44,20 @@ module RailsApiDoc
32
44
  module Model
33
45
  _dir = 'rails_api_doc/model/'
34
46
 
47
+ autoload :AttributeMerger, _dir + 'attribute_merger'
35
48
  autoload :AttributeParser, _dir + 'attribute_parser'
36
49
  end
37
50
 
38
51
  require 'rails_api_doc/config'
39
52
  class Config
53
+
40
54
  _dir = 'rails_api_doc/config/'
41
55
 
42
56
  autoload :Validator, _dir + 'validator'
43
57
  autoload :ValidateEnum, _dir + 'validate_enum'
44
58
  autoload :ValidateType, _dir + 'validate_type'
45
59
  autoload :ValidateAryObject, _dir + 'validate_ary_object'
60
+
46
61
  end
47
62
 
48
63
  module Exception