Module: Datagrid::Core::ClassMethods

Defined in:
lib/datagrid/core.rb

Overview

self.included

Instance Method Summary collapse

Instance Method Details

#datagrid_attribute(name, &block) ⇒ Object

:nodoc:


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/datagrid/core.rb', line 27

def datagrid_attribute(name, &block) #:nodoc:
  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

#driverObject

:nodoc:


57
58
59
# File 'lib/datagrid/core.rb', line 57

def driver #:nodoc:
  @driver ||= Drivers::AbstractDriver.guess_driver(scope).new
end

#dynamic(&block) ⇒ Object

Allows dynamic columns definition, that could not be defined at class level

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

grid = MerchantsGrid.new
grid.data # => [
          #      [ "Name",   "Swimwear Sales", "Sportswear Sales", ... ]
          #      [ "Reebok", 2083382,            8382283,          ... ]
          #      [ "Nike",   8372283,            18734783,         ... ]
          #    ]

84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datagrid/core.rb', line 84

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

#scope(&block) ⇒ Object

Defines a scope at class level


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/datagrid/core.rb', line 44

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
    check_scope_defined!
    scope_value.call
  end
end