Last active
April 21, 2022 10:32
-
-
Save TsubasaKawajiri/1b1a023cf4f708441aa48238ce8f48c4 to your computer and use it in GitHub Desktop.
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
# 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