Class: DeclarativeGrid::Base

Inherits:
Object
  • Object
show all
Extended by:
Model, Renderers
Defined in:
lib/declarative_grid/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Model

column, columns, inherited, row

Methods included from Renderers

renderer_classes

Constructor Details

#initialize(records = nil) ⇒ Base

Constructor

Usage 1: grid = MyGrid.new { MyRecord.all }



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/declarative_grid/base.rb', line 13

def initialize(records = nil)
  if block_given?
    STDERR.puts 'WARNING: block-style instantization is deprecated'
    records ||= yield
  end
  
  if defined? ::ActiveRecord
    if records.is_a?(::ActiveRecord::Relation) or
       (records.is_a?(Class) and 
        records.ancestors.include?(::ActiveRecord::Base))
      records = Enumerator.new records, :find_each
    end
  end
  
  if defined? ::Sequel
    if records.is_a?(Class) && records.ancestors.include?(::Sequel::Model)
      raise unless records.is_a?(Enumerable)
    elsif records.is_a?(::Sequel::Dataset)
      raise unless records.is_a?(Enumerable)
    end
  end
  
  self.records = records
end

Instance Attribute Details

#recordsObject

Attributes



7
8
9
# File 'lib/declarative_grid/base.rb', line 7

def records
  @records
end

Instance Method Details

#inspectObject

Prevent from @records dumping



44
45
46
# File 'lib/declarative_grid/base.rb', line 44

def inspect
  self.class.inspect
end

#renderer(name) ⇒ Object

Renderer by name



39
40
41
# File 'lib/declarative_grid/base.rb', line 39

def renderer(name)
  self.class.renderer_classes[name].new(self.records)
end