Skip to content

Scope

Bogdan Gusiev edited this page Sep 6, 2013 · 14 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 (ignore previously defined)
grid.scope { current_user.projects }
Something went wrong with that request. Please try again.