Module: JsonapiSwaggerHelpers::StrongResourceMixin
- Defined in:
- lib/jsonapi_swagger_helpers/strong_resource_mixin.rb
Defined Under Namespace
Classes: Schemas
Instance Method Summary collapse
- #jsonapi_input_schema(schema_name, id: false, jsonapi_type:, attributes: nil, relationships: nil) ⇒ Object
- #jsonapi_payload(schema_name, payload) ⇒ Object
- #strong_resource(controller, action) ⇒ Object
- #strong_resource_attributes(resource, is_update = false) ⇒ Object
- #strong_resource_relationships(resource, is_update = false) ⇒ Object
Instance Method Details
#jsonapi_input_schema(schema_name, id: false, jsonapi_type:, attributes: nil, relationships: nil) ⇒ 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 |
# File 'lib/jsonapi_swagger_helpers/strong_resource_mixin.rb', line 87 def jsonapi_input_schema(schema_name, id: false, jsonapi_type:, attributes: nil, relationships: nil) Schemas.schema(schema_name) do property :data do key :type, :object property :type do key :type, :string key :required, true key :enum, [jsonapi_type] end if id property :id do key :type, :string end end if attributes instance_exec(attributes, &SchemaHelpers.attributes_schema) end if relationships instance_exec(relationships, &SchemaHelpers.relationships_schema) end end end end |
#jsonapi_payload(schema_name, payload) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/jsonapi_swagger_helpers/strong_resource_mixin.rb', line 74 def jsonapi_payload(schema_name, payload) jsonapi_input_schema(schema_name, payload) parameter do key :name, :payload key :in, :body schema do key :'$ref', schema_name end end end |
#strong_resource(controller, action) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jsonapi_swagger_helpers/strong_resource_mixin.rb', line 12 def strong_resource(controller, action) update = action == :update resource = controller._strong_resources[action] opts = { jsonapi_type: resource.jsonapi_type } if (attributes = strong_resource_attributes(resource, update)).present? opts[:attributes] = attributes end if (relationships = strong_resource_relationships(resource, update)).present? opts[:relationships] = relationships end jsonapi_payload(SecureRandom.uuid.to_sym, opts) end |
#strong_resource_attributes(resource, is_update = false) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/jsonapi_swagger_helpers/strong_resource_mixin.rb', line 28 def strong_resource_attributes(resource, is_update = false) attributes = {} resource.attributes.each_pair do |name, opts| type = StrongResources.config.strong_params[opts[:type]][:swagger] attributes[name] = type if is_update if resource.destroy? attributes[:_destroy] = :boolean end if resource.delete? attributes[:_delete] = :boolean end end end attributes = attributes.slice(*resource.only) if !!resource.only attributes = attributes.except(*resource.except) if !!resource.except attributes end |
#strong_resource_relationships(resource, is_update = false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jsonapi_swagger_helpers/strong_resource_mixin.rb', line 50 def strong_resource_relationships(resource, is_update = false) {}.tap do |relations| resource.relations.each_pair do |relation_name, opts| resource = opts[:resource] payload = { jsonapi_type: resource.jsonapi_type, id: true, array: resource.has_many? } if (attributes = strong_resource_attributes(resource, is_update)).present? payload[:attributes] = attributes end if (relationships = strong_resource_relationships(resource, is_update)).present? payload[:relationships] = relationships end relations[relation_name] = payload end end end |