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(cols:, records:, paginate: false, link: nil, actions: [], styles: ActionTable.config.table_styles) ⇒ View

Returns a new instance of View.



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

def initialize(
  cols:,
  records:,
  paginate: false,
  link: nil,
  actions: [],
  styles: ActionTable.config.table_styles
)
  @col_names  = cols.map(&:to_s)
  @rows       = records
  @table_name = records.table_name
  @model_name = @table_name.singularize
  @paginate   = paginate
  @link       = Set.new(Array(link.to_s)).reject(&:empty?)
  @actions    = Array(actions).map(&:to_s)
  @styles     = Array(styles)
end

Instance Attribute Details

#model_nameObject (readonly)

Returns the value of attribute model_name.



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

def model_name
  @model_name
end

#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



35
36
37
# File 'lib/action_table/view.rb', line 35

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

#cols(record) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/action_table/view.rb', line 39

def cols(record)
  attribute_columns = @col_names.map do |name|
    title = record.public_send(name)
    if title.present? && @link.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



70
71
72
# File 'lib/action_table/view.rb', line 70

def controller
  @table_name
end

#headersObject



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

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

#paginate?Boolean

Returns:

  • (Boolean)


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

def paginate?
  @paginate
end

#paginate_param_nameObject



65
66
67
# File 'lib/action_table/view.rb', line 65

def paginate_param_name
  "#{model_name}_page"
end

#record_path(record, action: nil) ⇒ Object

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



75
76
77
78
79
# File 'lib/action_table/view.rb', line 75

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

#styles_classObject



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

def styles_class
  @styles.map { |style| "table-#{style}" }.join(' ')
end

#t_action(name) ⇒ Object



85
86
87
88
# File 'lib/action_table/view.rb', line 85

def t_action(name)
  name = name.to_s
  I18n.t("actions.#{name}", default: name.titleize)
end

#t_actionsObject



81
82
83
# File 'lib/action_table/view.rb', line 81

def t_actions
  @actions.map { |name| t_action(name) }
end

#t_col(col_name) ⇒ Object



90
91
92
93
# File 'lib/action_table/view.rb', line 90

def t_col(col_name)
  t_key = "activerecord.attributes.#{@model_name}.#{col_name}"
  I18n.t(t_key, default: col_name.titleize)
end