Skip to content

Commit

Permalink
Added custom fields to XLS export of accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Ullmann committed Aug 6, 2012
1 parent b26184c commit c1253af
Showing 1 changed file with 60 additions and 13 deletions.
73 changes: 60 additions & 13 deletions app/views/accounts/index.xls.builder
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,76 @@ xml.Worksheet 'ss:Name' => I18n.t(:tab_accounts) do
unless @accounts.empty?
# Header.
xml.Row do
columns = %w{user assigned_to name email phone fax website background_info access phone_toll_free rating category date_created date_updated
street1 street2 city state zipcode country address}
heads = [I18n.t('user'),
I18n.t('assigned_to'),
I18n.t('name'),
I18n.t('email'),
I18n.t('phone'),
I18n.t('fax'),
I18n.t('website'),
I18n.t('background_info'),
I18n.t('access'),
I18n.t('phone_toll_free'),
I18n.t('rating'),
I18n.t('category'),
I18n.t('date_created'),
I18n.t('date_updated'),
I18n.t('street1'),
I18n.t('street2'),
I18n.t('city'),
I18n.t('state'),
I18n.t('zipcode'),
I18n.t('country'),
I18n.t('address')]

for column in columns
# Append custom field labels to header
Account.fields.each do |field|
heads << field.label
end

heads.each do |head|
xml.Cell do
xml.Data I18n.t(column), 'ss:Type' => 'String'
xml.Data head,
'ss:Type' => 'String'
end
end
end

# Contact rows.
for a in @accounts
# Account rows.
@accounts.each do |account|
xml.Row do
ba = a.billing_address
values = [a.user.try(:name), a.assignee.try(:name), a.name, a.email, a.phone, a.fax, a.website, a.background_info, a.access, a.toll_free_phone, a.rating, a.category, a.created_at, a.updated_at]

unless ba.nil?
values.concat [ba.street1, ba.street2, ba.city, ba.state, ba.zipcode, ba.country, ba.full_address]
address = account.billing_address
data = [account.user.try(:name),
account.assignee.try(:name),
account.name,
account.email,
account.phone,
account.fax,
account.website,
account.background_info,
account.access,
account.toll_free_phone,
account.rating,
account.category,
account.created_at,
account.updated_at,
address.try(:street1),
address.try(:street2),
address.try(:city),
address.try(:state),
address.try(:zipcode),
address.try(:country),
address.try(:full_address)]

# Append custom field values.
Account.fields.each do |field|
data << account.send(field.name)
end

for value in values
data.each do |value|
xml.Cell do
xml.Data value, 'ss:Type' => "#{value.respond_to?(:abs) ? 'Number' : 'String'}"
xml.Data value,
'ss:Type' => 'String'
end
end
end
Expand Down

0 comments on commit c1253af

Please sign in to comment.