Sha256: aba4b954f49dad9f4b61ee24d66db5c562d85616dab806d98d4ac36714dc9275

Contents?: true

Size: 906 Bytes

Versions: 1

Compression:

Stored size: 906 Bytes

Contents

module ActiveRecord
  module Block
    # Block destroy and update just overwriting destroyable? and updateable?
    # methods
    #
    def self.included(base)
      base.extend(ClassMethods)

      base.load_cant_be_updated_validate
      base.load_cant_be_destroyed_callback
    end

    module ClassMethods
      def load_cant_be_updated_validate
        validate do
          if persisted? && !updateable?
            attribute_names.each do |attr|
              errors.add(attr, :cant_be_updated) if send("#{attr}_changed?")
            end
          end
        end
      end

      def load_cant_be_destroyed_callback
        before_destroy :cant_be_destroyed, if: -> { !destroyable? }
      end
    end

    def destroyable?
      true
    end

    def updateable?
      true
    end

    private

    def cant_be_destroyed
      errors.add(:base, :cant_be_destroyed)
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unico-training-7.8.0 lib/active_record/block.rb