riak-ripple / ripple

A rich Ruby modeling layer for Riak, Basho's distributed database
Other
619 stars 152 forks source link

Dirty tracking for dynamically-defined attributes #242

Closed outoftime closed 12 years ago

outoftime commented 12 years ago

This would depend on #241, but anyway, it would be great to do dirty-tracking on dynamically-defined attributes (i.e. attributes not defined as properties, but set on model instances via the #[]= method).

Here is the relevant bits of the quick patch we're using in our application to handle this:

  def []=(attr, value)
    attribute_will_change!(attr.to_s) unless value.as_json == self[attr].as_json
    super
  end

  def attribute_change(attr)
    [changed_attributes[attr], self[attr]] if attribute_changed?(attr)
  end

  def attribute_will_change!(attr)
    value = self[attr]
    changed_attributes[attr] = value.duplicable? ? value.clone : value
  end
ntalbott commented 12 years ago

I'd say that this sort of functionality is the reason that attribute_will_change! is exposed; once you start coloring outside the "property lines", you're responsible for your own dirty tracking. @seancribbs do you concur?

seancribbs commented 12 years ago

"It's a bicycle accident."

I concur.