Skip to content

Commit

Permalink
Initial commit for CSV configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gregbell authored and pcreux committed May 31, 2011
1 parent 492b760 commit 880abc5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
18 changes: 18 additions & 0 deletions features/index/format_as_csv.feature
@@ -0,0 +1,18 @@
Feature: Format as CSV

Scenario: Default with no index customization
Given an index configuration of:
"""
ActiveAdmin.register Post
"""
And a post with the title "Hello World" exists
When I am on the index page for posts
And I follow "CSV"
Then I should see the CSV:
| Id | Title | Body | Published At | Created At | Updated At |
| 1 | Hello World | | | (.*) | (.*) |

Scenario: With index customization

Scenario: With CSV format customization

12 changes: 12 additions & 0 deletions features/step_definitions/format_steps.rb
Expand Up @@ -5,3 +5,15 @@
Then /^I should see a link to download "([^"]*)"$/ do |format_type|
Then %{I should see "#{format_type}" within "#index_footer a"}
end

require 'csv'

Then /^I should see the CSV:$/ do |table|
puts page.body
csv = CSV.parse(page.body)
csv.each_with_index do |row, row_index|
row.each_with_index do |cell, col_index|
cell.should match(/#{table.raw[row_index][col_index]}/)
end
end
end
1 change: 1 addition & 0 deletions lib/active_admin.rb
Expand Up @@ -29,6 +29,7 @@ module ActiveAdmin
autoload :Scope, 'active_admin/scope'
autoload :Sidebar, 'active_admin/sidebar'
autoload :TableBuilder, 'active_admin/table_builder'
autoload :TableToCSV, 'active_admin/table_to_csv'
autoload :ViewFactory, 'active_admin/view_factory'
autoload :ViewHelpers, 'active_admin/view_helpers'
autoload :Views, 'active_admin/views'
Expand Down
30 changes: 30 additions & 0 deletions lib/active_admin/table_to_csv.rb
@@ -0,0 +1,30 @@
module ActiveAdmin

# Convert an Arbre::HTML::Table to CSV
class TableToCSV
include ActionView::Helpers::SanitizeHelper

def initialize(table)
@table = table
end

def to_s
CSV.generate do |csv|

thead = @table.find_by_tag("thead").first
csv << thead.find_by_tag("th").collect do |th|
th.content
end

tbody = @table.find_by_tag("tbody").first
tbody.find_by_tag("tr").each do |tr|
row = tr.find_by_tag("td").map do |td|
strip_tags(td.content)
end
csv << row
end
end
end
end

end
46 changes: 46 additions & 0 deletions spec/unit/table_to_csv_spec.rb
@@ -0,0 +1,46 @@
require 'spec_helper'

describe ActiveAdmin::TableToCSV do
include Arbre::HTML

let(:assigns){ {} }
let(:helpers){ action_view }

let(:post_1){ Post.new(:title => "Hello world")}
let(:post_2){ Post.new(:title => "Hello world 2")}

let(:csv){ ActiveAdmin::TableToCSV.new(table).to_s }


describe "a basic table" do
let :table do
table_for [post_1, post_2] do
column :title
column :body
end
end


it "should render the table headers" do
csv.split("\n").first.should == "Title,Body"
end

it "should render the first row" do
csv.split("\n")[1].should == %{Hello world,""}
end
end

describe "a table with html" do
let :table do
table_for [post_1, post_2] do
column(:title){|p| a p.title, :href => "/woot" }
column(:body){|p| para p.body }
end
end

it "content without html tags" do
csv.split("\n")[1].should == %{Hello world,""}
end
end

end

0 comments on commit 880abc5

Please sign in to comment.