README.mdown in scoped_from-0.8.0 vs README.mdown in scoped_from-0.8.1
- old
+ new
@@ -1,9 +1,9 @@
# ScopedFrom
Provides a simple mapping between scopes and controller parameters for
-[Ruby On Rails 3](http://rubyonrails.org/).
+[Ruby On Rails 4](http://rubyonrails.org/).
## Installation
Just add this into your `Gemfile`:
@@ -14,39 +14,39 @@
## Example
First, a model with some scopes:
class Post < ActiveRecord::Base
-
+
scope :commented, where('comments_count > 0')
scope :created_between, lambda { |after, before|
where('created_at >= ? AND created_at <= ?', after, before)
}
-
+
scope :search, lambda { |pattern|
where('body LIKE ?', "%#{pattern}%")
}
-
+
scope :with_category, lambda { |category_id|
where(:category_id, category_id)
}
-
+
end
-
+
After, a controller:
-
+
class PostsController < ActionController::Base
-
+
def index
@posts = Post.scoped_from(params)
end
-
+
end
-
+
Then, it just filter your model from params:
-
+
/posts?commented=1
/posts?search=rails
/posts?search=rails&commented=1&with_category=42
## Accepted scopes
@@ -75,11 +75,11 @@
If you need to map an SQL order, just pass `order` parameter:
@posts = Post.scoped_from(order: 'created_at')
Order direction can be specified using a dot, space or `:` as delimiter:
-
+
@posts = Post.scoped_from(order: 'created_at.desc')
Note that order is SQL safe with `scoped_from` method (columns names are
checked).
@@ -87,13 +87,13 @@
If your provide an array as parameter value, scope is invoked with each item
of the array:
@posts = Post.scoped_from(search: ['bar', 'foo'])
-
+
is equivalent to
-
+
@posts = Post.search('bar').search('foo')
You may also not want to filter on columns, just specify `:exclude_columns`
option:
@@ -116,18 +116,18 @@
But, you may also have to subclass this query class. You have to create a
subclass of `ScopedFrom::Query` named `#{RecordClassName}Query`. Here is an
example:
class PostQuery < ScopedFrom::Query
-
+
def category
Category.find_by_id(params[:with_category]) if params[:with_category]
end
-
+
end
This class has to be in load path.
-
+
Then into a view:
<% if @query.category %>
<p>All posts of category <%= @query.category.name %></p>
<% else %>