Class: Munger::Render::SortableHtml

Inherits:
Object
  • Object
show all
Defined in:
lib/munger/render/sortable_html.rb

Overview

Render a table that lets the user sort the columns

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report, options = {}) ⇒ SortableHtml

options: :url => /some/url/for/link # link to put on column :params => :url_params # parameters from url if any :sort => ‘column’ # column that is currently sorted :order => ‘asc’ || ‘desc’ # order of the currently sorted field



14
15
16
17
18
19
20
21
# File 'lib/munger/render/sortable_html.rb', line 14

def initialize(report, options = {})
  @report = report
  @options = options
  # default url and params options
  @options[:url] ||= '/'
  @options[:params] ||= {}
  set_classes(options[:classes])
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



7
8
9
# File 'lib/munger/render/sortable_html.rb', line 7

def classes
  @classes
end

#reportObject (readonly)

Returns the value of attribute report.



7
8
9
# File 'lib/munger/render/sortable_html.rb', line 7

def report
  @report
end

Instance Method Details

#cycle(one, two) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/munger/render/sortable_html.rb', line 125

def cycle(one, two)
  if @current == one
    @current = two
  else
    @current = one
  end
end

#renderObject



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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/munger/render/sortable_html.rb', line 29

def render
  x = Builder::XmlMarkup.new
  
  x.table(:class => @classes[:table]) do
    
    x.thead do
      x.tr do
        @report.columns.each do |column|
          # TODO: Should be able to see if a column is 'sortable'
          # Assume all columns are sortable here - for now.
          state = 'unsorted'
          order = 'desc'
          direction_class = ""

          if [column.to_s, @report.column_data_field(column)].include?(@options[:sort])
            state = "sorted"
            order = @options[:order] || 'asc'
            direction_class = "sorted-#{order}"
          end

          new_params = @options[:params].merge({'sort' => @report.column_data_field(column),'order' => (order == 'asc' ? 'desc' : 'asc')})
          x.th(:class => "columnTitle #{state} #{direction_class}") do 
             # x << @report.column_title(column) 
             x << "<a href=\"#{@options[:url]}?#{create_querystring(new_params)}\">#{@report.column_title(column)}</a>"
           end
        end
      end
    end # x.thead
    
    x.tbody do
      @report.process_data.each do |row|
      
        classes = []
        classes << row[:meta][:row_styles]
        classes << 'group' + row[:meta][:group].to_s if row[:meta][:group]
        classes << cycle('even', 'odd')
        classes.compact!

        if row[:meta][:group_header]
          classes << 'groupHeader' + row[:meta][:group_header].to_s 
        end
      
        row_attrib = {}
        row_attrib = {:class => classes.join(' ')} if classes.size > 0
      
        if row[:meta][:group_header]
          x.thead do
            x.tr(row_attrib) do
              header = @report.column_title(row[:meta][:group_name]) + ' : ' + row[:meta][:group_value].to_s
              x.th(:colspan => @report.columns.size) { x << header }
            end
          end
        else 
          x.tr(row_attrib) do
            @report.columns.each do |column|
        
              cell_attrib = {}
              if cst = row[:meta][:cell_styles]
                cst = Item.ensure(cst)
                if cell_styles = cst[column]
                  cell_attrib = {:class => cell_styles.join(' ')}
                end
              end
              # TODO: Clean this up, I don't like it but it's working
              # output the cell
              # x.td(cell_attrib) { x << row[:data][column].to_s }
              x.td(cell_attrib) do
                formatter,*args = *@report.column_formatter(column)
                col_data = row[:data] #[column]
                if formatter && col_data[column]
                  formatted = if formatter.class == Proc
                    data = col_data.respond_to?(:data) ? col_data.data : col_data
                    formatter.call(data)
                  elsif col_data[column].respond_to? formatter
                    col_data[column].send(formatter, *args)
                  elsif
                    col_data[column].to_s
                  end
                else
                  formatted = col_data[column].to_s
                end
                x << formatted.to_s
              end

            end # columns
          end # x.tr
        end
        
      end # rows
      
    end # x.tbody
    
  end # x.table
  
end

#set_classes(options = nil) ⇒ Object



23
24
25
26
27
# File 'lib/munger/render/sortable_html.rb', line 23

def set_classes(options = nil)
  options = {} if !options
  default = {:table => 'report-table'}
  @classes = default.merge(options)
end

#valid?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/munger/render/sortable_html.rb', line 133

def valid?
  @report.is_a? Munger::Report
end