From 82b4e2b44a80d56a9fde3de57e243dee7cfb6aff Mon Sep 17 00:00:00 2001 From: Andrey Sidorov Date: Wed, 22 Jan 2014 13:54:00 +0400 Subject: [PATCH] column titles internalization similar to current activeadmin behaviour --- lib/active_admin/axlsx/builder.rb | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/active_admin/axlsx/builder.rb b/lib/active_admin/axlsx/builder.rb index 6fb28ad..1e76ea5 100644 --- a/lib/active_admin/axlsx/builder.rb +++ b/lib/active_admin/axlsx/builder.rb @@ -42,7 +42,8 @@ class Builder # @see ActiveAdmin::Axlsx::DSL def initialize(resource_class, options={}, &block) @skip_header = false - @columns = resource_columns(resource_class) + @resource_class = resource_class + @columns = resource_columns(@resource_class) parse_options options instance_eval &block if block_given? end @@ -117,7 +118,7 @@ def whitelist # @param [Proc] block A block of code that is executed on the resource # when generating row data for this column. def column(name, &block) - @columns << Column.new(name, block) + @columns << Column.new(name, @resource_class, block) end # removes columns by name @@ -140,16 +141,20 @@ def serialize(collection) class Column - def initialize(name, block = nil) - @name = name.to_sym + def initialize(name, resource_class = nil, block = nil) + @name = name + @resource_class = resource_class @data = block || @name end attr_reader :name, :data def localized_name(i18n_scope = nil) - return name.to_s.titleize unless i18n_scope - I18n.t name, scope: i18n_scope + if i18n_scope + I18n.t name, scope: i18n_scope + else + name.is_a?(Symbol) && @resource_class.present? ? @resource_class.human_attribute_name(name) : name.to_s.humanize + end end end @@ -219,8 +224,8 @@ def header_style_id end def resource_columns(resource) - [Column.new(:id)] + resource.content_columns.map do |column| - Column.new(column.name.to_sym) + [Column.new(:id, @resource_class)] + resource.content_columns.map do |column| + Column.new(column.name.to_sym, @resource_class) end end end