Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AM::Dirty less dirty to plugin into AR or other library #10816

Closed
wants to merge 1 commit into from

Conversation

bogdan
Copy link
Contributor

@bogdan bogdan commented Jun 1, 2013

Added new API methods #reset_changes! and #changed_applied!
to control changes state. Previsously you needed to update internal
instance variables, but now API methods is available.

    class Person
      include ActiveModel::Dirty

      define_attribute_methods :name

      # Old way:

      def save
        # do some work
        @previously_changed = changes
        @changed_attributes = nil
      end

      def reload
        # do some work
        @previously_changed = nil
        @changed_attributes = nil
      end

      # New way:

      def save
        # do some work
        changes_applied!
      end

      def reload
        # do some work
        reset_changes!
      end
    end

@egilburg
Copy link
Contributor

egilburg commented Jun 1, 2013

  1. If calling reset_changes!, should other method be called apply_changes! for consistency?
  2. Not sure using ! in method name is a good idea as you are only setting state for later use. I'd conventionally expect a method ending with ! to either possibly raise exceptions, or to perform an immediate destructive/persistive change, neither of which happen here.
  3. Risk of shadowing user defined methods (not sure if big deal or avoidable)

@@ -142,9 +136,22 @@ def changed_attributes
@changed_attributes ||= {}
end

# Removes current changes and makes them accessible through
# #previous_changes.
def changes_applied!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my opinion these methods should not be public

@rafaelfranca
Copy link
Member

@carlosantoniodasilva @josevalim do you have some input about this?

@guilleiguaran
Copy link
Member

👍 on this (with all the changes suggested by @rafaelfranca)

@bogdan
Copy link
Contributor Author

bogdan commented Jun 3, 2013

Made all changes suggested by @rafaelfranca

@changed_attributes = {}
end

# Handle changes<tt>*_changed?</tt> for +method_missing+.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is still wrong. Should not be: Handle <tt>*_changed?</tt> for +method_missing+.?

@rafaelfranca
Copy link
Member

I agree with @egilburg about the bang on the method name. I'd only define they without the bang

@bogdan
Copy link
Contributor Author

bogdan commented Jun 4, 2013

Fixed comment, removed bang.

@@ -144,6 +138,19 @@ def changed_attributes

private

# Removes current changes and makes them accessible through
# #previous_changes.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be +previous_changes+

@rafaelfranca
Copy link
Member

We will need a CHANGELOG entry and a rebase. So I'll merge

@ghost ghost assigned rafaelfranca Sep 22, 2013
Added new API methods #reset_changes and #changed_applied
to control changes state. Previsously you needed to update internal
instance variables, but now API methods is available.

    class Person
      include ActiveModel::Dirty

      define_attribute_methods :name

      # Old way:

      def save
        # do some work
        @previously_changed = changes
        @changed_attributes = nil
      end

      def reload
        # do some work
        @previously_changed = nil
        @changed_attributes = nil
      end

      # New way:

      def save
        # do some work
        changes_applied!
      end

      def reload
        # do some work
        reset_changes
      end
    end
@bogdan
Copy link
Contributor Author

bogdan commented Sep 23, 2013

Done

rafaelfranca added a commit that referenced this pull request Sep 23, 2013
Make AM::Dirty less dirty to plugin into AR or other library
@rafaelfranca
Copy link
Member

Merged

@bogdan bogdan deleted the less-dirty-dirty branch May 7, 2014 15:30
@dhh
Copy link
Member

dhh commented Jul 14, 2014

We discussing the problem of how "reset_changes" doesn't actually reset the changes, as in restore them to their original value. That's creating a mismatch with reset_, which does do that.

Would be great to see some real use cases that prompted adding reset_changes in the first place. Can people using it on this thread share?

@chancancode
Copy link
Member

Ah, this is how we refactored Active Record to use reset_changes in this PR: 9aa1a3d#diff-2bd7c52253c45ccc631ca0d1199d1201R34

It's probably more of a "plugin API" rather than something an ordinary user would use in their app.

@rafaelfranca
Copy link
Member

Yes it is intended to be a plugin API.
On Jul 14, 2014 7:54 PM, "Godfrey Chan" notifications@github.com wrote:

Ah, this is how we refactored Active Record to use reset_changes in this
PR: 9aa1a3d#diff-2bd7c52253c45ccc631ca0d1199d1201R34
9aa1a3d#diff-2bd7c52253c45ccc631ca0d1199d1201R34

It's probably more of a "plugin API" rather than something an ordinary
user would use in their app.


Reply to this email directly or view it on GitHub
#10816 (comment).

@bogdan
Copy link
Contributor Author

bogdan commented Jul 15, 2014

After a fresh look at this PR reset_changes looks really stupid name now. I agree with @dhh.

The good name would be clear_dirty_data as it is not part of public API outside of activerecord and it don't need to be short and simple.

I think we can still rename it and deprecate old method without causing too many problems to people who are mixing up Dirty outside of ActiveRecord..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants