Skip to content
Browse files

Initial commit for CSV configuration

  • Loading branch information...
1 parent 492b760 commit 880abc5cb1b74dcdf7418a458d62989a7816844b @gregbell gregbell committed with pcreux May 17, 2011
View
18 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
+
View
12 features/step_definitions/format_steps.rb
@@ -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
View
1 lib/active_admin.rb
@@ -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'
View
30 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
View
46 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.
Something went wrong with that request. Please try again.