Class: Margrid::Grid

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

Instance Method Summary collapse

Constructor Details

#initialize(id, relation) ⇒ Grid

Returns a new instance of Grid.



3
4
5
6
7
8
# File 'lib/margrid/grid.rb', line 3

def initialize(id, relation)
  @id = id
  @relation = relation
  @registered_components = {sorter: Sorter, paginator: Paginator}
  @components = {}
end

Instance Method Details

#component(comp_sym) ⇒ Object

Get the component for a given comp_sym.



22
23
24
# File 'lib/margrid/grid.rb', line 22

def component(comp_sym)
  @components[comp_sym]
end

#component?(comp_sym) ⇒ Boolean

Check wether a component is registered or not.

Returns:

  • (Boolean)


17
18
19
# File 'lib/margrid/grid.rb', line 17

def component?(comp_sym)
  @components.key? comp_sym
end

#dump(comps = nil) ⇒ Object

Dump components to a nested Hash.



47
48
49
50
51
52
53
# File 'lib/margrid/grid.rb', line 47

def dump(comps = nil)
  comps ||= @components.values
  grid_params = comps.inject({}) do |params, comp|
    params.merge(comp.dump)
  end
  {"margrid" => {@id => grid_params}}
end

#load(params) ⇒ Object

Load components from a Hash.



36
37
38
39
40
41
42
43
44
# File 'lib/margrid/grid.rb', line 36

def load(params)
  grid_params = params.fetch("margrid", {}).fetch(@id, {})
  @registered_components.each do |comp_sym, comp_class|
    if comp = comp_class.load(grid_params)
      prepend comp_sym => comp
    end
  end
  self
end

#param_prefixObject



75
76
77
# File 'lib/margrid/grid.rb', line 75

def param_prefix
  @param_prefix ||= "margrid[#{@id}]"
end

#prepend(comp_hash) ⇒ Object

Attach components to the grid. This method can be called multiple times. Prepending a component will overwrite existing components.

comp_hash uses a comp_sym as key and a component instance as value.



30
31
32
33
# File 'lib/margrid/grid.rb', line 30

def prepend(comp_hash)
  @components.update comp_hash
  self
end

#register(comp_sym, comp_class, comp = nil) ⇒ Object



10
11
12
13
14
# File 'lib/margrid/grid.rb', line 10

def register(comp_sym, comp_class, comp = nil)
  @registered_components[comp_sym] = comp_class
  @components[comp_sym] = comp unless comp.nil?
  self
end

#rowsObject

Rows to display in the grid. This will apply every registered component to the ActiveRecord::Relation passed upon grid initialization. This method returns a new ActiveRecord::Relation representing the grid rows.



69
70
71
72
73
# File 'lib/margrid/grid.rb', line 69

def rows
  @rows ||= @components.values.inject(@relation) do |relation, comp|
    comp.apply(relation)
  end
end

#to_query(comps = nil) ⇒ Object

Dump components to a Hash. The key of the Hash is the rack param name.



56
57
58
59
60
61
62
63
64
# File 'lib/margrid/grid.rb', line 56

def to_query(comps = nil)
  comps ||= @components.values
  comps.inject({}) do |params, comp|
    comp.dump.each do |k, v|
      params[param_prefix + "[#{k}]"] = v
    end
    params
  end
end