Skip to content

Commit

Permalink
Merge pull request #40500 from jonathanhefner/guides-irb-code-fences
Browse files Browse the repository at this point in the history
Use irb code fences where applicable [ci-skip]
  • Loading branch information
jonathanhefner committed Nov 2, 2020
2 parents 9f7d265 + 3c9d7a2 commit 71f743f
Show file tree
Hide file tree
Showing 17 changed files with 710 additions and 529 deletions.
3 changes: 1 addition & 2 deletions guides/source/2_3_release_notes.md
Expand Up @@ -520,8 +520,7 @@ XmlMini.backend = 'LibXML'
The `Time` and `TimeWithZone` classes include an `xmlschema` method to return the time in an XML-friendly string. As of Rails 2.3, `TimeWithZone` supports the same argument for specifying the number of digits in the fractional second part of the returned string that `Time` does:

```ruby
>> Time.zone.now.xmlschema(6)
=> "2009-01-16T13:00:06.13653Z"
Time.zone.now.xmlschema(6) # => "2009-01-16T13:00:06.13653Z"
```

* Lead Contributor: [Nicholas Dainty](http://www.workingwithrails.com/person/13536-nicholas-dainty)
Expand Down
3 changes: 1 addition & 2 deletions guides/source/5_1_release_notes.md
Expand Up @@ -144,8 +144,7 @@ The `direct` method allows creation of custom URL helpers.
``` ruby
direct(:homepage) { "http://www.rubyonrails.org" }

>> homepage_url
=> "http://www.rubyonrails.org"
homepage_url # => "http://www.rubyonrails.org"
```

The return value of the block must be a valid argument for the `url_for`
Expand Down
226 changes: 138 additions & 88 deletions guides/source/active_model_basics.md
Expand Up @@ -49,12 +49,17 @@ class Person
send(attribute) > 100
end
end
```

person = Person.new
person.age = 110
person.age_highest? # => true
person.reset_age # => 0
person.age_highest? # => false
```irb
irb> person = Person.new
irb> person.age = 110
irb> person.age_highest?
=> true
irb> person.reset_age
=> 0
irb> person.age_highest?
=> false
```

### Callbacks
Expand Down Expand Up @@ -102,11 +107,16 @@ class Person
nil
end
end
```

person = Person.new
person.to_model == person # => true
person.to_key # => nil
person.to_param # => nil
```irb
irb> person = Person.new
irb> person.to_model == person
=> true
irb> person.to_key
=> nil
irb> person.to_param
=> nil
```

### Dirty
Expand Down Expand Up @@ -149,51 +159,62 @@ end

#### Querying object directly for its list of all changed attributes.

```ruby
person = Person.new
person.changed? # => false
```irb
irb> person = Person.new
irb> person.changed?
=> false
person.first_name = "First Name"
person.first_name # => "First Name"
irb> person.first_name = "First Name"
irb> person.first_name
=> "First Name"
# returns true if any of the attributes have unsaved changes.
person.changed? # => true
# Returns true if any of the attributes have unsaved changes.
irb> person.changed?
=> true
# returns a list of attributes that have changed before saving.
person.changed # => ["first_name"]
# Returns a list of attributes that have changed before saving.
irb> person.changed
=> ["first_name"]
# returns a Hash of the attributes that have changed with their original values.
person.changed_attributes # => {"first_name"=>nil}
# Returns a Hash of the attributes that have changed with their original values.
irb> person.changed_attributes
=> {"first_name"=>nil}
# returns a Hash of changes, with the attribute names as the keys, and the
# values as an array of the old and new values for that field.
person.changes # => {"first_name"=>[nil, "First Name"]}
# Returns a Hash of changes, with the attribute names as the keys, and the values as an array of the old and new values for that field.
irb> person.changes
=> {"first_name"=>[nil, "First Name"]}
```

#### Attribute based accessor methods

Track whether the particular attribute has been changed or not.

```ruby
```irb
irb> person.first_name
=> "First Name"
# attr_name_changed?
person.first_name # => "First Name"
person.first_name_changed? # => true
irb> person.first_name_changed?
=> true
```

Track the previous value of the attribute.

```ruby
```irb
# attr_name_was accessor
person.first_name_was # => nil
irb> person.first_name_was
=> nil
```

Track both previous and current value of the changed attribute. Returns an array
if changed, otherwise returns nil.

```ruby
```irb
# attr_name_change
person.first_name_change # => [nil, "First Name"]
person.last_name_change # => nil
irb> person.first_name_change
=> [nil, "First Name"]
irb> person.last_name_change
=> nil
```

### Validations
Expand All @@ -211,17 +232,23 @@ class Person
validates_format_of :email, with: /\A([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})\z/i
validates! :token, presence: true
end
```

person = Person.new
person.token = "2b1f325"
person.valid? # => false
person.name = 'vishnu'
person.email = 'me'
person.valid? # => false
person.email = 'me@vishnuatrai.com'
person.valid? # => true
person.token = nil
person.valid? # => raises ActiveModel::StrictValidationFailed
```irb
irb> person = Person.new
irb> person.token = "2b1f325"
irb> person.valid?
=> false
irb> person.name = 'vishnu'
irb> person.email = 'me'
irb> person.valid?
=> false
irb> person.email = 'me@vishnuatrai.com'
irb> person.valid?
=> true
irb> person.token = nil
irb> person.valid?
ActiveModel::StrictValidationFailed
```

### Naming
Expand Down Expand Up @@ -277,14 +304,16 @@ When including `ActiveModel::Model` you get some features like:
It also gives you the ability to initialize an object with a hash of attributes,
much like any Active Record object.

```ruby
email_contact = EmailContact.new(name: 'David',
email: 'david@example.com',
message: 'Hello World')
email_contact.name # => 'David'
email_contact.email # => 'david@example.com'
email_contact.valid? # => true
email_contact.persisted? # => false
```irb
irb> email_contact = EmailContact.new(name: 'David', email: 'david@example.com', message: 'Hello World')
irb> email_contact.name
=> "David"
irb> email_contact.email
=> "david@example.com"
irb> email_contact.valid?
=> true
irb> email_contact.persisted?
=> false
```

Any class that includes `ActiveModel::Model` can be used with `form_with`,
Expand All @@ -311,11 +340,13 @@ end

Now you can access a serialized Hash of your object using the `serializable_hash` method.

```ruby
person = Person.new
person.serializable_hash # => {"name"=>nil}
person.name = "Bob"
person.serializable_hash # => {"name"=>"Bob"}
```irb
irb> person = Person.new
irb> person.serializable_hash
=> {"name"=>nil}
irb> person.name = "Bob"
irb> person.serializable_hash
=> {"name"=>"Bob"}
```

#### ActiveModel::Serializers
Expand Down Expand Up @@ -344,11 +375,13 @@ end
The `as_json` method, similar to `serializable_hash`, provides a Hash representing
the model.

```ruby
person = Person.new
person.as_json # => {"name"=>nil}
person.name = "Bob"
person.as_json # => {"name"=>"Bob"}
```irb
irb> person = Person.new
irb> person.as_json
=> {"name"=>nil}
irb> person.name = "Bob"
irb> person.as_json
=> {"name"=>"Bob"}
```

You can also define the attributes for a model from a JSON string.
Expand All @@ -374,11 +407,13 @@ end

Now it is possible to create an instance of `Person` and set attributes using `from_json`.

```ruby
json = { name: 'Bob' }.to_json
person = Person.new
person.from_json(json) # => #<Person:0x00000100c773f0 @name="Bob">
person.name # => "Bob"
```irb
irb> json = { name: 'Bob' }.to_json
irb> person = Person.new
irb> person.from_json(json)
=> #<Person:0x00000100c773f0 @name="Bob">
irb> person.name
=> "Bob"
```

### Translation
Expand Down Expand Up @@ -483,39 +518,54 @@ class Person

attr_accessor :password_digest, :recovery_password_digest
end
```

person = Person.new
```irb
irb> person = Person.new
# When password is blank.
person.valid? # => false
irb> person.valid?
=> false
# When the confirmation doesn't match the password.
person.password = 'aditya'
person.password_confirmation = 'nomatch'
person.valid? # => false
irb> person.password = 'aditya'
irb> person.password_confirmation = 'nomatch'
irb> person.valid?
=> false
# When the length of password exceeds 72.
person.password = person.password_confirmation = 'a' * 100
person.valid? # => false
irb> person.password = person.password_confirmation = 'a' * 100
irb> person.valid?
=> false
# When only password is supplied with no password_confirmation.
person.password = 'aditya'
person.valid? # => true
irb> person.password = 'aditya'
irb> person.valid?
=> true
# When all validations are passed.
person.password = person.password_confirmation = 'aditya'
person.valid? # => true

person.recovery_password = "42password"

person.authenticate('aditya') # => person
person.authenticate('notright') # => false
person.authenticate_password('aditya') # => person
person.authenticate_password('notright') # => false

person.authenticate_recovery_password('42password') # => person
person.authenticate_recovery_password('notright') # => false

person.password_digest # => "$2a$04$gF8RfZdoXHvyTjHhiU4ZsO.kQqV9oonYZu31PRE4hLQn3xM2qkpIy"
person.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
irb> person.password = person.password_confirmation = 'aditya'
irb> person.valid?
=> true
irb> person.recovery_password = "42password"
irb> person.authenticate('aditya')
=> #<Person> # == person
irb> person.authenticate('notright')
=> false
irb> person.authenticate_password('aditya')
=> #<Person> # == person
irb> person.authenticate_password('notright')
=> false
irb> person.authenticate_recovery_password('42password')
=> #<Person> # == person
irb> person.authenticate_recovery_password('notright')
=> false
irb> person.password_digest
=> "$2a$04$gF8RfZdoXHvyTjHhiU4ZsO.kQqV9oonYZu31PRE4hLQn3xM2qkpIy"
irb> person.recovery_password_digest
=> "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
```
10 changes: 7 additions & 3 deletions guides/source/active_record_basics.md
Expand Up @@ -339,10 +339,14 @@ A quick example to illustrate:
class User < ApplicationRecord
validates :name, presence: true
end
```

user = User.new
user.save # => false
user.save! # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
```irb
irb> user = User.new
irb> user.save
=> false
irb> user.save!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
```

You can learn more about validations in the [Active Record Validations
Expand Down

0 comments on commit 71f743f

Please sign in to comment.