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
  include Datagrid
  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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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.



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

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.



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

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


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

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