Module: JsonapiSwaggerHelpers::Readable

Included in:
IndexAction, ShowAction
Defined in:
lib/jsonapi_swagger_helpers/readable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 5

def self.included(klass)
  klass.class_eval do
    attr_reader :node,
                :controller,
                :description,
                :tags,
                :singular
  end
end

Instance Method Details

#all_tagsObject



59
60
61
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 59

def all_tags
  tags + payload_tags
end

#default_descriptionObject



31
32
33
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 31

def default_description
  "#{action_name.capitalize} Action"
end

#each_associationObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 80

def each_association
  types = [jsonapi_type]
  resource_map = util.all_resources(resource, include_directive)
  resource_map.each_pair do |association_name, association_resource|
    resource_type = association_resource.config[:type]
    next if types.include?(resource_type)
    types << resource_type
    yield association_name, association_resource
  end
end

#each_statObject



71
72
73
74
75
76
77
78
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 71

def each_stat
  resource.config[:stats].each_pair do |stat_name, opts|
    calculations = opts.calculations.keys - [:keys]
    calculations = calculations.join(', ')

    yield stat_name, calculations
  end
end

#full_descriptionObject



55
56
57
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 55

def full_description
  "#{description}<br /><br />#{util.sideload_label(include_directive)}"
end

#generateObject



112
113
114
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 112

def generate
  raise 'override me'
end

#generate_response_schema!Object



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 99

def generate_response_schema!
  _self = self

  payloads = util.payloads_for(resource, include_directive.to_hash)
  JsonapiSwaggerHelpers.docs_controller.send(:swagger_schema, response_schema_id) do
    payloads.each do |p|
      property p.name do
        key :'$ref', p.name
      end
    end
  end
end

#has_extra_fields?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 51

def has_extra_fields?
  resource.config[:extra_fields].keys.length > 1
end

#has_sideloads?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 47

def has_sideloads?
  include_directive.keys.length > 0
end

#include_directiveObject



43
44
45
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 43

def include_directive
  util.include_directive_for(controller, action_name)
end

#initialize(node, controller, description: nil, tags: [], singular: false) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 15

def initialize(node, controller, description: nil, tags: [], singular: false)
  @node = node
  @controller = controller
  @description = description || default_description
  @tags = tags
  @singular = singular
end

#jsonapi_typeObject



91
92
93
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 91

def jsonapi_type
  resource.config[:type]
end

#operation_idObject



35
36
37
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 35

def operation_id
  "#{controller.name.gsub('::', '-')}-#{action_name}"
end

#payload_tagsObject



63
64
65
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 63

def payload_tags
  util.payload_tags_for(resource, include_directive.to_hash)
end

#resourceObject



23
24
25
26
27
28
29
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 23

def resource
  @resource = controller._jsonapi_compliable
  if @resource.is_a?(Hash)
    @resource = @resource[action_name]
  end
  @resource
end

#response_schema_idObject



95
96
97
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 95

def response_schema_id
  "#{operation_id}_#{action_name}_response"
end

#utilObject



39
40
41
# File 'lib/jsonapi_swagger_helpers/readable.rb', line 39

def util
  JsonapiSwaggerHelpers::Util
end