Skip to content

Commit

Permalink
Move scope wiki page as Core module doc
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Nov 18, 2024
1 parent 8611ebc commit 9a53463
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion lib/datagrid/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,46 @@
require "datagrid/drivers"
require "active_support/core_ext/class/attribute"
require "active_model/attribute_assignment"

module Datagrid
# ## Simple example
#
# 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`:
#
# ``` ruby
# class ProjectsGrid
# include Datagrid
# scope { Project.includes(:category) }
# end
# ```
#
# Scope is also used to choose a ORM driver(Mongoid, ActiveRecord, etc.), get wether filters and columns defined below has order.
#
# You can set scope at instance level:
#
# ``` ruby
# 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:
#
# ``` ruby
# 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
# ```
module Core
include ::ActiveModel::AttributeAssignment

Expand Down

0 comments on commit 9a53463

Please sign in to comment.