Skip to content

Instantly share code, notes, and snippets.

@TsubasaKawajiri
Last active April 21, 2022 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TsubasaKawajiri/1b1a023cf4f708441aa48238ce8f48c4 to your computer and use it in GitHub Desktop.
Save TsubasaKawajiri/1b1a023cf4f708441aa48238ce8f48c4 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "active_record"
require "active_model"
class Shrine
module Plugins
module FormObject
def self.load_dependencies(uploader, **)
uploader.plugin :model
uploader.plugin :activerecord
end
def self.configure(uploader, **opts)
uploader.opts[:form_object] ||= { validations: true }
uploader.opts[:form_object].merge!(opts)
end
module AttachmentMethods
def included(form_object)
super
return unless form_object.is_a? ::ActiveModel::Validations
return if form_object.is_a? ::ActiveRecord::Base
name = @name
if shrine_class.opts[:form_object][:validations]
form_object.validate do
send(:"#{name}_attacher").send(:activerecord_validate)
end
end
end
end
end
register_plugin(:form_object, FormObject)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment