Class: ActiveGrid::Grid

Inherits:
Object
  • Object
show all
Defined in:
app/lib/active_grid/grid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, name, scope, partial, options = {}) ⇒ Grid

Returns a new instance of Grid.



6
7
8
9
10
11
12
# File 'app/lib/active_grid/grid.rb', line 6

def initialize(view, name, scope, partial, options = {})
  @columns = []
  @view, @name, @scope, @partial, @options = view, name, scope, partial, options

  self.view._activegrid = self
  self.view.render(partial)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'app/lib/active_grid/grid.rb', line 4

def block
  @block
end

#columnsObject (readonly)

Returns the value of attribute columns.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def columns
  @columns
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def options
  @options
end

#partialObject (readonly)

Returns the value of attribute partial.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def partial
  @partial
end

#scopeObject (readonly)

Returns the value of attribute scope.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def scope
  @scope
end

#viewObject (readonly)

Returns the value of attribute view.



3
4
5
# File 'app/lib/active_grid/grid.rb', line 3

def view
  @view
end

Instance Method Details

#bodyObject



26
27
28
29
30
31
32
# File 'app/lib/active_grid/grid.rb', line 26

def body
  mode :body do
    models.map do |model|
      view.(:tr, view.capture(self, model, &block))
    end.reduce(:<<)
  end
end

#column(name, options = {}, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/lib/active_grid/grid.rb', line 34

def column(name, options = {}, &block)
  column = Column.new(view, name, options, &block)

  case mode
  when :header
    columns << column
    view.(:th, column.header, column.tag_options)
  when :filter
    view.(:th, column.filter)
  when :body
    view.(:td, column.value)
  end
end

#filtersObject



20
21
22
23
24
# File 'app/lib/active_grid/grid.rb', line 20

def filters
  mode :filter do
    view.(:tr, view.capture(self, nil, &block))
  end
end

#headerObject



14
15
16
17
18
# File 'app/lib/active_grid/grid.rb', line 14

def header
  mode :header do
    view.(:tr, view.capture(self, nil, &block))
  end
end

#mode(value = nil, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/lib/active_grid/grid.rb', line 48

def mode(value = nil, &block)
  if value
    begin
      old_mode, @mode = @mode, value
      yield
    ensure
      @mode = old_mode
    end
  else
    @mode
  end
end

#modelsObject



61
62
63
64
65
66
67
# File 'app/lib/active_grid/grid.rb', line 61

def models
  if paginate = options[:paginate]
    scope.limit(paginate)
  else
    scope
  end
end

#tag_optionsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/lib/active_grid/grid.rb', line 69

def tag_options
  tag_options = {}
  tag_options["id"] = "#{name}_grid"
  tag_options["class"] = "activegrid"
  tag_options["data-activegrid"] = true
  tag_options["data-activegrid-name"] = name

  if options[:paginate]
    tag_options["data-activegrid-pages"] = (scope.count / options[:paginate].to_f).ceil
    tag_options["data-activegrid-paginate"] = options[:paginate]
  end

  tag_options
end

#to_htmlObject



84
85
86
87
88
# File 'app/lib/active_grid/grid.rb', line 84

def to_html
  view.(:table, tag_options) do
    view.(:thead, header << filters) << view.(:tbody, body)
  end
end