Module: RESTFramework::Mixins::BaseControllerMixin::ClassMethods
- Defined in:
- lib/rest_framework/mixins/base_controller_mixin.rb
Instance Method Summary collapse
-
#get_title ⇒ Object
By default, this is the name of the controller class, titleized and with any custom inflection acronyms applied.
-
#label_for(s) ⇒ Object
Get a label from a field/column name, titleized and inflected.
- #openapi_document(request, route_group_name, routes) ⇒ Object
- #openapi_paths(routes, tag) ⇒ Object
- #openapi_request_content_types ⇒ Object
-
#openapi_response_content_types ⇒ Object
:nocov:.
-
#rrf_finalize ⇒ Object
Define any behavior to execute at the end of controller definition.
Instance Method Details
#get_title ⇒ Object
By default, this is the name of the controller class, titleized and with any custom inflection acronyms applied.
47 48 49 50 51 52 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 47 def get_title self.title || RESTFramework::Utils.inflect( self.name.demodulize.chomp("Controller").titleize(keep_id_suffix: true), self.inflect_acronyms, ) end |
#label_for(s) ⇒ Object
Get a label from a field/column name, titleized and inflected.
55 56 57 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 55 def label_for(s) RESTFramework::Utils.inflect(s.to_s.titleize(keep_id_suffix: true), self.inflect_acronyms) end |
#openapi_document(request, route_group_name, routes) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 139 def openapi_document(request, route_group_name, routes) server = request.base_url + request.original_fullpath.gsub(/\?.*/, "") { openapi: "3.1.1", info: { title: self.get_title, description: self.description, version: self.version.to_s, }.compact, servers: [ { url: server } ], paths: self.openapi_paths(routes, route_group_name), tags: [ { name: route_group_name, description: self.description }.compact ], components: { schemas: { "Error" => { type: "object", required: [ "message" ], properties: { message: { type: "string" }, errors: { type: "object" }, exception: { type: "string" }, }, }, }, responses: { "BadRequest": { description: "Bad Request", content: self.openapi_response_content_types.map { |ct| [ ct, ct == "text/html" ? {} : { schema: { "$ref" => "#/components/schemas/Error" } }, ] }.to_h, }, "NotFound": { description: "Not Found", content: self.openapi_response_content_types.map { |ct| [ ct, ct == "text/html" ? {} : { schema: { "$ref" => "#/components/schemas/Error" } }, ] }.to_h, }, }, }, }.compact end |
#openapi_paths(routes, tag) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 87 def openapi_paths(routes, tag) resp_cts = self.openapi_response_content_types req_cts = self.openapi_request_content_types routes.group_by { |r| r[:concat_path] }.map { |concat_path, routes| [ concat_path.gsub(/:([0-9A-Za-z_-]+)/, "{\\1}"), routes.map { |route| = RESTFramework::ROUTE_METADATA[route[:path]] || {} summary = .delete(:label).presence || self.label_for(route[:action]) description = .delete(:description).presence extra_action = RESTFramework::EXTRA_ACTION_ROUTES.include?(route[:path]) error_response = { "$ref" => "#/components/responses/BadRequest" } not_found_response = { "$ref" => "#/components/responses/NotFound" } spec = { tags: [ tag ], summary: summary, description: description }.compact # All routes should have a successful response. spec[:responses] = { 200 => { content: resp_cts.map { |ct| [ ct, {} ] }.to_h, description: "Success" }, } # Builtin POST, PUT, PATCH, and DELETE should have a 400 and 404 response. if route[:verb].in?([ "POST", "PUT", "PATCH", "DELETE" ]) && !extra_action spec[:responses][400] = error_response spec[:responses][404] = not_found_response end # All POST, PUT, PATCH should have a request body. if route[:verb].in?([ "POST", "PUT", "PATCH" ]) spec[:requestBody] ||= { content: req_cts.map { |ct| [ ct, {} ] }.to_h } end # Add remaining metadata as an extension. spec["x-rrf-metadata"] = if .present? next route[:verb].downcase, spec }.to_h.merge( { parameters: routes.first[:route].required_parts.map { |p| { name: p, in: "path", required: true, schema: { type: "integer" }, } }, }, ), ] }.to_h end |
#openapi_request_content_types ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 79 def openapi_request_content_types @openapi_request_content_types ||= [ "application/json", "application/x-www-form-urlencoded", "multipart/form-data", ] end |
#openapi_response_content_types ⇒ Object
:nocov:
71 72 73 74 75 76 77 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 71 def openapi_response_content_types @openapi_response_content_types ||= [ "text/html", self.serialize_to_json ? "application/json" : nil, self.serialize_to_xml ? "application/xml" : nil, ].compact end |
#rrf_finalize ⇒ Object
Define any behavior to execute at the end of controller definition. :nocov:
61 62 63 64 65 66 67 68 |
# File 'lib/rest_framework/mixins/base_controller_mixin.rb', line 61 def rrf_finalize if RESTFramework.config.freeze_config self::RRF_BASE_CONFIG.keys.each { |k| v = self.send(k) v.freeze if v.is_a?(Hash) || v.is_a?(Array) } end end |