Skip to content

Commit

Permalink
Convert code blocks into GFM style
Browse files Browse the repository at this point in the history
  • Loading branch information
sikachu committed Sep 17, 2012
1 parent 0867fcd commit 7bc1ca3
Show file tree
Hide file tree
Showing 36 changed files with 4,103 additions and 4,103 deletions.
180 changes: 90 additions & 90 deletions guides/source/action_controller_overview.md

Large diffs are not rendered by default.

128 changes: 64 additions & 64 deletions guides/source/action_mailer_basics.md

Large diffs are not rendered by default.

576 changes: 288 additions & 288 deletions guides/source/action_view_overview.md

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions guides/source/active_model_basics.md
Expand Up @@ -14,7 +14,7 @@ h4. AttributeMethods

The AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes, which methods on the object will use them.

<ruby>
```ruby
class Person
include ActiveModel::AttributeMethods

Expand All @@ -41,13 +41,13 @@ person.age_highest? # true
person.reset_age # 0
person.age_highest? # false

</ruby>
```

h4. Callbacks

Callbacks gives Active Record style callbacks. This provides the ability to define the callbacks and those will run at appropriate time. After defining a callbacks you can wrap with before, after and around custom methods.

<ruby>
```ruby
class Person
extend ActiveModel::Callbacks

Expand All @@ -65,13 +65,13 @@ class Person
# This method will call when you are calling update on object as a before_update callback as defined.
end
end
</ruby>
```

h4. Conversion

If a class defines persisted? and id methods then you can include Conversion module in that class and you can able to call Rails conversion methods to objects of that class.

<ruby>
```ruby
class Person
include ActiveModel::Conversion

Expand All @@ -88,13 +88,13 @@ person = Person.new
person.to_model == person #=> true
person.to_key #=> nil
person.to_param #=> nil
</ruby>
```

h4. Dirty

An object becomes dirty when it has gone through one or more changes to its attributes and has not been saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Let's consider a Person class with attributes first_name and last_name

<ruby>
```ruby
require 'active_model'

class Person
Expand Down Expand Up @@ -124,11 +124,11 @@ class Person
end

end
</ruby>
```

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

<ruby>
```ruby
person = Person.new
person.first_name = "First Name"

Expand All @@ -145,42 +145,42 @@ person.changed_attributes #=> {"first_name" => "First Name Changed"}

#returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field.
person.changes #=> {"first_name" => ["First Name","First Name Changed"]}
</ruby>
```

h5. Attribute based accessor methods

Track whether the particular attribute has been changed or not.

<ruby>
```ruby
#attr_name_changed?
person.first_name #=> "First Name"

#assign some other value to first_name attribute
person.first_name = "First Name 1"

person.first_name_changed? #=> true
</ruby>
```

Track what was the previous value of the attribute.

<ruby>
```ruby
#attr_name_was accessor
person.first_name_was #=> "First Name"
</ruby>
```

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

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

h4. Validations

Validations module adds the ability to class objects to validate them in Active Record style.

<ruby>
```ruby
class Person
include ActiveModel::Validations

Expand All @@ -201,4 +201,4 @@ person.email = 'me@vishnuatrai.com'
person.valid? #=> true
person.token = nil
person.valid? #=> raises ActiveModel::StrictValidationFailed
</ruby>
```
60 changes: 30 additions & 30 deletions guides/source/active_record_basics.md
Expand Up @@ -74,58 +74,58 @@ h3. Creating Active Record Models

It is very easy to create Active Record models. All you have to do is to subclass the +ActiveRecord::Base+ class and you're good to go:

<ruby>
```ruby
class Product < ActiveRecord::Base
end
</ruby>
```

This will create a +Product+ model, mapped to a +products+ table at the database. By doing this you'll also have the ability to map the columns of each row in that table with the attributes of the instances of your model. Suppose that the +products+ table was created using an SQL sentence like:

<sql>
```sql
CREATE TABLE products (
id int(11) NOT NULL auto_increment,
name varchar(255),
PRIMARY KEY (id)
);
</sql>
```

Following the table schema above, you would be able to write code like the following:

<ruby>
```ruby
p = Product.new
p.name = "Some Book"
puts p.name # "Some Book"
</ruby>
```

h3. Overriding the Naming Conventions

What if you need to follow a different naming convention or need to use your Rails application with a legacy database? No problem, you can easily override the default conventions.

You can use the +ActiveRecord::Base.table_name=+ method to specify the table name that should be used:

<ruby>
```ruby
class Product < ActiveRecord::Base
self.table_name = "PRODUCT"
end
</ruby>
```

If you do so, you will have to define manually the class name that is hosting the fixtures (class_name.yml) using the +set_fixture_class+ method in your test definition:

<ruby>
```ruby
class FunnyJoke < ActiveSupport::TestCase
set_fixture_class :funny_jokes => 'Joke'
fixtures :funny_jokes
...
end
</ruby>
```

It's also possible to override the column that should be used as the table's primary key using the +ActiveRecord::Base.set_primary_key+ method:

<ruby>
```ruby
class Product < ActiveRecord::Base
set_primary_key "product_id"
end
</ruby>
```

h3. CRUD: Reading and Writing Data

Expand All @@ -137,73 +137,73 @@ Active Record objects can be created from a hash, a block or have their attribut

For example, given a model +User+ with attributes of +name+ and +occupation+, the +create+ method call will create and save a new record into the database:

<ruby>
```ruby
user = User.create(:name => "David", :occupation => "Code Artist")
</ruby>
```

Using the +new+ method, an object can be created without being saved:

<ruby>
```ruby
user = User.new
user.name = "David"
user.occupation = "Code Artist"
</ruby>
```

A call to +user.save+ will commit the record to the database.

Finally, if a block is provided, both +create+ and +new+ will yield the new object to that block for initialization:

<ruby>
```ruby
user = User.new do |u|
u.name = "David"
u.occupation = "Code Artist"
end
</ruby>
```

h4. Read

Active Record provides a rich API for accessing data within a database. Below are a few examples of different data access methods provided by Active Record.

<ruby>
```ruby
# return array with all records
users = User.all
</ruby>
```

<ruby>
```ruby
# return the first record
user = User.first
</ruby>
```

<ruby>
```ruby
# return the first user named David
david = User.find_by_name('David')
</ruby>
```

<ruby>
```ruby
# find all users named David who are Code Artists and sort by created_at in reverse chronological order
users = User.where(:name => 'David', :occupation => 'Code Artist').order('created_at DESC')
</ruby>
```

You can learn more about querying an Active Record model in the "Active Record Query Interface":"active_record_querying.html" guide.

h4. Update

Once an Active Record object has been retrieved, its attributes can be modified and it can be saved to the database.

<ruby>
```ruby
user = User.find_by_name('David')
user.name = 'Dave'
user.save
</ruby>
```

h4. Delete

Likewise, once retrieved an Active Record object can be destroyed which removes it from the database.

<ruby>
```ruby
user = User.find_by_name('David')
user.destroy
</ruby>
```

h3. Validations

Expand Down

0 comments on commit 7bc1ca3

Please sign in to comment.