An operation using the module:
module Api
module V1
module Page
class Update
include Operation
attr_reader :resource
SCHEMA = Dry::Validation.JSON do
optional(:keywords, [:nil, Yubb::Types::ArrayOfStrings]).each(:str?)
optional(:description, [:nil, :string]).filled(:str?)
end
def process(resource, input)
@resource = resource
schema = SCHEMA.call(input)
return Flow::Failure.call(schema.errors) if schema.failure?
resource.update(input)
# if you need to send an email, invoke a job or something else, all of this could be done here
Flow::Success.call decorated_resource
end
private
def decorated_resource
Decorators::Resource.new(resource)
end
end
end
end
end
Then on the controller:
module Api
module V1
class PagesController < BaseController
before_action :authenticate!
before_action :authorize!
def update
Api::V1::Page::Update.call(resource, payload) do |on|
on.success { |output| return json_ok(output) }
on.failure { |errors| return json_unprocessable_entity(errors) }
end
end
private
def resource
@resource ||= ::Page.find_by!(slug: params[:id])
end
end
end
end