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

Fix errors for four of the code samples #14313

Merged
merged 1 commit into from
Mar 8, 2014
Merged

Fix errors for four of the code samples #14313

merged 1 commit into from
Mar 8, 2014

Conversation

m-Peter
Copy link

@m-Peter m-Peter commented Mar 7, 2014

The four code samples that fail to run are:

  • Add attribute magic to objects. Fixed by introducing a Person
    instance variable.
  • Tracking value changes. Fixed by replacing attr_accessor with
    define_attribute_methods, providing getter and setter methods
    for name and providing the missing Person#save method. A
    call to Person#save has to precede the person.name = 'robert'
    assignment, if we want previous_changes to include 'bob'.
  • Adding errors interface to objects. Fixed by introducing a
    Person instance variable, assigning nil to its name and calling
    Person#validate!.
  • Custom validators. Fixed by defining HasNameValidator before
    it is used by ValidatorPerson.

All the code samples can now be run smoothly.


def save
@previously_changed = changes
@changed_attributes.clear
Copy link
Member

Choose a reason for hiding this comment

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

you can use changes_applied instead of modifying the instance vars.

Copy link
Author

Choose a reason for hiding this comment

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

You are correct. As the documentation states, this method should be called after changes are persisted.

@senny
Copy link
Member

senny commented Mar 8, 2014

@m-Peter added a few minor comments, the rest looks good.

@senny senny added docs labels Mar 8, 2014
The four code samples that fail to run are:

- Add attribute magic to objects. Fixed by introducing a Person
  instance variable.

- Tracking value changes. Fixed by replacing `attr_accessor` with
  `define_attribute_methods`, providing getter and setter methods
  for `name` and providing the missing `Person#save` method. A
  call to `Person#save` has to precede the `person.name = 'robert'`
  assignment, if we want `previous_changes` to include 'bob'.

- Adding `errors` interface to objects. Fixed by introducing a
  Person instance variable, assigning `nil` to its name and calling
  `Person#validate!`.

- Custom validators. Fixed by defining `HasNameValidator` before
  it is used by `ValidatorPerson`.

All the code samples can now be run smoothly.

Call Dirty#changes_applied in Person#save, instead of modifying instance vars.
@m-Peter
Copy link
Author

m-Peter commented Mar 8, 2014

Made the change you proposed and squashed the commits into one, as the Contributing to Ruby on Rails guide suggests.

senny added a commit that referenced this pull request Mar 8, 2014
Fix errors for four of the code samples [ci skip]
@senny senny merged commit 002c063 into rails:master Mar 8, 2014
@senny
Copy link
Member

senny commented Mar 8, 2014

@m-Peter thanks 💛

@m-Peter m-Peter deleted the active_model_doc_fixes branch March 9, 2014 09:03
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.

2 participants