Module: Sequel::Plugins::Dirty

Defined in:


The dirty plugin makes Sequel save the initial value of a column when setting a new value for the column. This makes it easier to see what changes were made to the object:                   # => 'Foo' = 'Bar'
artist.initial_value(:name)   # 'Foo'
artist.column_change(:name)   # ['Foo', 'Bar']
artist.column_changes         # {:name => ['Foo', 'Bar']}
artist.column_changed?(:name) # true
artist.reset_column(:name)                   # => 'Foo'
artist.column_changed?(:name) # false

It also makes changed_columns more accurate in that it can detect when a the column value is changed and then changed back:                   # => 'Foo' = 'Bar'
artist.changed_columns        # => [:name] = 'Foo'
artist.changed_columns        # => []

It can handle situations where a column value is modified in place:

artist.will_change_column(:name)!(/o/, 'u')
artist.changed_columns       # => [:name]
artist.initial_value(:name)  # => 'Foo'
artist.column_change(:name)  # => ['Foo', 'Fuu']

It also saves the previously changed values after an update:

artist.column_changes        # => {}
artist.previous_changes      # => {:name=>['Foo', 'Bar']}

There is one caveat; when used with a column that also uses the serialization plugin, setting the column back to its original value after changing it is not correctly detected and will leave an entry in changed_columns.


# Make all model subclass instances record previous values (called before loading subclasses)
Sequel::Model.plugin :dirty

# Make the Album class record previous values
Album.plugin :dirty

Defined Under Namespace

Modules: InstanceMethods