Class: Spyro::ActionViewExtension::CollectionForHelper::Output::Table

Inherits:
Base
  • Object
show all
Includes:
Fields
Defined in:
lib/spyro/collections/outputs/table.rb

Direct Known Subclasses

AdminTable, FlatuiTable, InplaceTable

Instance Method Summary collapse

Methods included from Fields

#format_boolean, #format_classy_enum, #format_cw_file, #format_cw_image, #format_datetime, #format_default, #format_email, #format_float, #format_html, #format_period, #format_prct, #format_progress, #format_string, #format_text, #format_url

Methods inherited from Base

#initialize, #t

Constructor Details

This class inherits a constructor from Spyro::ActionViewExtension::CollectionForHelper::Output::Base

Instance Method Details

#custom_value_formatted(value, elem) ⇒ Object



70
71
72
# File 'lib/spyro/collections/outputs/table.rb', line 70

def custom_value_formatted value, elem
  value
end


30
31
32
# File 'lib/spyro/collections/outputs/table.rb', line 30

def default_destroy_link_attributes
  {:data => {:confirm => "Are you sure?", :method => "delete", :'confirm-title' => "Confirmation"}}
end

#format_value(value, elem) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/spyro/collections/outputs/table.rb', line 57

def format_value value, elem
  return self.send("format_#{elem.as}".to_sym, value, {}) if elem.as


  @@fields.each do |keys, values|
    klass, pattern = *keys
    if (Symbol === klass and klass == elem.db_type) or (Class === klass and value.is_a? klass)
      return self.send("format_#{values[:method]}".to_sym, value, {}) if elem.name.to_s.match pattern
    end
  end
  value
end

#headerObject



14
15
16
# File 'lib/spyro/collections/outputs/table.rb', line 14

def header
  @header ||= (@unicollection.meta[:header] || []) + (@unicollection.meta[:actions] ? ['Actions'] : [])
end

#renderObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/spyro/collections/outputs/table.rb', line 112

def render
  return "" if @unicollection.rows.empty?
  table_html_opts = {:class => ['table', self.class.to_s.demodulize.underscore]}.deep_merge(@unicollection.meta[:html] || {}) { |key, old, new| Array.wrap(old) + Array.wrap(new) }
   :table, table_html_opts do
    html = render_header.html_safe
    html << render_body
    html << render_footer
    html
  end
end

#render_bodyObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/spyro/collections/outputs/table.rb', line 74

def render_body
  content = @unicollection.rows.map do |row|
    html = row[:data].map do |elem|
      value_formatted = elem.value
      value_formatted = t(value_formatted) if elem.translate
      value_formatted = format_value value_formatted, elem
      value_formatted = link_to value_formatted, elem.link if elem.link
      value_formatted = custom_value_formatted value_formatted, elem
      elem.html ? (:td, value_formatted, elem.html) : "<td>#{value_formatted}</td>"
    end.join('')
    html += render_cell_buttons row if @unicollection.meta[:actions]
    (row[:meta][:opts] and row[:meta][:opts][:html]) ? (:tr, html.html_safe, row[:meta][:opts][:html]) : "<tr>#{html}</tr>"
  end.join('').html_safe

  if @unicollection.meta[:header]
     :tbody, @unicollection.meta[:html_body] do
      content
    end
  else
    content
  end
end

#render_cell_buttons(row) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/spyro/collections/outputs/table.rb', line 34

def render_cell_buttons row
  row[:meta][:actions] = {} if row[:meta][:actions].nil?

  html = if row[:meta][:actions].is_a? Hash
           (row[:meta][:actions] || []).map do |name, link|
             case name
               when :show
                 mini_button_success name, link
               when :edit
                 mini_button_info name, link
               when :destroy
                 mini_button_danger name, link, default_destroy_link_attributes
               else
                 mini_button name, link
             end
           end.join ''
         else
           row[:meta][:actions]
         end

  "<td><span class='btn-group'>#{html}</span></td>"
end


97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spyro/collections/outputs/table.rb', line 97

def render_footer
  return "" unless @unicollection.meta[:footer] and @unicollection.meta[:header]

  total = header.count

   :tfoot, @unicollection.meta[:html_footer] do
    content = @unicollection.meta[:footer].map do |elem|
      colspan = [(header.count / @unicollection.meta[:footer].count.to_f).ceil, total].min
      total -= colspan
      "<td colspan='#{colspan}'>#{elem}</td>"
    end.join('')
    "<tr>#{content}</tr>".html_safe
  end
end

#render_headerObject



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/spyro/collections/outputs/table.rb', line 18

def render_header
  return "" unless @unicollection.meta[:header]
   :thead, @unicollection.meta[:html_header] do
    html = header.map do |header_sym|
      header = t(header_sym)
      header = @unicollection.meta[:block_header].call header_sym, header if @unicollection.meta[:block_header]
      "<th>#{header}</th>"
    end.join('')
    "<tr>#{html}</tr>".html_safe
  end
end