rails/rails — pull request #18165 by petedmarsh (2014-12-23 14:51:13+00:00)
Title: A new validator: Unchanged validator

This ensures that the specified model attributes have not changed. For example:

class Person < ActiveRecord::Base
validates_unchanged :first_name
end

p = Person.first
#=> #

p.first_name = 'Bob'
p.valid?
#=> false

p.first_name = 'Dave'
p.valid?
#=> true

This is most useful when there are attributes on a model that should only be
set once, or at least only rarely.

I've have had a couple of instances where I've required a validator like this, and a quick Google turns up some instances of other people online asking for something similar - I think it may be useful.

There's two things I think need to be carefully looked at in this PR:

1. Tests - the style of the tests for various validators are quite different. I tried to follow the examples of presence_validator_test.rb and absence_validator_test.rb - please let me know if you would like me to change/improve the tests for this validator.
2. This validator requires the model to include `ActiveModel::Dirty` which means it doesn't work out-of-the-box with plain Ruby classes (as the other validators do). Is that ok?