Skip to content

Commit

Permalink
Active Admin developer can pass options to the CSV generation\
Browse files Browse the repository at this point in the history
For example, in active admin config: `config.csv_options = { :force_quotes => true }`
or in the active admin resource definition: `csv :options => { :force_quotes => true} do . . .`
  • Loading branch information
rheaton committed Dec 3, 2012
1 parent fa6cfe0 commit 5447ee1
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 6 deletions.
5 changes: 4 additions & 1 deletion app/views/active_admin/resource/index.csv.erb
Expand Up @@ -7,7 +7,10 @@
CSV
end

csv_output = csv_lib.generate(:col_sep => active_admin_config.csv_builder.column_separator || active_admin_application.csv_column_separator) do |csv|
col_sep = active_admin_config.csv_builder.column_separator || active_admin_application.csv_column_separator
options = (active_admin_config.csv_builder.options || active_admin_application.csv_options).merge(:col_sep => col_sep)

csv_output = csv_lib.generate(options) do |csv|
columns = active_admin_config.csv_builder.columns
csv << columns.map(&:name)
collection.each do |resource|
Expand Down
37 changes: 37 additions & 0 deletions features/index/format_as_csv.feature
Expand Up @@ -61,6 +61,24 @@ Feature: Format as CSV
| Title | Body |
| Hello, World | (.*) |

Scenario: With CSV option customization
Given a configuration of:
"""
ActiveAdmin.register Post do
csv :options => {:force_quotes => true} do
column :title
column :body
end
end
"""
And a post with the title "012345" exists
When I am on the index page for posts
And I follow "CSV"
And I should download a CSV file with "," separator for "posts" containing:
| Title | Body |
| 012345 | (.*) |
And the CSV file should contain "012345" in quotes

Scenario: With default CSV separator option
Given a configuration of:
"""
Expand All @@ -79,3 +97,22 @@ Feature: Format as CSV
| Title | Body |
| Hello, World | (.*) |

Scenario: With default CSV options
Given a configuration of:
"""
ActiveAdmin.application.csv_options = {:force_quotes => true}
ActiveAdmin.application.csv_column_separator = ','
ActiveAdmin.register Post do
csv do
column :title
column :body
end
end
"""
And a post with the title "012345" exists
When I am on the index page for posts
And I follow "CSV"
And I should download a CSV file with "," separator for "posts" containing:
| Title | Body |
| 012345 | (.*) |
And the CSV file should contain "012345" in quotes
7 changes: 6 additions & 1 deletion features/step_definitions/format_steps.rb
Expand Up @@ -48,4 +48,9 @@

Then /^I should download a CSV file for "([^"]*)" containing:$/ do |resource_name, table|
step "I should download a CSV file with \",\" separator for \"#{resource_name}\" containing:", table
end
end

Then /^the CSV file should contain "([^"]*)" in quotes$/ do |text|
body = page.driver.response.body
body.should match(/\"#{text}\"/)
end
3 changes: 3 additions & 0 deletions lib/active_admin/application.rb
Expand Up @@ -69,6 +69,9 @@ def self.inheritable_setting(name, default)
# Default CSV separator
inheritable_setting :csv_column_separator, ','

# Default CSV options
inheritable_setting :csv_options, {}

# Active Admin makes educated guesses when displaying objects, this is
# the list of methods it tries calling in order
setting :display_name_methods, [ :display_name,
Expand Down
3 changes: 2 additions & 1 deletion lib/active_admin/csv_builder.rb
Expand Up @@ -25,11 +25,12 @@ def self.default_for_resource(resource)
end
end

attr_reader :columns, :column_separator
attr_reader :columns, :column_separator, :options

def initialize(options={}, &block)
@columns = []
@column_separator = options.delete(:separator)
@options = options.delete(:options)
instance_eval &block if block_given?
end

Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/resource_dsl.rb
Expand Up @@ -68,7 +68,7 @@ def form(options = {}, &block)
# column("Author") { |post| post.author.full_name }
# end
#
# csv :separator => ";" do
# csv :separator => ";", :options => { :force_quotes => true } do
# column :name
# end
#
Expand Down
Expand Up @@ -116,7 +116,7 @@ ActiveAdmin.setup do |config|
# Enable and disable Batch Actions
#
config.batch_actions = true


# == Controller Filters
#
Expand All @@ -134,7 +134,7 @@ ActiveAdmin.setup do |config|
#
# To load a stylesheet:
# config.register_stylesheet 'my_stylesheet.css'

# You can provide an options hash for more control, which is passed along to stylesheet_link_tag():
# config.register_stylesheet 'my_print_stylesheet.css', :media => :print
#
Expand All @@ -146,4 +146,7 @@ ActiveAdmin.setup do |config|
#
# Set the CSV builder separator (default is ",")
# config.csv_column_separator = ','
#
# Set the CSV builder options (default is {})
# config.csv_options = {}
end
10 changes: 10 additions & 0 deletions spec/unit/csv_builder_spec.rb
Expand Up @@ -90,4 +90,14 @@
end
end

context "with csv_options" do
let(:builder) do
ActiveAdmin::CSVBuilder.new :options => {:force_quotes => true}
end

it "should have proper separator" do
builder.options.should == {:force_quotes => true}
end
end

end

0 comments on commit 5447ee1

Please sign in to comment.