Low commit activity in last 3 years
Library for validating unchangeable attributes.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 10.0
~> 3.0
> 0.90
>= 5.0, < 7.1

Runtime

>= 0
 Project Readme

Validates Unchangeable

This gem adds the capability of validating unchangeable attributes to ActiveRecord and ActiveModel.

Installation

Add this line to your application's Gemfile:

gem 'validates_unchangeable'

And then execute:

$ bundle

Or install it yourself as:

$ gem install validates_unchangeable

Usage

With ActiveRecord

class User < ActiveRecord::Base
  validates :email, unchangeable: true
end

With ActiveModel

class User
  include ActiveModel::Dirty
  include ActiveModel::Validations

  define_attribute_methods :email
  attr_reader :email

  validates :email, unchangeable: true

  def self.create(attributes = {})
    new(attributes).tap(&:save)
  end

  def initialize(attributes = {})
    @email = attributes[:email]
  end

  def value=(val)
    value_will_change! unless val == @email
    @email = val
  end

  def save
    changes_applied
  end
end

With RSpec

Require the matcher in spec_helper.rb or rails_helper.rb:

require 'validates_unchangeable/rspec_matcher'

In your spec:

describe User
  it { is_expected.to validate_unchangeable_of(:email) }
end

I18n

The default error message cannot be changed. You can pass the message: "my custom error" option to your validation to define your own, custom message.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validates_unchangeable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.