Module: JsonapiSwaggerHelpers::Writeable

Included in:
CreateAction, DestroyAction, UpdateAction
Defined in:
lib/jsonapi_swagger_helpers/writeable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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

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

Instance Method Details

#action_nameObject



29
30
31
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 29

def action_name
  raise 'override me'
end

#all_tagsObject



41
42
43
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 41

def all_tags
  tags + payload_tags
end

#contextObject



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

def context
  JsonapiSwaggerHelpers.docs_controller
end

#default_descriptionObject



33
34
35
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 33

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

#define_schemaObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 93

def define_schema
  generate_request_schema!

  _self = self
  context.send(:swagger_schema, :"#{strong_resource.name}_#{action_name}") do
    _self.strong_resource.attributes.each_pair do |attribute, config|
      property attribute do
        key :type, config[:type]
      end
    end
  end

  _self.each_strong_relation(_self.strong_resource) do |relation_name, relation_config|
    context.send(:swagger_schema, :"#{strong_resource.name}_#{relation_name}_#{action_name}") do
      relation_config[:resource].attributes.each_pair do |attribute, config|
        property attribute do
          key :type, config[:type]
        end
      end
    end
  end
end

#each_strong_relation(strong_resource) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 83

def each_strong_relation(strong_resource)
  strong_resource.relations.each_pair do |relation_name, relation_config|
    yield relation_name, relation_config

    each_strong_relation(relation_config[:resource]) do |sub_relation_name, sub_relation_config|
      yield sub_relation_name, sub_relation_config
    end
  end
end

#generateObject



116
117
118
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 116

def generate
  raise 'override me'
end

#generate_request_schema!Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 67

def generate_request_schema!
  _self = self

  JsonapiSwaggerHelpers.docs_controller.send(:swagger_schema, request_schema_id) do
    property _self.strong_resource.name do
      key :'$ref', :"#{_self.strong_resource.name}_#{_self.action_name}"
    end

    _self.each_strong_relation(_self.strong_resource) do |relation_name, _relation_config|
      property relation_name do
        key :'$ref', :"#{_self.strong_resource.name}_#{relation_name}_#{_self.action_name}"
      end
    end
  end
end

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



16
17
18
19
20
21
22
23
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 16

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

#operation_idObject



37
38
39
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 37

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

#payload_tagsObject



45
46
47
48
49
50
51
52
53
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 45

def payload_tags
  tags = [:"payload-#{strong_resource.name}_#{action_name}"]

  strong_resource.relations.each_pair do |relation_name, _relation_config|
    tags << :"payload-#{strong_resource.name}_#{relation_name}_#{action_name}"
  end

  tags
end

#request_schema_idObject



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

def request_schema_id
  "#{operation_id}_#{action_name}_request"
end

#strong_resourceObject



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

def strong_resource
  controller._strong_resources[action_name]
end

#utilObject



25
26
27
# File 'lib/jsonapi_swagger_helpers/writeable.rb', line 25

def util
  JsonapiSwaggerHelpers::Util
end