Project

deep_dirty

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
ActiveRecord dirty tracking that compares every attribute to it's value before type cast and marks all changes detected as changed attributes. This makes it possible to detect `user.name.upcase!` `user.roles << 'some_role'` and similar implicit changes. To make it automatic on `save`, this module sets up a `before_validation` callback when included into a model.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
>= 0
>= 0

Runtime

>= 3.0.0
 Project Readme

DeepDirty

ActiveModel::Dirty does not track changes that are destructive or inplace:

user = User.first
user.name.upcase!
u.changed?          # => false

DeepDirty fixes this by adding a before_validation hook that compares all attributes to their before_type_cast values and records any changes.

Installation

Add this line to your application's Gemfile:

gem 'deep_dirty'

And then execute:

$ bundle

Or install it yourself as:

$ gem install deep_dirty

Usage

In an ActiveModel include this module:

class User < ActiveRecord::Base
    include DeepDirty
end

user = User.first
user.name.upcase!
user.valid?
user.changed?       # => true
user.changes        # => {"name" => ["Test User", "TEST USER"]}

Alternatively if you need to check changes without calling validation, use deep_changed?:

user = User.first
user.name.upcase!
user.changed?       # => false
user.deep_changed?  # => true
user.changed?       # => true
user.changes        # => {"name" => ["Test User", "TEST USER"]}

Contributing

  1. Fork it ( https://github.com/borgand/deep_dirty/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request