forked from activeadmin/activeadmin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit for CSV configuration
- Loading branch information
Showing
5 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |