module Datagrid::Core
Public Class Methods
@!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
# 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
# File lib/datagrid/core.rb, line 260 def ==(other) self.class == other.class && attributes == other.attributes && scope == other.scope end
@return [Object] Any datagrid attribute value
# File lib/datagrid/core.rb, line 161 def [](attribute) self.public_send(attribute) end
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
@return [Object] a scope relation (e.g ActiveRecord::Relation) with all applied filters
# File lib/datagrid/core.rb, line 174 def assets scope end
@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
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'
# File lib/datagrid/core.rb, line 156 def attributes=(attributes) super(attributes) end
@!visibility private
# File lib/datagrid/core.rb, line 248 def check_scope_defined!(message = nil) self.class.send :check_scope_defined!, message end
@!visibility private
# File lib/datagrid/core.rb, line 243 def driver self.class.driver end
@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
@!visibility private
# File lib/datagrid/core.rb, line 226 def original_scope check_scope_defined! scope_value.call end
@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
@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
Resets loaded assets and column values cache @return [void]
# File lib/datagrid/core.rb, line 268 def reset assets.reset end
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
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
# File lib/datagrid/core.rb, line 273 def sanitize_for_mass_assignment(attributes) forbidden_attributes_protection ? super(attributes) : attributes end