module Datagrid::Core

Public Class Methods

included(base) click to toggle source

@!visibility private

# File lib/datagrid/core.rb, line 10
def self.included(base)
  base.extend ClassMethods
  base.class_eval do
    class_attribute :scope_value
    class_attribute :datagrid_attributes, instance_writer: false, default: []
    class_attribute :dynamic_block, instance_writer: false
    class_attribute :forbidden_attributes_protection, instance_writer: false, default: false
  end
end
new(attributes = nil, &block) click to toggle source
Calls superclass method
# File lib/datagrid/core.rb, line 126
def initialize(attributes = nil, &block)
  super()

  if attributes
    self.attributes = attributes
  end

  instance_eval(&dynamic_block) if dynamic_block
  if block_given?
    self.scope(&block)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/datagrid/core.rb, line 260
def ==(other)
  self.class == other.class &&
    attributes == other.attributes &&
    scope == other.scope
end
[](attribute) click to toggle source

@return [Object] Any datagrid attribute value

# File lib/datagrid/core.rb, line 161
def [](attribute)
  self.public_send(attribute)
end
[]=(attribute, value) click to toggle source

Assigns any datagrid attribute @param attribute [Symbol, String] Datagrid attribute name @param value [Object] Datagrid attribute value @return [void]

# File lib/datagrid/core.rb, line 169
def []=(attribute, value)
  self.public_send(:"#{attribute}=", value)
end
as_query() click to toggle source

Returns serializable query arguments skipping all nil values @example

grid = ProductsGrid.new(category: 'dresses', available: true)
grid.as_query # => {category: 'dresses', available: true}
# File lib/datagrid/core.rb, line 182
def as_query
  attributes = self.attributes.clone
  attributes.each do |key, value|
    attributes.delete(key) if value.nil?
  end
  attributes
end
assets() click to toggle source

@return [Object] a scope relation (e.g ActiveRecord::Relation) with all applied filters

# File lib/datagrid/core.rb, line 174
def assets
  scope
end
attributes() click to toggle source

@return [Hash<Symbol, Object>] grid attributes including filter values and ordering values

# File lib/datagrid/core.rb, line 141
def attributes
  result = {}
  self.datagrid_attributes.each do |name|
    result[name] = self[name]
  end
  result
end
attributes=(attributes) click to toggle source

Updates datagrid attributes with a passed hash argument @param attributes [Hash<Symbol, Object>] @example

grid = MyGrid.new
grid.attributes = {first_name: 'John', last_name: 'Smith'}
grid.first_name # => 'John'
grid.last_name # => 'Smith'
Calls superclass method
# File lib/datagrid/core.rb, line 156
def attributes=(attributes)
  super(attributes)
end
check_scope_defined!(message = nil) click to toggle source

@!visibility private

# File lib/datagrid/core.rb, line 248
def check_scope_defined!(message = nil)
  self.class.send :check_scope_defined!, message
end
driver() click to toggle source

@!visibility private

# File lib/datagrid/core.rb, line 243
def driver
  self.class.driver
end
inspect() click to toggle source

@return [String] a datagrid attributes and their values in inspection form

# File lib/datagrid/core.rb, line 253
def inspect
  attrs = attributes.map do |key, value|
    "#{key}: #{value.inspect}"
  end.join(", ")
  "#<#{self.class} #{attrs}>"
end
original_scope() click to toggle source

@!visibility private

# File lib/datagrid/core.rb, line 226
def original_scope
  check_scope_defined!
  scope_value.call
end
query_params(attributes = {}) click to toggle source

@return [Hash<Symbol, Hash<Symbol, Object>>] query parameters to link this grid from a page @example

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"
# File lib/datagrid/core.rb, line 195
def query_params(attributes = {})
  { param_name.to_sym => as_query.merge(attributes) }
end
redefined_scope?() click to toggle source

@return [Boolean] true if the scope was redefined for this instance of grid object

# File lib/datagrid/core.rb, line 238
def redefined_scope?
  self.class.scope_value != scope_value
end
reset() click to toggle source

Resets loaded assets and column values cache @return [void]

# File lib/datagrid/core.rb, line 268
def reset
  assets.reset
end
reset_scope() click to toggle source

Resets current instance scope to default scope defined in a class @return [void]

# File lib/datagrid/core.rb, line 233
def reset_scope
  self.scope_value = self.class.scope_value
end
scope(&block) click to toggle source

Redefines scope at instance level @example

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
# File lib/datagrid/core.rb, line 212
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

Protected Instance Methods

sanitize_for_mass_assignment(attributes) click to toggle source
Calls superclass method
# File lib/datagrid/core.rb, line 273
def sanitize_for_mass_assignment(attributes)
  forbidden_attributes_protection ? super(attributes) : attributes
end