Module: JsonapiSwaggerHelpers::ResourceMixin

Defined in:
lib/jsonapi_swagger_helpers/resource_mixin.rb

Instance Method Summary collapse

Instance Method Details

#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
# 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]
  actions.select! { |a| only.include?(a) } unless only.empty?
  actions.reject! { |a| except.include?(a) } unless except.empty?

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

  ctx = self
  if [:create, :index].any? { |a| actions.include?(a) }
    swagger_path base_path do
      if actions.include?(:index) && controller.action_methods.include?('index')
        index_action = JsonapiSwaggerHelpers::IndexAction.new \
          self, controller, tags: tags, description: descriptions[:index]
        index_action.generate
      end

      if actions.include?(:create) && controller.action_methods.include?('create')
        create_action = JsonapiSwaggerHelpers::CreateAction.new \
          self, controller, tags: tags, description: descriptions[:create]
        create_action.generate
      end
    end
  end

  if [:show, :update, :destroy].any? { |a| actions.include?(a) }
    ctx = self
    swagger_path "#{base_path}/{id}" do
      if actions.include?(:show) && controller.action_methods.include?('show')
        show_action = JsonapiSwaggerHelpers::ShowAction.new \
          self, controller, tags: tags, description: descriptions[:show]
        show_action.generate
      end

      if actions.include?(:update) && controller.action_methods.include?('update')
        update_action = JsonapiSwaggerHelpers::UpdateAction.new \
          self, controller, tags: tags, description: descriptions[:update]
        update_action.generate
      end

      if actions.include?(:destroy) && controller.action_methods.include?('destroy')
        destroy_action = JsonapiSwaggerHelpers::DestroyAction.new \
          self, controller, tags: tags, description: descriptions[:destroy]
        destroy_action.generate
      end
    end
  end
end