Skip to content

Commit 1eb699d

Browse files
committed
Merge pull request activeadmin#1626 from forthemakers/1625-pass-options-to-csv
Active Admin developer can pass options for CSV generation
2 parents fa6cfe0 + 9185d12 commit 1eb699d

File tree

10 files changed

+100
-16
lines changed

10 files changed

+100
-16
lines changed

app/views/active_admin/resource/index.csv.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
CSV
88
end
99

10-
csv_output = csv_lib.generate(:col_sep => active_admin_config.csv_builder.column_separator || active_admin_application.csv_column_separator) do |csv|
10+
col_sep = active_admin_config.csv_builder.column_separator || active_admin_application.csv_column_separator
11+
options = (active_admin_config.csv_builder.options || active_admin_application.csv_options).merge(:col_sep => col_sep)
12+
13+
csv_output = csv_lib.generate(options) do |csv|
1114
columns = active_admin_config.csv_builder.columns
1215
csv << columns.map(&:name)
1316
collection.each do |resource|

docs/4-csv-format.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ Customizing the CSV format is as simple as customizing the index page.
1313
end
1414
end
1515

16-
You can choose custom separator:
16+
You can set custom csv options:
1717

1818
ActiveAdmin.register Post do
19-
csv :separator => ';' do
19+
csv :options => { :force_quotes => true } do
2020
column :title
2121
column("Author") { |post| post.author.full_name }
2222
end
2323
end
2424

25-
You can also set the custom separator system-wide:
25+
You can set options for the CSV format system-wide:
2626

2727
# config/initializers/active_admin.rb
28-
config.csv_column_separator = ";"
28+
# Set the CSV builder separator (default is ",")
29+
config.csv_column_separator = ';'
30+
31+
# Set the CSV builder options (default is {})
32+
config.csv_options = { :force_quotes => true }

features/index/format_as_csv.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,24 @@ Feature: Format as CSV
6161
| Title | Body |
6262
| Hello, World | (.*) |
6363

64+
Scenario: With CSV option customization
65+
Given a configuration of:
66+
"""
67+
ActiveAdmin.register Post do
68+
csv :options => {:force_quotes => true} do
69+
column :title
70+
column :body
71+
end
72+
end
73+
"""
74+
And a post with the title "012345" exists
75+
When I am on the index page for posts
76+
And I follow "CSV"
77+
And I should download a CSV file with "," separator for "posts" containing:
78+
| Title | Body |
79+
| 012345 | (.*) |
80+
And the CSV file should contain "012345" in quotes
81+
6482
Scenario: With default CSV separator option
6583
Given a configuration of:
6684
"""
@@ -79,3 +97,22 @@ Feature: Format as CSV
7997
| Title | Body |
8098
| Hello, World | (.*) |
8199

100+
Scenario: With default CSV options
101+
Given a configuration of:
102+
"""
103+
ActiveAdmin.application.csv_options = {:force_quotes => true}
104+
ActiveAdmin.application.csv_column_separator = ','
105+
ActiveAdmin.register Post do
106+
csv do
107+
column :title
108+
column :body
109+
end
110+
end
111+
"""
112+
And a post with the title "012345" exists
113+
When I am on the index page for posts
114+
And I follow "CSV"
115+
And I should download a CSV file with "," separator for "posts" containing:
116+
| Title | Body |
117+
| 012345 | (.*) |
118+
And the CSV file should contain "012345" in quotes

features/step_definitions/format_steps.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,9 @@
4848

4949
Then /^I should download a CSV file for "([^"]*)" containing:$/ do |resource_name, table|
5050
step "I should download a CSV file with \",\" separator for \"#{resource_name}\" containing:", table
51-
end
51+
end
52+
53+
Then /^the CSV file should contain "([^"]*)" in quotes$/ do |text|
54+
body = page.driver.response.body
55+
body.should match(/\"#{text}\"/)
56+
end

lib/active_admin/application.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ def self.inheritable_setting(name, default)
1212
setting name, default
1313
end
1414

15+
def self.deprecated_inheritable_setting(name, default)
16+
Namespace.deprecated_setting name, nil
17+
deprecated_setting name, default
18+
end
19+
1520
# The default namespace to put controllers and routes inside. Set this
1621
# in config/initializers/active_admin.rb using:
1722
#
@@ -66,8 +71,8 @@ def self.inheritable_setting(name, default)
6671
# The namespace root.
6772
inheritable_setting :root_to, 'dashboard#index'
6873

69-
# Default CSV separator
70-
inheritable_setting :csv_column_separator, ','
74+
# Default CSV options
75+
inheritable_setting :csv_options, {}
7176

7277
# Active Admin makes educated guesses when displaying objects, this is
7378
# the list of methods it tries calling in order
@@ -82,6 +87,9 @@ def self.inheritable_setting(name, default)
8287

8388
# == Deprecated Settings
8489

90+
# @deprecated Default CSV separator will be removed in 0.6.0. Use `csv_options = { :col_sep => ',' }` instead.
91+
deprecated_inheritable_setting :csv_column_separator, ','
92+
8593
# @deprecated The default sort order for index pages
8694
deprecated_setting :default_sort_order, 'id_desc'
8795

lib/active_admin/csv_builder.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ def self.default_for_resource(resource)
2525
end
2626
end
2727

28-
attr_reader :columns, :column_separator
28+
attr_reader :columns, :column_separator, :options
2929

3030
def initialize(options={}, &block)
3131
@columns = []
3232
@column_separator = options.delete(:separator)
33+
@options = options.delete(:options)
3334
instance_eval &block if block_given?
3435
end
3536

lib/active_admin/resource_dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def form(options = {}, &block)
6868
# column("Author") { |post| post.author.full_name }
6969
# end
7070
#
71-
# csv :separator => ";" do
71+
# csv :separator => ";", :options => { :force_quotes => true } do
7272
# column :name
7373
# end
7474
#

lib/generators/active_admin/install/templates/active_admin.rb.erb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ ActiveAdmin.setup do |config|
116116
# Enable and disable Batch Actions
117117
#
118118
config.batch_actions = true
119-
119+
120120

121121
# == Controller Filters
122122
#
@@ -134,7 +134,7 @@ ActiveAdmin.setup do |config|
134134
#
135135
# To load a stylesheet:
136136
# config.register_stylesheet 'my_stylesheet.css'
137-
137+
138138
# You can provide an options hash for more control, which is passed along to stylesheet_link_tag():
139139
# config.register_stylesheet 'my_print_stylesheet.css', :media => :print
140140
#
@@ -146,4 +146,7 @@ ActiveAdmin.setup do |config|
146146
#
147147
# Set the CSV builder separator (default is ",")
148148
# config.csv_column_separator = ','
149+
#
150+
# Set the CSV builder options (default is {})
151+
# config.csv_options = {}
149152
end

spec/unit/application_spec.rb

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
application.site_title = "New Title"
3131
application.site_title.should == "New Title"
3232
end
33-
33+
3434
it "should store the site's title link" do
3535
application.site_title_link.should == ""
3636
end
@@ -39,11 +39,11 @@
3939
application.site_title_link = "http://www.mygreatsite.com"
4040
application.site_title_link.should == "http://www.mygreatsite.com"
4141
end
42-
42+
4343
it "should store the site's title image" do
4444
application.site_title_image.should == ""
4545
end
46-
46+
4747
it "should set the site's title image" do
4848
application.site_title_image = "http://railscasts.com/assets/episodes/stills/284-active-admin.png?1316476106"
4949
application.site_title_image.should == "http://railscasts.com/assets/episodes/stills/284-active-admin.png?1316476106"
@@ -53,7 +53,7 @@
5353
application.view_factory.should be_an_instance_of(ActiveAdmin::ViewFactory)
5454
end
5555

56-
it "should have deprecated admin notes by default" do
56+
it "should have deprecated admin notes by default" do
5757
application.admin_notes.should be_nil
5858
end
5959

@@ -80,6 +80,19 @@
8080
end
8181
end
8282

83+
describe "inheritable settings" do
84+
it "should set csv_options" do
85+
application.csv_options.should == {}
86+
end
87+
88+
context "when deprecated" do
89+
it "should set and warn csv_column_separator" do
90+
ActiveAdmin::Deprecation.should_receive(:warn)
91+
application.csv_column_separator.should == ','
92+
end
93+
end
94+
end
95+
8396
describe "files in load path" do
8497
it "should load files in the first level directory" do
8598
application.files_in_load_path.should include(File.expand_path("app/admin/dashboard.rb", Rails.root))

spec/unit/csv_builder_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,14 @@
9090
end
9191
end
9292

93+
context "with csv_options" do
94+
let(:builder) do
95+
ActiveAdmin::CSVBuilder.new :options => {:force_quotes => true}
96+
end
97+
98+
it "should have proper separator" do
99+
builder.options.should == {:force_quotes => true}
100+
end
101+
end
102+
93103
end

0 commit comments

Comments
 (0)