Module: Sortablecolumns::Helpers
- Defined in:
- lib/sortablecolumns/helpers.rb
Instance Method Summary collapse
-
#get_col_heading_url(klass, sorter, col) ⇒ Object
Get URL for sortable table column header (TH element).
-
#get_url(order_by, dir) ⇒ Object
get url for given set of params, order_by, and dir.
-
#print_col(obj, sorter, col) ⇒ Object
Prints a single table column (TD element) with content.
-
#print_col_heading(klass, sorter, col, options = {}) ⇒ Object
Prints single TH tag with content.
-
#print_table(collection, sorter, options = {}) ⇒ Object
Prints entire HTML table with content.
-
#print_table_body(collection, sorter, options = {}) ⇒ Object
Prints table body with content.
-
#print_table_heading_row(klass, sorter, options = {}) ⇒ Object
Prints a row of TH tags with content.
-
#print_table_row(obj, sorter, options = {}) ⇒ Object
Prints single table row with content.
-
#print_table_thead(klass, sorter) ⇒ Object
Prints THEAD tags with content.
Instance Method Details
#get_col_heading_url(klass, sorter, col) ⇒ Object
Get URL for sortable table column header (TH element)
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/sortablecolumns/helpers.rb', line 143 def get_col_heading_url(klass, sorter, col) order_by = col default_dir = 'asc' sort_ops = klass.send("#{sorter}_sort_options", col) if sort_ops order_by = sort_ops['order_by'] if sort_ops['order_by'] default_dir = sort_ops['default_dir'] if sort_ops['default_dir'] end reg = Regexp.new(order_by.to_s) if params[:order_by] =~ reg dir = (params[:dir] == "asc") ? "desc" : "asc" else dir = default_dir end get_url(order_by, dir) end |
#get_url(order_by, dir) ⇒ Object
get url for given set of params, order_by, and dir
163 164 165 166 167 168 169 |
# File 'lib/sortablecolumns/helpers.rb', line 163 def get_url(order_by, dir) _params = params.dup _params[:order_by] = order_by _params[:dir] = dir _params.delete(:page) url_for(_params) end |
#print_col(obj, sorter, col) ⇒ Object
Prints a single table column (TD element) with content
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 91 92 93 94 95 96 |
# File 'lib/sortablecolumns/helpers.rb', line 13 def print_col(obj, sorter, col) col = col.to_s klass = obj.class txt = klass.send("#{sorter}_col_text",obj,col) wrappers = [] datatype = klass.send("#{sorter}_datatype",col) || 'string' if datatype == 'number' precision = klass.send("#{sorter}_precision",col) txt = number_with_precision(txt, :precision => precision) if precision end if datatype == 'currency' precision = klass.send("#{sorter}_precision",col) unit = klass.send("#{sorter}_unit",col) separator = klass.send("#{sorter}_separator",col) delimiter = klass.send("#{sorter}_delimiter",col) txt = number_to_currency(txt, :precision => precision, :unit => unit, :separator => separator, :delimiter => delimiter) end if datatype == 'datetime' or datatype == 'date' if txt.respond_to?(:strftime) format = klass.send("#{sorter}_date_format",col) txt = txt.strftime(format) end end do_link = klass.send("#{sorter}_link?",col) if do_link link_ops = klass.send("#{sorter}_link_options",col).dup raise "link_options must be defined for #{klass}:#{col}" unless link_ops if link_ops[:object_url] #link_to failed in the tests in Rails 2.2.2 url = send("#{klass.to_s.downcase}_url", obj) txt = link_to(txt, url) elsif link_ops[:controller] && link_ops[:action] if link_ops[:id] && link_ops[:id] == 'obj_id' link_ops[:id] = obj.id end if link_ops[:extras] extras = link_ops.delete(:extras) txt = link_to(txt, link_ops, extras) else if link_ops.size > 2 # cycle through link_ops, other than controller, action, and id, # and check if the option value is a method for this object. If not, # assign the value directly in the url. ops = link_ops.reject{|key,val| key == :controller || key == :action }# || key == :id} ops.each do |key, val| unless key == :id && val == 'obj_id' if val.is_a?(String) || val.is_a?(Symbol) link_ops[key] = obj.send(val) if val && obj.respond_to?(val.to_sym) end end end end txt = link_to(txt, link_ops) end elsif link_ops[:url] url = link_ops[:url].gsub(/:id/, obj.id.to_s) txt = link_to(txt, url) end end = klass.send("#{sorter}_print_options",col) wrappers << ['wrappers'] if && ['wrappers'] wrappers.flatten.each do |wrapper| if wrapper.is_a? Hash key = wrapper.keys.first extra_args = wrapper[key] txt = send(key, txt, extra_args) else txt = send(wrapper, txt) end end td_class = klass.send("#{sorter}_td_class",col) return content_tag("td", txt, :class => td_class) end |
#print_col_heading(klass, sorter, col, options = {}) ⇒ Object
Prints single TH tag with content
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/sortablecolumns/helpers.rb', line 129 def print_col_heading(klass, sorter, col, ={}) col = col.to_s th_txt = klass.send("#{sorter}_heading", col) th_class = klass.send("#{sorter}_th_class", col) return content_tag("th", '', :class => th_class) unless th_txt sortable = klass.send("#{sorter}_sortable?", col) return content_tag("th", th_txt, :class => th_class) unless sortable url = get_col_heading_url(klass, sorter, col) link = link_to(th_txt, url) #link = "<a href=\"#{url}\">#{th_txt}</a>" return content_tag("th", link, :class => th_class) end |
#print_table(collection, sorter, options = {}) ⇒ Object
Prints entire HTML table with content
5 6 7 8 9 10 |
# File 'lib/sortablecolumns/helpers.rb', line 5 def print_table(collection, sorter, ={}) klass = collection.first.class txt = print_table_thead(klass, sorter) txt << print_table_body(collection, sorter, ) return content_tag('table', txt, [:table]) end |
#print_table_body(collection, sorter, options = {}) ⇒ Object
Prints table body with content
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/sortablecolumns/helpers.rb', line 110 def print_table_body(collection, sorter, ={}) txt = "" if [:tr] && [:tr][:classes] alternate = true tr_classes = [:tr][:classes] end tr_class = nil i = 0 collection.each do |obj| if alternate tr_class = tr_classes[i % 2] end txt << print_table_row(obj, sorter, :class => tr_class) i += 1 end content_tag("tbody", txt.html_safe) end |
#print_table_heading_row(klass, sorter, options = {}) ⇒ Object
Prints a row of TH tags with content
172 173 174 175 176 177 178 179 |
# File 'lib/sortablecolumns/helpers.rb', line 172 def print_table_heading_row(klass, sorter, ={}) cols = klass.send("#{sorter}_col_keys_in_order") txt = '' cols.each do |col| txt << print_col_heading(klass, sorter, col) end content_tag("tr", txt.html_safe, ) end |
#print_table_row(obj, sorter, options = {}) ⇒ Object
Prints single table row with content
99 100 101 102 103 104 105 106 107 |
# File 'lib/sortablecolumns/helpers.rb', line 99 def print_table_row(obj, sorter, ={}) txt = '' klass = obj.class cols = klass.send("#{sorter}_col_keys_in_order") cols.each do |col| txt << print_col(obj, sorter, col) end content_tag("tr", txt.html_safe, ) end |
#print_table_thead(klass, sorter) ⇒ Object
Prints THEAD tags with content
182 183 184 |
# File 'lib/sortablecolumns/helpers.rb', line 182 def print_table_thead(klass, sorter) content_tag('thead', print_table_heading_row(klass, sorter)) end |