Module: JsonapiSwaggerHelpers::ResourceMixin

Defined in:
lib/jsonapi_swagger_helpers/resource_mixin.rb

Instance Method Summary collapse

Instance Method Details

#controller_for(path) ⇒ Object



69
70
71
72
73
# File 'lib/jsonapi_swagger_helpers/resource_mixin.rb', line 69

def controller_for(path)
  path = path.sub('{id}', '1')
  route = Rails.application.routes.recognize_path(path)
  "#{route[:controller]}_controller".classify.constantize
end

#jsonapi_resource(base_path, tags: [], descriptions: {}, only: [], except: []) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/jsonapi_swagger_helpers/resource_mixin.rb', line 4

def jsonapi_resource(base_path, tags: [], descriptions: {}, only: [], except: [])
  actions   = [:index, :show, :create, :update, :destroy]

  unless only.empty?
    actions.select! { |a| only.include?(a) }
  end

  unless except.empty?
    actions.reject! { |a| except.include?(a) }
  end

  prefix     = @swagger_root_node.data[:basePath]
  full_path  = [prefix, base_path].join('/').gsub('//', '/')
  controller = controller_for(full_path)

  if [:create, :index].any? { |a| actions.include?(a) }
    swagger_path base_path do
      if actions.include?(:index)
        operation :get do
          key :tags, tags
          key :description, descriptions[:index]
          jsonapi_index(controller)
        end
      end

      if actions.include?(:create)
        operation :post do
          key :tags, tags
          key :description, descriptions[:create]
          strong_resource(controller, :create)
        end
      end
    end
  end

  if [:show, :update, :destroy].any? { |a| actions.include?(a) }
    swagger_path "#{base_path}/{id}" do
      if actions.include?(:show)
        operation :get do
          key :tags, tags
          key :description, descriptions[:show]
          jsonapi_show(controller)
        end
      end

      if actions.include?(:update)
        operation :put do
          key :tags, tags
          key :description, descriptions[:update]
          id_in_url
          strong_resource(controller, :update)
        end
      end

      if actions.include?(:destroy)
        operation :delete do
          key :tags, tags
          key :description, descriptions[:destroy]
          id_in_url
        end
      end
    end
  end
end