From 4dfab72190bd0ac1be7495f264a6e3a5f095fa86 Mon Sep 17 00:00:00 2001 From: Sam Pohlenz Date: Sun, 30 Jul 2017 20:21:08 +0930 Subject: [PATCH] Add README example of calling the collection block --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 435520d..363377d 100644 --- a/README.md +++ b/README.md @@ -23,10 +23,24 @@ Trestle.resource(:articles) do end ``` -The search block accepts one or two parameters; the first is the string value of the search query. The second, optional parameter is the complete `params` hash. +The search block accepts one or two parameters; the first is the string value of the search query. The second, optional parameter is the full `params` hash. The search block will be called instead of the default (or custom) `collection` block when the `q` query parameter is present (i.e. when a search query has been entered). It must return a chainable scope. +The original collection block can be called to avoid redefining scopes. For example: + +```ruby +Trestle.resource(:articles) do + collection do + Article.order(created_at: :desc).includes(:author) + end + + search do |q| + collection.where("title ILIKE ?", "%#{q}%") + end +end +``` + ## License