module Datagrid::Core::ClassMethods
Public Instance Methods
Source
# File lib/datagrid/core.rb, line 148 def check_scope_defined!(message = nil) message ||= "#{self}.scope is not defined" raise(Datagrid::ConfigurationError, message) unless scope_value end
@!visibility private
Source
# File lib/datagrid/core.rb, line 61 def datagrid_attribute(name, &block) return if datagrid_attributes.include?(name) datagrid_attributes << name define_method name do instance_variable_get("@#{name}") end define_method :"#{name}=" do |value| instance_variable_set("@#{name}", block ? instance_exec(value, &block) : value) end end
@!visibility private
Source
# File lib/datagrid/core.rb, line 100 def driver @driver ||= Drivers::AbstractDriver.guess_driver(scope_value.call).new end
@!visibility private
Source
# File lib/datagrid/core.rb, line 134 def dynamic(&block) previous_block = dynamic_block self.dynamic_block = if previous_block proc { instance_eval(&previous_block) instance_eval(&block) } else block end end
Allows dynamic columns definition, that could not be defined at class level Columns
that depend on the database state or third party service can be defined this way. @param block [Proc] block that defines dynamic columns @return [void] @example
class MerchantsGrid scope { Merchant } column(:name) dynamic do PurchaseCategory.all.each do |category| column(:"#{category.name.underscore}_sales") do |merchant| merchant.purchases.where(category_id: category.id).count end end end end ProductCategory.create!(name: 'Swimwear') ProductCategory.create!(name: 'Sportswear') grid = MerchantsGrid.new grid.data # => [ # [ "Name", "Swimwear Sales", "Sportswear Sales", ... ] # [ "Reebok", 2083382, 8382283, ... ] # [ "Nike", 8372283, 18734783, ... ] # ]
Source
# File lib/datagrid/core.rb, line 94 def original_scope check_scope_defined! scope_value.call end
@!visibility private
Source
# File lib/datagrid/core.rb, line 80 def scope(&block) if block current_scope = scope_value self.scope_value = proc { Datagrid::Utils.apply_args(current_scope ? current_scope.call : nil, &block) } self else scope = original_scope driver.to_scope(scope) end end
Defines a relation scope of database models to be filtered @return [void] @example
scope { User } scope { Project.where(deleted: false) } scope { Project.preload(:stages) }
Protected Instance Methods
Source
# File lib/datagrid/core.rb, line 156 def inherited(child_class) super child_class.datagrid_attributes = datagrid_attributes.clone end
@!visibility private
Calls superclass method