Sha256: a7f947c0dc37baf487b9d697983d514a6fba96b6e73bf50e7f0846b3c23629cc

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

require 'active_model/validations'

module ActiveModel
  module Validations
    # == Active Model Unchanged Validator
    class UnchangedValidator < EachValidator #:nodoc:
      def validate_each(record, attr_name, value)#
        if !record.new_record? && record.send(:"#{attr_name}_changed?")
          record.errors.add(attr_name, :changed, options)
        end
      end
    end

    module HelperMethods
      # Validates that the specified attributes are unchanged (unless the record is new).
      # Happens by default on save.
      #
      #   class Person < ActiveRecord::Base
      #     validates_unchanged :first_name
      #   end
      #
      # The first_name attribute must be in the object and it must not have changed.
      #
      # Configuration options:
      # * <tt>:message</tt> - A custom error message (default is: "must not change").
      #
      # There is also a list of default options supported by every validator:
      # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
      # See <tt>ActiveModel::Validation#validates</tt> for more information
      def validates_unchanged(*attr_names)
        validates_with UnchangedValidator, _merge_attributes(attr_names)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
unchanged_validator-1.0.1 lib/active_model/validations/unchanged_validator.rb
unchanged_validator-1.0.0 lib/active_model/validations/unchanged_validator.rb