Navigation Menu

Skip to content
This repository has been archived by the owner on Jan 2, 2018. It is now read-only.

Commit

Permalink
Updated the chapters structure.
Browse files Browse the repository at this point in the history
  • Loading branch information
Julio Javier Cicchelli authored and mattetti committed Jan 30, 2009
1 parent 29e3350 commit e1363dc
Show file tree
Hide file tree
Showing 44 changed files with 518 additions and 1 deletion.
5 changes: 5 additions & 0 deletions book-content/es/10-recipes/toc.markdown
@@ -0,0 +1,5 @@
# recipes

<ul class='toc'><li><a href='/en/recipes/blog'>Simple blog</a></li></ul>

<ul class='toc'><li><a href='/en/recipes/slice'>Blog Slice</a></li></ul>
9 changes: 9 additions & 0 deletions book-content/es/11-deployment/toc.markdown
@@ -0,0 +1,9 @@
# deployment

<ul class='toc'><li><a href='/en/deployment/passenger'>Passenger</a><ul style='list-style: none;'><li><a href='/en/deployment/passenger#installing_ruby_enterprise_edition_ree'>Installing Ruby Enterprise Edition (REE)</a><ul style='list-style: none;'><li><a href='/en/deployment/passenger#download_ree'>Download REE</a></li><li><a href='/en/deployment/passenger#install'>Install</a></li></ul></li><li><a href='/en/deployment/passenger#installing_passenger'>Installing Passenger</a></li><li><a href='/en/deployment/passenger#configuration'>Configuration</a><ul style='list-style: none;'><li><a href='/en/deployment/passenger#configru'>config.ru</a></li></ul></li><li><a href='/en/deployment/passenger#capistrano_task'>Capistrano Task</a></li></ul></li></ul>

<ul class='toc'><li><a href='/en/deployment/nginx'>Nginx</a><ul style='list-style: none;'><li><a href='/en/deployment/nginx#installation'>Installation</a><ul style='list-style: none;'><li><a href='/en/deployment/nginx#mac_os_x_requires_macports'>Mac OS X (requires <a href='http://macports.org/'>MacPorts</a>)</a></li><li><a href='/en/deployment/nginx#debian__ubuntu_linux'>Debian / Ubuntu Linux</a></li><li><a href='/en/deployment/nginx#gentoo_linux'>Gentoo Linux</a></li></ul></li><li><a href='/en/deployment/nginx#configuration'>Configuration</a><ul style='list-style: none;'><li><a href='/en/deployment/nginx#etcnginxnginxconf'>/etc/nginx/nginx.conf</a></li></ul></li><li><a href='/en/deployment/nginx#capistrano_task'>Capistrano Task</a></li><li><a href='/en/deployment/nginx#monit'>Monit</a></li></ul></li></ul>

<ul class='toc'><li><a href='/en/deployment/jruby'>JRuby and Glassfish</a></li></ul>

<ul class='toc'><li><a href='/en/deployment/bundle'>Bundling your Merb application</a><ul style='list-style: none;'><li><a href='/en/deployment/bundle#why_bundling'>Why bundling</a></li><li><a href='/en/deployment/bundle#how_to_bundle'>How to bundle</a></li></ul></li></ul>
7 changes: 7 additions & 0 deletions book-content/es/13-appendix/toc.markdown
@@ -0,0 +1,7 @@
# appendix

<ul class='toc'><li><a href='/en/appendix/tips'>Hints and Tips</a></li></ul>

<ul class='toc'><li><a href='/en/appendix/slices'>Available Slices</a></li></ul>

<ul class='toc'><li><a href='/en/appendix/gems'>Useful Gems</a></li></ul>
@@ -0,0 +1,4 @@
# La fusión entre Merb y Rails

* Esto será una tabla de contenidos (este texto será pegado).
{:toc}
5 changes: 5 additions & 0 deletions book-content/es/15-migration-to-rails3/toc.markdown
@@ -0,0 +1,5 @@
# migration-to-rails3

<ul class='toc'>
<li><a href='/es/migration-to-rails3/merge'>La fusión entre Merb y Rails</a></li>
</ul>
@@ -0,0 +1,4 @@
# Configurando Active Record

* Esto será una tabla de contenidos (este texto será pegado).
{:toc}
@@ -0,0 +1,4 @@
# Consultas a la base de datos con Active Record

* Esto será una tabla de contenidos (este texto será pegado).
{:toc}
@@ -0,0 +1,6 @@
# Interactuando con la base de datos utilizando Active Record

<ul class='toc'>
<li><a href='/es/4-interacting-with-the-database-active_record/up'>Configurando Active Record</a></li>
<li><a href='/es/4-interacting-with-the-database-active_record/queries'>Consultas a la base de datos</a></li>
</ul>
@@ -0,0 +1,26 @@
# Instalacion y configuracion

## Generando un proyecto utilizando Sequel
A fin de crear una aplicacion Merb utilizando Sequel,
To create a core app with Sequel, merb-gen takes a `--orm` flag.

$ merb-gen core --orm sequel new_app

This will build the desired application, but falls a little short when it comes to bells and whistles.

To achieve results similar to merb-gen app, the merb-sequel-stack gem can be used.

$ git clone git://github.com/jackdempsey/merb-sequel-stack.git
$ cd merb-sequel-stack/
$ rake install

merb-gen will now include a sequel-app generator.
This will generate an app that uses Sequel, rspec, and the rest of what can be expected from `merb-gen app`.

$merb-gen sequel-app new_app

By default, `merb-gen sequel-app` will generate a database.yml file that uses sqlite3.
For more info, check out the [repository][].


[repository]: http://github.com/jackdempsey/merb-sequel-stack/tree
@@ -0,0 +1,53 @@
#Database Migrations

Sequel migrations will be generated with any merb-gen task that creates a model.

$ merb-gen model post
Generating with model generator:
[ADDED] spec/models/post_spec.rb
[ADDED] app/models/post.rb
[ADDED] schema/migrations/001_post_migration.rb
$ cat schema/migrations/001_post_migration.rb

# For details on Sequel migrations see
# http://sequel.rubyforge.org/

class PostMigration < Sequel::Migration

def up
create_table :posts do
primary_key :id
end
end

def down
drop_table :posts
end

end

Columns can be added to the table definition using the `#column` method or the column type can be used as the method in the specification.

class PostMigration < Sequel::Migration

def up
create_table :posts do
primary_key :id
string :title
text :body
boolean :published, :default => false
end
end
def down
drop_table :posts
end

end

After editing the migration, it can be run with a rake task.

$ rake sequel:db:migrate

A VERSION flag can be specified to roll back to particular migration versions.
@@ -0,0 +1,65 @@
#Database Retreival and Manipulation

##Finding records

Square brackets will return a single object.
A primary key can be passed in as parameter, or a hash, which is used as a set of conditions.

$ merb -i
>> Post[1]
=> #<Post @values={:published=>false, :body=>"This is a blog post", :id=>1, :title=>"First Post"}>

>> Post[:title => 'First Post']
=> #<Post @values={:published=>false, :body=>"This is a blog post", :id=>1, :title=>"First Post"}>

`Post.all` will return the entire set of Post objects.
The Sequel model is treated like a Sequel dataset.
All of the selection and refinement methods available to a dataset can be applied to a Sequel::Model.
To simplify the following examples, the SQL statements produced by Sequel::Model have been shown.

$ merb -i
>> Post.all
~ SELECT * FROM `posts`
>> Post.filter(:published => true).all
~ SELECT * FROM `posts` WHERE (`published` = 't')

>> Post.exclude(:published => true).all
~ SELECT * FROM `posts` WHERE (`published` != 't')
>> Post.order(:created_at.desc).paginate(2, 10).all
~ SELECT * FROM `posts` ORDER BY `created_at` DESC LIMIT 10 OFFSET 10
##The Other Three Letters in CRUD

###Create

>> p = Post.create(:title => 'second post', :body => 'another post for something')
=> #<Post @values={:published=>false, :body=>"another post for something", :id=>2, :title=>"second post"}>
>> p.new?
=> false

###Update

>> p = Post[2]
=> #<Post @values={:published=>false, :body=>"another post for something", :id=>2, :title=>"second post"}>
>> p.update(:title => 'replacement title')
=> #<Post @values={:published=>false, :body=>"another post for something", :id=>2, :title=>"replacement title"}>

###Delete

>> p = Post[2]
=> p.destroy
>> p.exists?
=> false

Mass deletions are also available.

# This will run destruction hooks on each object
Post.filter(:user_id => 3).destroy

# This is faster, but will not run hooks.
# It just runs a single query.
Post.filter(:user_id => 3).delete

A simple way to see Sequel CRUD in action is to merb-gen a resource and view the generated controller.
@@ -0,0 +1,40 @@
#Model Associations

`Sequel::Model` has a very simple set of association methods.
Sequel provides `many_to_one`, `one_to_many` and `many_to_many` to create database associations.


##One to Many

class Post < Sequel::Model
one_to_many :comments
end
{:lang=ruby html_use_syntax=true}



The Post class above now has a set of association methods similar to ActiveRecord's has\_many.

##Many to One

class Post < Sequel::Model
many_to_one :user
end
{:lang=ruby html_use_syntax=true}

##Many to Many

class Post < Sequel::Model
many_to_many :tags
end
{:lang=ruby html_use_syntax=true}

## One to One

class User < Sequel::Model
one_to_many :addresses, :one_to_one => true
end
{:lang=ruby html_use_syntax=true}



@@ -0,0 +1,55 @@
#Model Validation

`Sequel::Model` provides a set of validation methods by default.
The available standard validations are:

- acceptance\_of
- confirmation\_of
- format\_of
- length\_of
- numericality\_of
- presence\_of
- uniqueness\_of

Validations can be specified in two ways within a class.

class User < Sequel::Model

validates_confirmation_of :password
validates_length_of :login, :minimum => 5
# or
validates do
confirmation_of :password
length_of :login, :minimum => 5
end
end
{:lang=ruby html_use_syntax=true}



By default, Sequel::Model will raise an error on validation failures.
The class accessor `:raise_on_save_failure` can be set to false to avoid raising errors on validation failure.
If `raise_on_save_failure` is false, the `#save` method will return false on failures

An important distinction between Sequel::Model and ActiveRecord is the `#save!` method.
In Sequel::Model, `#save!` will update the record without validation.
This is significantly different from ActiveRecord, which will run validations and raise an error on failure.

If validation fails, errors can be accessed on the object with the `#errors` method.

User.raise_on_save_faliure = false
u = User.new(:login => 3)

u.save
=> false
u.errors.on(:login)
=> 'is too short'
{:lang=ruby html_use_syntax=true}


A full set of documentation for model validations can be found in the [Merb::Sequel documentation][]


[Merb::Sequel documentation]: http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html
@@ -0,0 +1,87 @@
#Sequel Plugins

Sequel::Model may seem like the minimal set of functionality for an ORM, but it is easy to extend its capabilities.
Sequel Plugins are libraries packaged as gems which can add useful functionality to your models.
Plugins are named like `sequel_pluginname`.
They're used in models by declaring `is :pluginname`.
Plugins for Sequel are painless to use, and are easy to write.


##Useful Plugins

###sequel\_timestamped

As the name would indicate, sequel\_timestamped will update standard timestamp columns in your model.

class Note < Sequel::Model
is :timestamped
end

If the model has `created_at` and `updated_at` timestamp columns, they will be updated as expected.

Here's the [sequel\_timestamped source][].

###sequel\_notnaughty

The sequel\_notnaughty plugin replaces standard Sequel::Model validation with not-naughty validation.
Not-naughty is a gem that provides an easily extensible validation framework.

class User < Sequel::Model
is :notnaughty

#now use the not-naughty syntax for validation
validates do
presence_of :login
length_of :login, :minimum => 5
end
end

Here's the [sequel\_notnaughty source][], and the [not-naughty source][].


###sequel\_polymorphic

Sequel\_polymorphic takes the hard work of setting up polymorphic relationships out of your hands.
The plugin adds functionality to Sequel's associations to handle these situations.

class Note < Sequel::Model
is :polymorphic
one_to_many :assets, :as => :attachable
end

class Asset < Sequel::Model
is :polymorphic
many_to_one :attachable, :polymorphic => true
end

This will create a transparent polymorphic relationship between the Note and its Assets.

Here's the [sequel\_polymorphic source][].

###sequel\_taggable

Sequel taggable is another plugin developed by Jack Dempsey while working with Merb.
It just adds simple tagging methods to a model.
As of right now, there are no tasks with the plugin to generate a Tag table.
There is, however, a migration included in the plugin source.

class Article < Sequel::Model
is :taggable
end

a = Article.create(:title => "My first article")
t = Tag.create(:name => "a tag")
a.add_tag(t)

Sequel\_taggable depends on the sequel\_polymophic plugin.
Here's the [sequel\_taggable source][].

[sequel_timestamped source]: http://github.com/bricooke/sequel_timestamped/tree/master

[sequel_notnaughty source]: http://github.com/boof/sequel_notnaughty/tree/master

[not-naughty source]: http://github.com/boof/not-naughty/tree/master

[sequel_polymorphic source]: http://github.com/jackdempsey/sequel_polymorphic/tree/master

[sequel_taggable source]: http://github.com/jackdempsey/sequel_taggable/tree/master

0 comments on commit e1363dc

Please sign in to comment.