Module: QUI::IndexTable::MainHelper

Defined in:
lib/qui-index-table-main-helper.rb

Instance Method Summary collapse

Instance Method Details

#index_table(data, options = {}, &b) ⇒ Object

Raises:

  • (RuntimeError)


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/qui-index-table-main-helper.rb', line 4

def index_table(data, options = {}, &b)
  @index_table_count ||= 0
  raise RuntimeError, "@index_table_count must be integer" unless @index_table_count.is_a? Fixnum

  options[:headers] ||= []
  options[:id] ||= "index_table_#{@index_table_count}"

  concat pagination(data) if data.is_a? WillPaginate::Collection if defined?(WillPaginate) and defined?(pagination)

  concat "<table class=\"index\" id=\"#{options[:id]}\"><thead><tr>"

  options[:headers].each do |h|
    # Make a substitution for macros :oneicon and :check. Not too pretty but simple.
    h = { nil => { :width => :oneicon } } if h == :oneicon or h == :check
    
    if h.is_a? Symbol
      # If first character of column identifier is a dot, we assume it's attribute name.
      # Try to lookup i18n translation. 
      if not data.nil? and data.respond_to?(:first) and h.to_s[0] == 46
        # If there's no records, we would try to assume class name from controller name.
        # Of course if it's not overriden by :class_name option.
        
        if options[:class_name] and options[:class_name].respond_to? :ancestors and options[:class_name].ancestors.include? ActiveRecord::Base
          klass = options[:class_name]

        elsif data.first.nil?
          klass = controller.class.to_s.demodulize.gsub("Controller", "").singularize.constantize
        elsif data.first.class.respond_to? :human_attribute_name

          klass = data.first.class
        end
        
        if klass
          concat (:th, klass.human_attribute_name(h.to_s[1..-1]))
        else
          concat (:th, t(h))
        end
      else
        concat (:th, t(h))
      end

    elsif h.is_a? Hash
      name = h.keys.first
      if name
        name = t(name)
      else
        name = "&nbsp;"
      end
      
      if h.values.first[:width] == :oneicon
        concat (:th, name, :class => "oneicon")
      elsif h.values.first[:width] == :expand
        concat (:th, name)
      else
        concat (:th, name)
      end

    elsif h.is_a? String
      concat (:th, h)
    end
  end
  concat "</tr></thead><tbody>"

  options[:h1][:last_value] == nil if options[:h1]
  data.each do |row|
    if options[:h1]
      if options[:h1][:last_value] != row.send(options[:h1][:check])
        concat "<tr class=\"h1\">"
        concat options[:h1][:onchange].call(row)
        concat "</tr>"

        options[:h1][:last_value] = row.send(options[:h1][:check])
        reset_cycle(options[:id])
      end
    end
    
    concat "<tr class=\"#{cycle("even", "odd", :name => options[:id])}\">"
    b.call row
    concat "</tr>"
  end
  
  concat "</tbody></table>"

  concat pagination(data) if data.is_a? WillPaginate::Collection if defined?(WillPaginate) and defined?(pagination)
          
  @index_table_count += 1
end