Class: Greedo::GridHelper::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/greedo/grid_helper.rb

Defined Under Namespace

Classes: Column, Field, Row

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context:, order: nil, order_by: nil, path_params: nil) ⇒ Grid

Returns a new instance of Grid.



8
9
10
11
12
13
14
15
16
17
# File 'lib/greedo/grid_helper.rb', line 8

def initialize(view_context:, order: nil, order_by: nil, path_params: nil)
  @view_context = view_context
  @row_id = ->(record) { default_row_id(record) }
  @fields = []
  @presenter = proc{|r| r}
  @empty_message_text = "No data to show."
  @order = order
  @order_by = order_by
  @path_params = path_params
end

Instance Attribute Details

#empty_message_textObject (readonly)

Returns the value of attribute empty_message_text.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def empty_message_text
  @empty_message_text
end

#fieldsObject (readonly)

Returns the value of attribute fields.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def fields
  @fields
end

#orderObject (readonly)

Returns the value of attribute order.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def order
  @order
end

#order_byObject (readonly)

Returns the value of attribute order_by.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def order_by
  @order_by
end

#paginatorObject

Returns the value of attribute paginator.



4
5
6
# File 'lib/greedo/grid_helper.rb', line 4

def paginator
  @paginator
end

#path_paramsObject (readonly)

Returns the value of attribute path_params.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def path_params
  @path_params
end

#presenter(klass = nil, &block) ⇒ Object (readonly)

Returns the value of attribute presenter.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def presenter
  @presenter
end

#view_contextObject (readonly)

Returns the value of attribute view_context.



5
6
7
# File 'lib/greedo/grid_helper.rb', line 5

def view_context
  @view_context
end

Instance Method Details

#column(name, label: name.to_s.humanize, sort: nil, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/greedo/grid_helper.rb', line 37

def column(name, label: name.to_s.humanize, sort: nil, &block)
  if sort.nil? && name.is_a?(Symbol)
    sort = name.to_s
  end
  if block
    renderer = ->(record) { view_context.capture(present(record), &block) }
  else
    renderer = ->(record) { present(record).public_send(name) }
  end

  fields << Field.new(name, label, renderer, order, order_by, view_context, sort, path_params)
  nil
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



19
20
21
# File 'lib/greedo/grid_helper.rb', line 19

def configure
  yield self if block_given?
end

#custom_empty_message(empty_message_text) ⇒ Object



23
24
25
26
# File 'lib/greedo/grid_helper.rb', line 23

def custom_empty_message(empty_message_text)
  @empty_message_text = empty_message_text
  nil
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/greedo/grid_helper.rb', line 71

def empty?
  records.empty?
end

#empty_messageObject



79
80
81
# File 'lib/greedo/grid_helper.rb', line 79

def empty_message
  view_context.(:p) { empty_message_text }
end

#headersObject



67
68
69
# File 'lib/greedo/grid_helper.rb', line 67

def headers
  fields
end

#ordered_byObject



28
29
30
# File 'lib/greedo/grid_helper.rb', line 28

def ordered_by
  fields.find(&:ordered_by?) || Field.new
end

#present(record) ⇒ Object



57
58
59
# File 'lib/greedo/grid_helper.rb', line 57

def present(record)
  @presenter.call(record)
end

#row_id(&block) ⇒ Object



32
33
34
35
# File 'lib/greedo/grid_helper.rb', line 32

def row_id(&block)
  @row_id = block
  nil
end

#rowsObject



61
62
63
64
65
# File 'lib/greedo/grid_helper.rb', line 61

def rows
  records.map do |record|
    Row.new(record, @row_id, fields)
  end
end

#show_pagination?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/greedo/grid_helper.rb', line 75

def show_pagination?
  paginator.show?
end