-
Notifications
You must be signed in to change notification settings - Fork 116
Scope
Bogdan Gusiev edited this page Nov 11, 2024
·
19 revisions
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) }
def scope
# scope can also be specified on the instance level
# here we have access to all the filter values.
end
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:
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 = ?
grid.redefined_scope? # => true
# Reset scope to default class value
grid.reset_scope
grid.assets # => SELECT * FROM projects
grid.redefined_scope? # => false
# Overwriting the scope (ignore previously defined)
grid.scope { current_user.projects }
grid.redefined_scope? # => true