Permalink
Browse files

Merge pull request #28 from etehtsea/patch-1

README's code highligting
  • Loading branch information...
2 parents 2ec427f + 95411d2 commit 0baaf5b0c587c47dbeb2e154c14fef94ea008059 @hakanensari hakanensari committed Jun 15, 2011
Showing with 40 additions and 32 deletions.
  1. +40 −32 README.md
View
@@ -11,37 +11,43 @@ Quick Start
Add mongoid_slug to your Gemfile:
- gem 'mongoid_slug'
+```ruby
+gem 'mongoid_slug'
+```
Set up some slugs:
- class Book
- include Mongoid::Document
- include Mongoid::Slug
+```ruby
+class Book
+ include Mongoid::Document
+ include Mongoid::Slug
- field :title
- embeds_many :authors
+ field :title
+ embeds_many :authors
- slug :title
- end
+ slug :title
+end
- class Author
- include Mongoid::Document
- include Mongoid::Slug
+class Author
+ include Mongoid::Document
+ include Mongoid::Slug
- field :first
- field :last
- embedded_in :book, :inverse_of => :authors
+ field :first
+ field :last
+ embedded_in :book, :inverse_of => :authors
- slug :first, :last, :as => :name
- end
+ slug :first, :last, :as => :name
+end
+```
In your controller, use available finders:
- # GET /books/a-thousand-plateaus/authors/gilles-deleuze
- author = Book.find_by_slug(params[:book_id]).
- authors.
- find_by_name(params[:id])
+```ruby
+# GET /books/a-thousand-plateaus/authors/gilles-deleuze
+author = Book.find_by_slug(params[:book_id]).
+ authors.
+ find_by_name(params[:id])
+```
[Read here](https://github.com/papercavalier/mongoid-slug/blob/master/lib/mongoid/slug.rb)
for all available options.
@@ -51,18 +57,20 @@ Scoping
To scope a slug by a reference association, pass `:scope`:
- class Company
- include Mongoid::Document
- references_many :employees
- end
-
- class Employee
- include Mongoid::Document
- include Mongoid::Slug
- field :name
- slug :name, :scope => :company
- referenced_in :company
- end
+```ruby
+class Company
+ include Mongoid::Document
+ references_many :employees
+end
+
+class Employee
+ include Mongoid::Document
+ include Mongoid::Slug
+ field :name
+ slug :name, :scope => :company
+ referenced_in :company
+end
+```
In this example, if you create an employee without associating it with
any company, the scope will fall back to the root employees collection.

0 comments on commit 0baaf5b

Please sign in to comment.