Module: Datagrid::Core

Includes:
ActiveModel::AttributeAssignment
Included in:
Base
Defined in:
lib/datagrid/core.rb

Overview

Simple example of using Datagrid scope as the assets source to be queried from the database.

In most cases, the scope is a model class with some default ORM scopes, like order or includes:

The scope is also used to:

  • Choose an ORM driver (e.g., Mongoid, ActiveRecord, etc.).
  • Association preloading
  • Columns Providing default order

You can set the scope at class level or instance level. Both having appropriate use cases

Examples:

Defining a scope in a grid class

class ProjectsGrid < ApplicationGrid
  scope { Project.includes(:category) }
end

Setting a scope at the 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]

Retrieving and redefining the scope

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 (ignoring previously defined)
grid.scope { current_user.projects }
grid.redefined_scope? # => true

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Object



288
289
290
291
292
# File 'lib/datagrid/core.rb', line 288

def ==(other)
  self.class == other.class &&
    attributes == other.attributes &&
    scope == other.scope
end

#[](attribute) ⇒ Object

Returns Any datagrid attribute value.

Parameters:

  • attribute (String, Symbol)

    attribute name

Returns:

  • (Object)

    Any datagrid attribute value



195
196
197
# File 'lib/datagrid/core.rb', line 195

def [](attribute)
  public_send(attribute)
end

#[]=(attribute, value) ⇒ void

This method returns an undefined value.

Assigns any datagrid attribute

Parameters:

  • attribute (Symbol, String)

    Datagrid attribute name

  • value (Object)

    Datagrid attribute value



203
204
205
# File 'lib/datagrid/core.rb', line 203

def []=(attribute, value)
  public_send(:"#{attribute}=", value)
end

#as_queryHash{Symbol => Object}

Returns serializable query arguments skipping all nil values.

Examples:

grid = ProductsGrid.new(category: 'dresses', available: true)
grid.as_query # => {category: 'dresses', available: true}

Returns:

  • (Hash{Symbol => Object})

    serializable query arguments skipping all nil values



216
217
218
219
220
221
222
# File 'lib/datagrid/core.rb', line 216

def as_query
  attributes = self.attributes.clone
  attributes.each do |key, value|
    attributes.delete(key) if value.nil?
  end
  attributes
end

#assetsObject

Returns a scope relation (e.g ActiveRecord::Relation) with all applied filters.

Returns:

  • (Object)

    a scope relation (e.g ActiveRecord::Relation) with all applied filters



208
209
210
# File 'lib/datagrid/core.rb', line 208

def assets
  scope
end

#attributesHash{Symbol => Object}

Returns grid attributes including filter values and ordering values.

Examples:

class UsersGrid < ApplicationGrid
  scope { User }
  filter(:first_name, :string)
  filter(:last_name, :string)
end

grid = UsersGrid.new(first_name: 'John', last_name: 'Smith')
grid.attributes # => {first_name: 'John', last_name: 'Smith', order: nil, descending: nil}

Returns:

  • (Hash{Symbol => Object})

    grid attributes including filter values and ordering values



185
186
187
188
189
190
191
# File 'lib/datagrid/core.rb', line 185

def attributes
  result = {}
  datagrid_attributes.each do |name|
    result[name] = self[name]
  end
  result
end

#initialize(attributes = nil) {|block| ... } ⇒ void

Returns Initializes a new instance with optional attributes and an optional block.

Parameters:

  • attributes (Hash{String, Symbol => Object}) (defaults to: nil)

    a hash of attributes to initialize the object

Yields:

  • (block)

    an optional block that is passed to the scope method for further customization



164
165
166
167
168
169
170
171
172
173
# File 'lib/datagrid/core.rb', line 164

def initialize(attributes = nil, &block)
  super()

  self.attributes = attributes if attributes

  instance_eval(&dynamic_block) if dynamic_block
  return unless block_given?

  scope(&block)
end

#inspectString

Returns a datagrid attributes and their values in inspection form.

Returns:

  • (String)

    a datagrid attributes and their values in inspection form



281
282
283
284
285
286
# File 'lib/datagrid/core.rb', line 281

def inspect
  attrs = attributes.map do |key, value|
    "#{key}: #{value.inspect}"
  end.join(", ")
  "#<#{self.class} #{attrs}>"
end

#query_params(attributes = {}) ⇒ Hash{Symbol => Hash{Symbol => Object}}

Returns query parameters to link this grid from a page.

Examples:

grid = ProductsGrid.new(category: 'dresses', available: true)
Rails.application.routes.url_helpers.products_path(grid.query_params)
  # => "/products?products_grid[category]=dresses&products_grid[available]=true"

Returns:

  • (Hash{Symbol => Hash{Symbol => Object}})

    query parameters to link this grid from a page



229
230
231
# File 'lib/datagrid/core.rb', line 229

def query_params(attributes = {})
  { param_name.to_sym => as_query.merge(attributes) }
end

#redefined_scope?Boolean

Returns true if the scope was redefined for this instance of grid object.

Returns:

  • (Boolean)

    true if the scope was redefined for this instance of grid object



271
272
273
# File 'lib/datagrid/core.rb', line 271

def redefined_scope?
  self.class.scope_value != scope_value
end

#resetvoid

This method returns an undefined value.

Returns Resets loaded assets and column values cache.



295
296
297
# File 'lib/datagrid/core.rb', line 295

def reset
  assets.reset
end

#reset_scopevoid

This method returns an undefined value.

Returns Resets current instance scope to default scope defined in a class.



266
267
268
# File 'lib/datagrid/core.rb', line 266

def reset_scope
  self.scope_value = self.class.scope_value
end

#scope(&block) ⇒ void

This method returns an undefined value.

Returns redefines scope at instance level.

Examples:

class MyGrid
  scope { Article.order('created_at desc') }
end

grid = MyGrid.new
grid.scope do |scope|
  scope.where(author_id: current_user.id)
end
grid.assets
    # => SELECT * FROM articles WHERE author_id = ?
    #    ORDER BY created_at desc


246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/datagrid/core.rb', line 246

def scope(&block)
  if block_given?
    current_scope = scope
    self.scope_value = proc {
      Datagrid::Utils.apply_args(current_scope, &block)
    }
    self
  else
    scope = original_scope
    driver.to_scope(scope)
  end
end