Class: ActionTable::View

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/action_table/view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns:, records:, paginate: false, links: ActionTable.config.links, actions: ActionTable.config.actions) ⇒ View

Returns a new instance of View.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/action_table/view.rb', line 12

def initialize(
  columns:,
  records:,
  paginate: false,
  links: ActionTable.config.links,
  actions: ActionTable.config.actions
)
  @columns    = Array(columns).map(&:to_s)
  @rows       = records
  @paginate   = paginate
  @links      = Set.new(Array(links).map(&:to_s)).reject(&:empty?)
  @actions    = Array(actions).map(&:to_s)
end

Instance Attribute Details

#rowsObject (readonly)

Returns the value of attribute rows.



10
11
12
# File 'lib/action_table/view.rb', line 10

def rows
  @rows
end

Instance Method Details

#actions_headerObject

add header column padding for actions



31
32
33
# File 'lib/action_table/view.rb', line 31

def actions_header
  @actions_header ||= [''] * @actions.length
end

#columns(record) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/action_table/view.rb', line 35

def columns(record)
  attribute_columns = @columns.map do |name|
    title = record.public_send(name)
    if title.present? && @links.include?(name)
      link_to(title, record_path(record))
    else
      title
    end
  end

  actions = t_actions.zip(@actions).map! do |data|
    title, name = data
    link_to(title, record_path(record, action: name))
  end

  attribute_columns + actions
end

#controllerObject

This must be defined for record_path to work



62
63
64
# File 'lib/action_table/view.rb', line 62

def controller
  table_name
end

#headersObject



26
27
28
# File 'lib/action_table/view.rb', line 26

def headers
  @headers ||= @columns.map { |name| t_col(name) }
end

#paginate?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/action_table/view.rb', line 53

def paginate?
  @paginate
end

#paginate_param_nameObject



57
58
59
# File 'lib/action_table/view.rb', line 57

def paginate_param_name
  "#{model_name}_page"
end

#record_path(record, action: nil) ⇒ Object

Contstruct path to record, i.e programs_path(record)



67
68
69
70
71
# File 'lib/action_table/view.rb', line 67

def record_path(record, action: nil)
  action = nil if action.to_s == 'show'
  path = [action, model_name, 'path'].compact.join('_')
  public_send(path, record)
end