lib/trestle/table/column.rb in trestle-0.8.3 vs lib/trestle/table/column.rb in trestle-0.8.4
- old
+ new
@@ -26,32 +26,39 @@
def sort_options
options[:sort].is_a?(Hash) ? options[:sort] : {}
end
+ def header
+ if options[:header]
+ options[:header]
+ elsif admin = table.options[:admin]
+ admin.human_attribute_name(field)
+ else
+ field.to_s.humanize.titleize
+ end
+ end
+
class Renderer
delegate :options, to: :@column
def initialize(column, template)
@column, @template = column, template
end
def header
return if options.has_key?(:header) && options[:header].in?([nil, false])
- header = I18n.t("admin.table.headers.#{@column.field}", default: options[:header] || @column.field.to_s.humanize.titleize)
+ header = I18n.t("admin.table.headers.#{@column.field}", default: @column.header)
header = @template.sort_link(header, @column.sort_field, @column.sort_options) if @column.sortable?
header
end
def content(instance)
value = column_value(instance)
+ content = @template.format_value(value, options)
- return blank_column(instance) if value.nil?
-
- content = format_column(value)
-
if value.respond_to?(:id) && options[:link] != false
# 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
@@ -70,49 +77,23 @@
end
private
def column_value(instance)
if @column.block
- @template.capture { @template.instance_exec(instance, &@column.block).to_s }
+ 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,
+ # so that the Haml version of the capture method is used. Because we
+ # evaluate the block using instance_exec, we need to set this up manually.
+ -> {
+ _hamlout = eval('_hamlout', @column.block.binding)
+ @template.capture { @template.instance_exec(instance, &@column.block).to_s }
+ }.call
+ else
+ @template.capture { @template.instance_exec(instance, &@column.block).to_s }
+ end
else
instance.send(@column.field)
- end
- end
-
- def blank_column(value)
- text = options.key?(:blank) ? options[:blank] : I18n.t("admin.table.column.blank")
- @template.content_tag(:span, text, class: "blank")
- end
-
- def format_column(value)
- if options.key?(:format)
- format_from_options(value)
- else
- autoformat_value(value)
- end
- end
-
- def format_from_options(value)
- case options[:format]
- when :currency
- @template.number_to_currency(value)
- else
- value
- end
- end
-
- def autoformat_value(value)
- case value
- when Time, DateTime
- @template.timestamp(value)
- when Date
- @template.datestamp(value)
- when TrueClass, FalseClass
- @template.status_tag(@template.icon("fa fa-check"), :success) if value
- when ->(value) { value.respond_to?(:id) }
- @template.display(value)
- else
- value
end
end
end
end
end