ernie/squeel — Issue #70: Making calls conditional and chainable?
Author: brainlid
Created: 2011-10-23 00:44:03+00:00
State: open

Squeel may be able to do exactly what I want. I couldn't find any examples or documentation about this use case.

On my index action, I provide the user with a number of options for limiting the result set. Some of the filter options may be present and other not. So I need to conditionally apply various filters.

UsersController#index
```@users = @users.includes(:manager, :office).param_search(params).order(order_sql(params))```

This returns a scope that can be further built upon.

User.param_search applies the request params to create the filter conditions and returns a scope that can be built upon.
```ruby
def self.param_search(params = {})
scope = self.scoped
scope = scope.where("users.name ILIKE ?", "%#{params[:name]}%") if params[:name].present?
scope = scope.where("users.email ILIKE ?", "%#{params[:email]}%") if params[:email].present?
scope = scope.where(:is_active => params[:is_active]) if params[:is_active].present?
scope = scope.where(:office_id => params[:office]) if params[:office].present?
scope = scope.where(:manager_id => params[:manager]) if params[:manager].present?
scope
end
```

Each of the above scopes is conditionally applied and a chainable scope is returned. I'm hoping to use Squeel as it would clean up a lot of these types of searches. However, I can't figure out how to use Squeel this way. I can't seem to make it chainable to return a usable scope.

Have I missed something? Or is a different approach advised for conditionally applying filters and maintaining chainabilty?

Thanks,
-Mark

I have a