Skip to content
Bogdan Gusiev edited this page Aug 26, 2013 · 19 revisions

Scope

Simple scope definition

Datagrid scope as assets source to be queried from the database. In most cases it is a model class with some default ORM scopes like order or includes:

class ProjectsGrid
 include Datagrid
 scope { Project.includes(:category) }
end

Scope is also used to choose a ORM driver(MongoMapper, Mongoid or ActiveRecord), get wether filters and columns defined below has order.

You can set scope at instance level (version >= 0.9.2):

grid = ProjectsGrid.new(params[:my_grid]) do |scope|
  scope.where(:owner_id => current_user.id)
end

grid.assets # => SELECT * FROM projects WHERE projects.owner_id = ? AND [other filtering conditions]

Scope can always be retrieved and redefined at instance level:

grid.scope # => SELECT * FROM projects WHERE projects.user_id = ?

# Reset scope to default class value
grid.reset_scope # version >= 0.9.3
grid.assets # => SELECT * FROM projects

# Overwriting the scope
grid.scope { current_user.projects }
Clone this wiki locally