lib/hyper_kitten_tables/concerns/table.rb in hyper-kitten-tables-0.1.1.alpha1.01 vs lib/hyper_kitten_tables/concerns/table.rb in hyper-kitten-tables-0.1.1.alpha1.02
- old
+ new
@@ -9,15 +9,16 @@
included do
extend Forwardable
def_delegators :@view_context, :content_tag, :capture, :request
- const_set :Column, Struct.new(:name, :method_name, :sort_key, :block, :sortable, :options)
+ const_set :Column, Struct.new(:name, :method_name, :sort_key, :block, :sortable, :options, :header_options)
end
- def initialize(collection: [], requested_columns: [], &block)
+ def initialize(collection: [], requested_columns: [], sortable_column_default: false, &block)
@collection = collection
+ @sortable_column_default = sortable_column_default
@columns = []
@table_options = {}
@thead_options = {}
@tbody_options = {}
@@ -26,17 +27,17 @@
@requested_columns = requested_columns
yield self if block_given?
end
- def td(name, method_name: nil, sort_key: nil, sortable: true, **options, &block)
+ def td(name, method_name: nil, sort_key: nil, sortable: @sortable_column_default, header_options: {}, **options, &block)
if method_name.nil?
method_name = name.to_s.parameterize.underscore
name = name.to_s.titleize unless block_given?
end
sort_key = method_name if sort_key.blank?
- @columns << self.class::Column.new(name, method_name, sort_key, block, sortable, options)
+ @columns << self.class::Column.new(name, method_name, sort_key, block, sortable, options, header_options)
end
def define_header_sort_url(&block)
define_singleton_method(:header_sort_url, &block)
end
@@ -91,15 +92,16 @@
def render_header
content_tag(:thead, @thead_options) do
content_tag(:tr, @tr_options) do
visible_columns.map do |column|
+ header_options = @th_options.merge(column.header_options)
if column.sortable
- content_tag(:th, @th_options) do
+ content_tag(:th, header_options) do
header_sort_url(column, query_params).html_safe
end
else
- content_tag(:th, column.name, @th_options)
+ content_tag(:th, column.name, header_options)
end
end.join.html_safe
end
end
end