Created
October 13, 2023 07:13
-
-
Save apotonick/95fabef571408d855f02ff0235fddeae to your computer and use it in GitHub Desktop.
TRB to the Max
This file contains hidden or 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
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'trailblazer-operation' | |
end | |
# require "trailblazer-operation" | |
module Image | |
module Operation | |
class Attach < Trailblazer::Operation | |
step :download | |
step :save | |
def download(ctx, url:, **) | |
ctx[:file] = Object.new # TODO: implement HTTPS download. | |
end | |
def save(ctx, file:, model:, **) | |
model.attach(file) | |
end | |
end | |
class AttachFromAPI < Attach # I wouldn't name it like that, though. | |
step :close_tmpfile, after: :save | |
# You can simply override the downloading {#download} | |
def download(ctx, api_url:, **) | |
ctx[:file] = Tempfile.new # TODO: implement API download. | |
end | |
def close_tmpfile(ctx, file:, **) | |
file.close | |
end | |
end | |
end | |
end | |
class ARImage | |
def attach(*) | |
true | |
end | |
end | |
class Tempfile | |
def close | |
true | |
end | |
end | |
# in your controller/script | |
result = Image::Operation::Attach.call(model: ARImage.new, url: "https://railscasts.sexy/topless.jpg") | |
puts result.success? | |
puts result[:model] | |
result = Image::Operation::AttachFromAPI.call(model: ARImage.new, api_url: "https://api.railscasts.sexy/img/topless.jpg") | |
puts result.success? | |
puts result[:model] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment