No commit activity in last 3 years
No release in over 3 years
A validator for ActiveModels that checks that attributes have not been modified since the last time the model was saved.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.0
~> 1.6
~> 10.0
~> 3.1

Runtime

 Project Readme

UnchangedValidator

Build Status Code Climate

A validator for ActiveModels that checks that attributes have not been modified since the last time the model was saved.

Installation

Add this line to your application's Gemfile:

gem 'unchanged_validator'

And then execute:

$ bundle

Or install it yourself as:

$ gem install unchanged_validator

Running the tests

Install various dependencies:

$ appraisal install

Then, to run the tests:

$ appraisal rspec

Usage

Example

class Person < ActiveRecord::Base
  validates_unchanged :first_name
end

p = Person.new
p.first_name = 'Bob'
p.save
#=> true
p.valid?
#=> true

p.first_name = 'Dave'
p.save
#=> false
p.valid?
#=> false

I18n

To change the default message for all usages of validates_unchanged:

# In config/locales/en.yml
en:
  activerecord:
    errors:
      messages:
        changed: 'cannot be different'

To change the message for a particular attribute of a particular model:

# In config/locales/en.yml
en:
  activerecord:
    models:
      person:
        attributes:
          first_name:
            changed: 'cannot be different'

Options

UnchangedValidator supports all of the common ActiveModel::Validations options:

class Book < ActiveRecord::Base
  validates_unchanged :title, allow_blank: true
  validates_unchanged :author, allow_nil: true
  validates_unchanged :copyright_expirty_date, if: :same_edition?
  validates_unchanged :published_date, message: 'cannot be re-released'
  validates_unchanged :foreward, on: :update
  validates_unchanged :publisher, unless: :rebranded?
end

Supported Rails Versions

  • 3.1
  • 3.2
  • 4.0
  • 4.1
  • 4.2