Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

column titles internalization similar to current activeadmin behaviour #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions lib/active_admin/axlsx/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down