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

Don't force humanize on column names. Provide an option instead #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
36 changes: 20 additions & 16 deletions lib/acts_as_xlsx/ar.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# -*- coding: utf-8 -*-
# Axlsx is a gem or generating excel spreadsheets with charts, images and many other features.
#
# Axlsx is a gem or generating excel spreadsheets with charts, images and many other features.
#
# acts_as_xlsx provides integration into active_record for Axlsx.
#
#
require 'axlsx'

# Adding to the Axlsx module
# Adding to the Axlsx module
# @see http://github.com/randym/axlsx
module Axlsx
# === Overview
# This module defines the acts_as_xlsx class method and provides to_xlsx support to both AR classes and instances
module Ar

def self.included(base) # :nodoc:
base.send :extend, ClassMethods
end

# Class methods for the mixin
module ClassMethods

Expand All @@ -26,8 +26,9 @@ module ClassMethods
# class MyModel < ActiveRecord::Base
# acts_as_xlsx :columns=> [:id, :created_at, :updated_at], :i18n => 'activerecord.attributes'
def acts_as_xlsx(options={})
cattr_accessor :xlsx_i18n, :xlsx_columns
cattr_accessor :xlsx_i18n, :xlsx_columns, :xlsx_humanize_column_names
self.xlsx_i18n = options.delete(:i18n) || false
self.xlsx_humanize_column_names = options.delete(:humanize_column_names) || false
self.xlsx_columns = options.delete(:columns)
extend Axlsx::Ar::SingletonMethods
end
Expand Down Expand Up @@ -61,28 +62,31 @@ def to_xlsx(options = {})
row_style = p.workbook.styles.add_style(row_style) unless row_style.nil?
header_style = p.workbook.styles.add_style(header_style) unless header_style.nil?
i18n = self.xlsx_i18n == true ? 'activerecord.attributes' : i18n
sheet_name = options.delete(:name) || (i18n ? I18n.t("#{i18n}.#{table_name.underscore}") : table_name.humanize)
humanize_column_names = (self.xlsx_humanize_column_names == true) || false
sheet_name = options.delete(:name) || (i18n ? I18n.t("#{i18n}.#{table_name.underscore}") : table_name.humanize)
data = options.delete(:data) || [*find(:all, options)]
data.compact!
data.flatten!

return p if data.empty?
p.workbook.add_worksheet(:name=>sheet_name) do |sheet|

col_labels = if i18n
columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") }
else
columns.map { |c| c.to_s.humanize }
end

columns.map { |c| I18n.t("#{i18n}.#{self.name.underscore}.#{c}") }
elsif humanize_column_names
columns.map { |c| c.to_s.humanize }
else
columns
end

sheet.add_row col_labels, :style=>header_style

data.each do |r|
row_data = columns.map do |c|
if c.to_s =~ /\./
v = r; c.to_s.split('.').each { |method| v = v.send(method) }; v
else
r.send(c)
r.send(c)
end
end
sheet.add_row row_data, :style=>row_style, :types=>types
Expand Down
10 changes: 2 additions & 8 deletions test/tc_acts_as_xlsx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def test_xlsx_options
assert_equal([:name, :title, :content, :votes, :ranking], Post.xlsx_columns)
assert_equal('activerecord.attributes', Post.xlsx_i18n)
end

end

class TestToXlsx < Test::Unit::TestCase
Expand All @@ -27,7 +26,7 @@ def test_to_xlsx_with_package

def test_to_xlsx_with_name
p = Post.to_xlsx :name=>'bob'
assert_equal(p.workbook.worksheets.first.name, 'bob')
assert_equal(p.workbook.worksheets.first.name, 'bob')
end

def test_xlsx_columns
Expand All @@ -39,14 +38,12 @@ def test_to_xslx_vanilla
assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value)
assert_equal(2,p.workbook.worksheets.first.rows.last.cells.first.value)
end



def test_to_xslx_with_provided_data
p = Post.to_xlsx :data => Post.where(:title => "This is the first post").all
assert_equal("Id",p.workbook.worksheets.first.rows.first.cells.first.value)
assert_equal(1,p.workbook.worksheets.first.rows.last.cells.first.value)
end


def test_columns
p = Post.to_xlsx :columns => [:name, :title, :content, :votes]
Expand All @@ -69,9 +66,6 @@ def test_chained_method
assert_equal("Name", sheet.rows.first.cells.first.value)
assert_equal(Post.last.comments.last.author.name, sheet.rows.last.cells.last.value)
end



end