Sha256: 19f9582a9b5c2babcf7c1f445ffc0d7adc7ce807f70e823c7a6dd39595316f20

Contents?: true

Size: 647 Bytes

Versions: 3

Compression:

Stored size: 647 Bytes

Contents

# write-once, read-many
#   Allows a value to be set to a non-nil value once, and then makes it immutable.
#   Combine with :presence=>true to accomplish the same thing as attr_readonly,
#   except with error messages (instead of silently refusing to save the change).
# eg: validates :user_id, :write_once=>true

module ActiveModel::Validations
  class WriteOnceValidator < ActiveModel::EachValidator
    def validate_each(record, attribute, value)
      if record.persisted? && record.send("#{attribute}_changed?") && !record.send("#{attribute}_was").nil?
        record.errors.add(attribute, :unchangeable, options)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
can_has_validations-0.3.0 lib/can_has_validations/validators/write_once_validator.rb
can_has_validations-0.2.1 lib/can_has_validations/validators/write_once_validator.rb
can_has_validations-0.2.0 lib/can_has_validations/validators/write_once_validator.rb