lib/trestle/table/column.rb in trestle-0.9.2 vs lib/trestle/table/column.rb in trestle-0.9.3

- old
+ new

@@ -1,21 +1,21 @@ module Trestle class Table class Column - attr_reader :table, :field, :options, :block + attr_reader :field, :options, :block - def initialize(table, field, options={}, &block) - @table, @field, @options = table, field, options + def initialize(field, options={}, &block) + @field, @options = field, options @block = block if block_given? end - def renderer(template) - Renderer.new(self, template) + def renderer(table:, template:) + Renderer.new(self, table: table, template: template) end def sortable? - table.sortable? && options[:sort] != false && (!@block || options.has_key?(:sort)) + options[:sort] != false && (!@block || options.has_key?(:sort)) end def sort_field if options[:sort].is_a?(Hash) options[:sort][:field] || field @@ -26,25 +26,15 @@ def sort_options options[:sort].is_a?(Hash) ? options[:sort] : {} end - def header - if options[:header] - options[:header] - elsif table.admin - table.admin.t("table.headers.#{field}", default: table.admin.human_attribute_name(field)) - else - I18n.t("admin.table.headers.#{field}", default: field.to_s.humanize.titleize) - end - end - class Renderer - delegate :options, :table, to: :@column + delegate :options, to: :@column - def initialize(column, template) - @column, @template = column, template + def initialize(column, table:, template:) + @column, @table, @template = column, table, template end def render(instance) @template.content_tag(:td, content(instance), class: classes, data: data) end @@ -68,14 +58,15 @@ end def header return if options.key?(:header) && options[:header].in?([nil, false]) - header = @column.header - header = @template.instance_exec(&header) if header.respond_to?(:call) - header = @template.sort_link(header, @column.sort_field, @column.sort_options) if @column.sortable? - header + if @table.sortable? && @column.sortable? + @template.sort_link(header_text, @column.sort_field, @column.sort_options) + else + header_text + end end def content(instance) value = column_value(instance) content = @template.format_value(value, options) @@ -84,11 +75,11 @@ # Column value was a model instance (e.g. from an association). # Automatically link to instance's admin if available content = @template.admin_link_to(content, value) elsif options[:link] # Explicitly link to the specified admin, or the table's admin - content = @template.admin_link_to(content, instance, admin: options[:admin] || table.admin) + content = @template.admin_link_to(content, instance, admin: options[:admin] || @table.admin) end content end @@ -99,9 +90,23 @@ def data options[:data] end private + def header_text + if header = options[:header] + if header.respond_to?(:call) + @template.instance_exec(&header) + else + header + end + elsif @table.admin + @table.admin.t("table.headers.#{@column.field}", default: @table.admin.human_attribute_name(@column.field)) + else + I18n.t("admin.table.headers.#{@column.field}", default: @column.field.to_s.humanize.titleize) + end + end + def column_value(instance) if @column.block if defined?(Haml) && Haml::Helpers.block_is_haml?(@column.block) # In order for table column blocks to work properly within Haml templates, # the _hamlout local variable needs to be defined in the scope of the block,