Sha256: c848e814752366fd34fd7e068b08ae5b33317e6aa77ea4fc55aef8bc6b0a1c55

Contents?: true

Size: 474 Bytes

Versions: 1

Compression:

Stored size: 474 Bytes

Contents

require 'active_model'
require 'i18n'

class ImmutabilityValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, _)
    return if record.new_record?

    record.errors.add(attribute, options[:message] || I18n.t('.activerecord.errors.base.immutability')) if record.attribute_changed?(attribute)
  end
end

module ClassMethods
  def validates_immutability_of(*attr_names)
    validates_with ImmutabilityValidator, _merge_attributes(attr_names)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
immutability_validator-1.1.1 lib/immutability_validator.rb