Graphiti ActiveStorage attachment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/resources/application_resource.rb | |
# ApplicationResource is similar to ApplicationRecord - a base class that | |
# holds configuration/methods for subclasses. | |
# All Resources should inherit from ApplicationResource. | |
class ApplicationResource < Graphiti::Resource | |
# Use the ActiveRecord Adapter for all subclasses. | |
# Subclasses can still override this default. | |
self.abstract_class = true | |
self.adapter = Graphiti::Adapters::ActiveRecord | |
self.base_url = Rails.application.routes.default_url_options[:host] | |
self.endpoint_namespace = '/api/v1' | |
around_persistence :handle_attachment_attributes | |
protected | |
# Extract attachment from attributes before saving model | |
# Attach attachments after save | |
def handle_attachment_attributes(attrs) | |
attachments_params = extract_attachments_params(attrs) | |
model = yield | |
save_attachments(model, attachments_params) | |
end | |
#@return [Hash] Attributes definitions matching type "attachment" | |
def attachment_attributes | |
self.attributes.select{|_name, opts| opts[:type].to_s == "attachment"} | |
end | |
#@return [Hash] Extracted attachment params | |
def extract_attachments_params(attrs) | |
Hash.new.tap do |attachments_params| | |
attachment_attributes.each do |name, _opts| | |
attachments_params[name] = attrs.delete(name.to_sym) | |
end | |
end | |
end | |
# Add attachments to saved model | |
def save_attachments(model, attachments_params) | |
attachments_params.each do |name, params| | |
# ActiveStorage attachment name is inferred from the param name | |
# but can also be overridden with option `:attached_as` | |
type = attachment_attributes[name].fetch(:attached_as){name} | |
attach(model, type, params) | |
end | |
end | |
# Attach a single attachment | |
def attach(record, type, params) | |
file = tempfile_for(params[:data]) | |
record.send(type).attach(io: file, **params.slice(:filename, :content_type)) | |
end | |
def tempfile_for(data) | |
file = Tempfile.new('temp') | |
file.binmode | |
file.write(Base64.decode64(data)) | |
file.close | |
file.open | |
file | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/types/attachment.rb | |
module Types | |
module Attachment | |
# validate attachment format | |
# Use Types::Hash.strict to avoid sending unwanted params to ActiveStorage | |
WriteAttachment = Types::Hash.schema( | |
data: Types::Strict::String, | |
filename: Types::Strict::String, | |
content_type?: Types::Strict::String | |
).with_key_transform(&:to_sym).strict | |
# Serialize to Hash | |
ReadAttachment = Types::Nominal::Hash.constructor do |attachment| | |
if attachment.attached? | |
{ | |
url: Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true), | |
filename: attachment.filename, | |
content_type: attachment.content_type | |
} | |
else | |
nil | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": { | |
"id": "2235", | |
"type": "places", | |
"attributes": { | |
"name": "Test name", | |
"coordinates": { | |
"lon": 5.11135, | |
"lat": 45.41 | |
}, | |
"image": { | |
"url": "/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBcmNDIiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--6ba858d6903afb5290a959befe3fda6481c6332b/present.png", | |
"filename": "image.png", | |
"content_type": "image/png" | |
} | |
} | |
}, | |
"meta": {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data":{ | |
"type": "places", | |
"attributes":{ | |
"name": "Test name", | |
"coordinates": { | |
"lon": 5.11135, | |
"lat": 45.41 | |
}, | |
"image": { | |
"filename": "image.png", | |
"data": "iVBORw0KGgoAAAANSUh...truncated base64...5QzFVEtFUveAAAAAElFTkSuQmCC" | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/initializers/graphiti_types.rb | |
Graphiti::Types[:attachment] = { | |
params: Types::Attachment::WriteAttachment, | |
read: Types::Attachment::ReadAttachment, | |
write: Types::Attachment::WriteAttachment, | |
kind: 'record', | |
canonical_name: :attachment, | |
description: 'Active Storage file attached to the resource model' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/place.rb:7 | |
class Place < ApplicationRecord | |
validates :name, presence: true | |
validates :coordinates, presence: true | |
has_one_attached :photo | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/resources/place_resource.rb | |
class PlaceResource < ApplicationResource | |
attribute :name, :string | |
attribute :coordinates, :geo_point | |
attribute :image, :attachment, attached_as: "photo" | |
def base_scope | |
Place.with_attached_photo | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks! Any suggestions/plan to update it for
has_many_attached
?