module Shrine::Plugins::Activerecord::AttachmentMethods

  1. lib/shrine/plugins/activerecord.rb

Methods

Public Instance

  1. included

Public Instance methods

included (model)
[show source]
# File lib/shrine/plugins/activerecord.rb, line 90
        def included(model)
          super

          return unless model < ::ActiveRecord::Base

          opts = shrine_class.opts

          model.class_eval "            validate do
              #{@name}_attacher.errors.each do |message|
                errors.add(:#{@name}, *message)
              end
            end
", __FILE__, __LINE__ + 1 if opts[:activerecord_validations]

          model.class_eval "            before_save do
              #{@name}_attacher.save if #{@name}_attacher.changed?
            end

            after_commit on: [:create, :update] do
              #{@name}_attacher.finalize if #{@name}_attacher.changed?
            end

            after_commit on: [:destroy] do
              #{@name}_attacher.destroy
            end
", __FILE__, __LINE__ + 1 if opts[:activerecord_callbacks]
        end