lib/tableficate/table.rb in tableficate-0.2.1 vs lib/tableficate/table.rb in tableficate-0.3.0
- old
+ new
@@ -1,36 +1,49 @@
module Tableficate
class Table
- attr_reader :columns, :rows, :current_sort, :filters, :options, :as, :template
+ attr_reader :columns, :rows, :current_sort, :filters, :attrs, :as, :template, :theme
def initialize(template, rows, options, data)
@template = template
@rows = rows
- @columns = []
- @filters = []
- @as = options[:as] || rows.table_name
- @options = {
- show_sorts: false,
- theme: ''
- }.merge(options)
+ @as = options.delete(:as) || rows.table_name
+ @theme = options.delete(:theme) || ''
+ @show_sorts = options.delete(:show_sorts) || false
+ @attrs = options
+ @columns = []
+ @filters = []
+
@current_sort = data[:current_sort]
+ @field_map = data[:field_map] || {}
end
- def column(name, options = {}, &block)
- options.reverse_merge!(
- show_sort: @options[:show_sorts]
- )
+ def empty(*args, &block)
+ if args.empty? and not block_given?
+ @empty
+ else
+ @empty = Empty.new(self, *args, &block)
+ end
+ end
- @columns.push(Column.new(self, name, options, &block))
+ def caption(*args, &block)
+ if args.empty? and not block_given?
+ @caption
+ else
+ @caption = Caption.new(*args, &block)
+ end
end
- def actions(&block)
- @columns.push(ActionColumn.new(self, &block))
+ def column(name, options = {}, &block)
+ @columns.push(Column.new(self, name, options.reverse_merge(show_sort: @show_sorts), &block))
end
+ def actions(options = {}, &block)
+ @columns.push(ActionColumn.new(self, options, &block))
+ end
+
def show_sort?
self.columns.any?{|column| column.show_sort?}
end
def filter(name, options = {})
@@ -52,11 +65,11 @@
select: Filter::Select,
radio: Filter::Radio,
checkbox: Filter::CheckBox
}
- as = options.delete(:as) || (options[:collection] ? :select : :text)
+ as = options.delete(:as) || find_as(name, options.has_key?(:collection))
raise Filter::UnknownInputType if as_map[as].nil?
options[:type] = as.to_s
@@ -80,24 +93,51 @@
search: Filter::InputRange,
color: Filter::InputRange,
select: Filter::SelectRange
}
- as = options.delete(:as) || (options[:collection] ? :select : :text)
+ as = options.delete(:as) || find_as(name, options.has_key?(:collection))
raise Filter::UnknownInputType if as_map[as].nil?
options[:type] = as.to_s
@filters.push(as_map[as].new(self, name, options))
end
- def render(options = {})
- options.reverse_merge!(
- partial: Tableficate::Utils::template_path('table', @options[:theme]),
- locals: {table: self}
- )
+ def find_as(name, has_collection)
+ field_name = (@field_map[name] || name).to_s
+ as = :text
- @template.render options
+ if has_collection
+ as = :select
+ else
+ case Tableficate::Utils::find_column_type(@rows, field_name)
+ when :integer, :float, :decimal
+ as = :number
+ when :date
+ as = :date
+ when :time
+ as = :time
+ when :datetime, :timestamp
+ as = :datetime
+ when :boolean
+ as = :checkbox
+ end
+ end
+
+ if as == :text
+ case name
+ when /email/
+ as = :email
+ when /url/
+ as = :url
+ when /phone/
+ as = :tel
+ end
+ end
+
+ as
end
+ private :find_as
end
end