Fix errors for four of the code samples #14313

Merged
merged 1 commit into from Mar 8, 2014

Conversation

Projects
None yet
2 participants

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.

activemodel/README.rdoc
+
+ def save
+ @previously_changed = changes
+ @changed_attributes.clear
@senny

senny Mar 8, 2014

Member

you can use changes_applied instead of modifying the instance vars.

@m-Peter

m-Peter Mar 8, 2014

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

@@ -88,6 +103,7 @@ behavior out of the box:
person.changed? # => true
person.changed # => ['name']
person.changes # => { 'name' => [nil, 'bob'] }
+ person.save
@senny

senny Mar 8, 2014

Member

I don't think we need to save at this point.

@m-Peter

m-Peter Mar 8, 2014

My intent was to maintain the output of this line: person.previous_changes. If we remove the call to Person#save the output will change to {'name' => [nil, 'robert']} .

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

Peter Markou
Fix errors for four of the code samples
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 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

Merge pull request #14313 from m-Peter/active_model_doc_fixes
Fix errors for four of the code samples [ci skip]

@senny senny merged commit 002c063 into rails:master Mar 8, 2014

Member

senny commented Mar 8, 2014

@m-Peter thanks 💛

@m-Peter m-Peter deleted the m-Peter:active_model_doc_fixes branch Mar 9, 2014

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