lib/tabulatr/renderer/column.rb in tabulatr2-0.9.20 vs lib/tabulatr/renderer/column.rb in tabulatr2-0.9.21

- old
+ new

@@ -22,148 +22,48 @@ #++ class Tabulatr::Renderer::Column include ActiveModel::Model - attr_accessor *%i{name header width align valign wrap type th_html filter_html - filter_label filter filter_width range_filter_symbol - sortable table_name block klass format map classes cell_style header_style - sort_sql filter_sql output} + attr_accessor *%i{name klass table_name col_options output block} + delegate :filter, to: :col_options + def self.from( name: nil, table_name: nil, - header: nil, - classes: nil, - width: false, - align: false, - valign: false, - wrap: nil, - th_html: false, - filter_html: false, - filter_label: nil, - filter: true, - sortable: true, - format: nil, - map: true, + col_options: nil, klass: nil, - cell_style: {}, - header_style: {}, - sort_sql: nil, - filter_sql: nil, output: nil, &block) self.new( name: name, table_name: table_name, - header: header, - classes: classes, - width: width, - align: align, - valign: valign, - wrap: wrap, - th_html: th_html, - filter_html: filter_html, - filter_label: filter_label, - filter: filter, - sortable: sortable, - format: format, - map: map, + col_options: col_options, klass: klass, - block: block, - cell_style: cell_style, - header_style: header_style, - sort_sql: sort_sql, - filter_sql: filter_sql, - output: output - ).apply_styles! + output: output, + block: block + ) end - def update_options(hash = {}, &block) - self.header = hash[:header] || self.header - self.classes = hash[:classes] || self.classes - self.width = hash[:width] || self.width - self.align = hash[:align] || self.align - self.valign = hash[:valign] || self.valign - self.wrap = hash[:wrap] || self.wrap - self.th_html = hash[:th_html] || self.th_html - self.filter_html = hash[:filter_html] || self.filter_html - self.filter_label = hash[:filter_label] || self.filter_label - self.filter = hash[:filter] || self.filter - self.sortable = hash[:sortable] || self.sortable - self.format = hash[:format] || self.format - self.map = hash[:map] || self.map - self.th_html = hash[:th_html] || self.th_html - self.output = block if block_given? - self.filter_sql = hash[:filter_sql] || self.filter_sql - self.sort_sql = hash[:sort_sql] || self.sort_sql - if self.cell_style == '' - self.cell_style = {} - end - self.cell_style = hash[:cell_style] || self.cell_style - if self.header_style == '' - self.header_style = {} - end - self.header_style = hash[:header_style] || self.header_style - self.apply_styles! - end - def klassname() @_klassname ||= @klass.name.underscore end - def human_name() header || klass.human_attribute_name(name) end + def human_name() col_options.header || klass.human_attribute_name(name) end def sort_param() "#{klassname}_sort" end def full_name() [table_name, name].compact.join(":") end def coltype() 'column' end def column?() true end def association?() false end def checkbox?() false end def action?() false end - def apply_styles! - # raise cell_style.inspect - self.cell_style = style_options.merge(self.cell_style) - self.header_style = style_options.merge(self.header_style) - self - end - - def html_cell_style - cell_style.map{|e| e.join(':')}.join(';') - end - - def html_header_style - header_style.map{|e| e.join(':')}.join(';') - end - - def style_options - default_style_attributes = { - :'text-align' => align, - width: width, - :'vertical-align' => valign, - :'white-space' => wrap - }.select{|k,v| v} - - default_style_attributes || {} - end - def value_for(record, view) val = principal_value(record, view) - if format.present? && val.respond_to?(:to_ary) - val.map do |v| - case format - when Symbol then view.send(format, v) - when String then format % v - when Proc then format.(v) - else val - end + if self.col_options.format.present? + Array(val).map do |v| + format_value(v) end - elsif format.present? - case format - when Symbol then view.send(format, val) - when String then format % val - when Proc then format.(val) - else val - end else val end end @@ -178,24 +78,39 @@ nil end end def determine_appropriate_filter! - typ = self.klass.columns_hash[self.name.to_s].try(:type).try(:to_sym) + typ = self.klass.columns_hash[self.name.to_s].type.to_sym rescue nil case typ - when :integer - if self.klass.respond_to?(:defined_enums) && self.klass.defined_enums.keys.include?(self.name.to_s) - self.filter = :enum - else - self.filter = :integer - end - when :enum then self.filter = :enum - when :float, :decimal then self.filter = :decimal - when :string, :text then self.filter = :like - when :date, :time, :datetime, :timestamp then self.filter = :date - when :boolean then self.filter = :checkbox - when nil then self.filter = :exact + when :integer then self.col_options.filter = filter_type_for_integer + when :enum then self.col_options.filter = :enum + when :float, :decimal then self.col_options.filter = :decimal + when :string, :text then self.col_options.filter = :like + when :date, :time, :datetime, :timestamp then self.col_options.filter = :date + when :boolean then self.col_options.filter = :checkbox + when nil then self.col_options.filter = :exact else raise "Unknown filter type for #{self.name}: »#{typ}«" + end + end + + + private + + def filter_type_for_integer + if self.klass.respond_to?(:defined_enums) && self.klass.defined_enums.keys.include?(self.name.to_s) + :enum + else + :integer + end + end + + def format_value(value) + case self.col_options.format + when Symbol then view.send(col_options.format, value) + when String then col_options.format % value + when Proc then col_options.format.(value) + else value end end end