Skip to content
Bogdan Gusiev edited this page Oct 30, 2017 · 18 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(grid_params) 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 }
Clone this wiki locally