module Datagrid::Core::ClassMethods
Public Instance Methods
datagrid_attribute(name, &block)
click to toggle source
@!visibility private
# File lib/datagrid/core.rb, line 23 def datagrid_attribute(name, &block) unless datagrid_attributes.include?(name) block ||= lambda do |value| value end datagrid_attributes << name define_method name do instance_variable_get("@#{name}") end define_method :"#{name}=" do |value| instance_variable_set("@#{name}", instance_exec(value, &block)) end end end
driver()
click to toggle source
@!visibility private
# File lib/datagrid/core.rb, line 65 def driver @driver ||= Drivers::AbstractDriver.guess_driver(scope_value.call).new end
dynamic(&block)
click to toggle source
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, ... ] # ]
# File lib/datagrid/core.rb, line 99 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
original_scope()
click to toggle source
@!visibility private
# File lib/datagrid/core.rb, line 59 def original_scope check_scope_defined! scope_value.call end
scope(&block)
click to toggle source
Defines a scope at class level @return [void] @example
scope { User } scope { Project.where(deleted: false) } scope { Project.preload(:stages) }
# File lib/datagrid/core.rb, line 45 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
Protected Instance Methods
check_scope_defined!(message = nil)
click to toggle source
# File lib/datagrid/core.rb, line 114 def check_scope_defined!(message = nil) message ||= "#{self}.scope is not defined" raise(Datagrid::ConfigurationError, message) unless scope_value end
inherited(child_class)
click to toggle source
Calls superclass method
# File lib/datagrid/core.rb, line 119 def inherited(child_class) super(child_class) child_class.datagrid_attributes = self.datagrid_attributes.clone end