Module: DryCrudJsonapiSwagger::Helper

Included in:
ControllerSetup, NestedControllerSetup
Defined in:
app/domain/dry_crud_jsonapi_swagger/helper.rb

Instance Method Summary collapse

Instance Method Details

#controller_routeObject



24
25
26
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 24

def controller_route
  controller_class.model_class.new(id: 1)
end

#human_nameObject



16
17
18
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 16

def human_name
  model_name.human
end

#include_description(controller = controller_class) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 40

def include_description(controller = controller_class)
  <<~DESC
    Available primary relations:
    #{includes(controller.model_class).map { |inc| "* #{inc}" }.join("\n")}

    Separate values with a comma

    To include sub-relations, specify the relationship chain with the elements separated by '.'
    i.e. "employee.address.town"
  DESC
end

#includes(model_class) ⇒ Object



52
53
54
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 52

def includes(model_class)
  model_class.reflect_on_all_associations.reject(&:polymorphic?).map(&:name).sort
end

#model_nameObject



12
13
14
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 12

def model_name
  controller_class.model_class.model_name
end

#nested_controller_idObject



32
33
34
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 32

def nested_controller_id
  controller_class.model_class.model_name.route_key.singularize + '_id'
end

#nested_human_nameObject



20
21
22
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 20

def nested_human_name
  nested_model_name.human
end

#nested_model_nameObject



36
37
38
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 36

def nested_model_name
  nested_class.model_class.model_name
end

#nested_root_pathObject



28
29
30
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 28

def nested_root_path
  nested_model_name.route_key
end

#parameter_custom(swagger_doc, type) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 119

def parameter_custom(swagger_doc, type)
  controller_class.swagger_params[type].each do |param|
    swagger_doc.parameter do
      key :name,        param.name
      key :in,          :query
      key :description, param.description
      key :required,    param.required
      key :type,        param.type
      key :enum,        param.enum if param.enum.present?
    end
  end
end

#parameter_id(swagger_doc, helper) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 91

def parameter_id(swagger_doc, helper)
  swagger_doc.parameter do
    key :name, :id
    key :in, :path
    key :description, "ID of #{helper.human_name} to fetch"
    key :required, true
    key :type, :integer
  end
end

#parameter_include(swagger_doc, helper, type) ⇒ Object

rubocop:disable Metrics/MethodLength



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 101

def parameter_include(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
  desc = case type.to_sym
           when :index, :show then include_description
           when :nested       then include_description(helper.nested_class)
         end
  swagger_doc.parameter do
    key :name,             :include
    key :in,               :query
    key :description,      desc
    key :required,         false
    key :type,             :array
    key :collectionFormat, :csv
    items do
      key :type, :string
    end
  end
end

#parameters(swagger_doc, helper, type) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 80

def parameters(swagger_doc, helper, type)
  case type.to_sym
  when :index
    parameter_custom swagger_doc, type
  when :show, :nested
    parameter_id swagger_doc, helper
  end

  parameter_include swagger_doc, helper, type
end

#path_spec(swagger_doc, helper, type) ⇒ Object

rubocop:disable Metrics/MethodLength



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 56

def path_spec(swagger_doc, helper, type) # rubocop:disable Metrics/MethodLength
  summary =
    case type.to_sym
    when :index  then "All #{human_name.pluralize}"
    when :show   then "Single #{human_name}"
    when :nested then "All #{nested_human_name.pluralize} belonging to #{human_name}"
    end

  swagger_doc.operation :get do
    key :summary, summary
    helper.setup_tag(self)
    helper.parameters(self, helper, type)
    response 200 do
      key :description, summary + ' Response'
      helper.response_schema(self, helper, type)
    end
  end
end

#response_schema(swagger_doc, helper, type) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 132

def response_schema(swagger_doc, helper, type)
  ref = case type.to_sym
        when :index, :show then helper.model_name
        when :nested       then helper.nested_model_name
        end

  swagger_doc.schema do
    key :type, :array
    items do
      key :'$ref', ref
    end
  end
end

#setup_swagger_path(path, helper = self, &block) ⇒ Object



4
5
6
7
8
9
10
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 4

def setup_swagger_path(path, helper = self, &block)
  return unless path
  @path = path.gsub('1', '{id}')
  controller_class.send(:swagger_path, @path) do
    instance_exec(helper, &block)
  end
end

#setup_tag(swagger_doc) ⇒ Object



75
76
77
78
# File 'app/domain/dry_crud_jsonapi_swagger/helper.rb', line 75

def setup_tag(swagger_doc)
  tag = TagsSetup.new.path_tag(@path)
  swagger_doc.key :tags, [tag] if tag.present?
end