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.
- Loading branch information
Showing
18 changed files
with
239 additions
and
171 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
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
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,45 @@ | ||
module ActiveAdmin | ||
# CSVBuilder stores CSV configuration | ||
# | ||
# Usage example: | ||
# | ||
# csv_builder = CSVBuilder.new | ||
# csv_builder.column :id | ||
# csv_builder.column("Name") { |resource| resource.full_name } | ||
# | ||
class CSVBuilder | ||
|
||
# Return a default CSVBuilder for a resource | ||
# The CSVBuilder's columns would be Id followed by this | ||
# resource's content columns | ||
def self.default_for_resource(resource) | ||
new.tap do |csv_builder| | ||
csv_builder.column(:id) | ||
resource.content_columns.each do |content_column| | ||
csv_builder.column(content_column.name.to_sym) | ||
end | ||
end | ||
end | ||
|
||
attr_reader :columns | ||
|
||
def initialize(&block) | ||
@columns = [] | ||
instance_eval &block if block_given? | ||
end | ||
|
||
# Add a column | ||
def column(name, &block) | ||
@columns << Column.new(name, block) | ||
end | ||
|
||
class Column | ||
attr_reader :name, :data | ||
|
||
def initialize(name, block = nil) | ||
@name = name.is_a?(Symbol) ? name.to_s.titleize : name | ||
@data = block || name.to_sym | ||
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
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 was deleted.
Oops, something went wrong.
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
17 changes: 15 additions & 2 deletions
17
lib/active_admin/views/templates/active_admin/resource/index.csv.erb
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 |
---|---|---|
@@ -1,2 +1,15 @@ | ||
<%= @csv_columns.collect{|c| c.to_s.titleize }.join(",").html_safe %> | ||
<%= collection.collect{|resource| @csv_columns.collect{|col| "\"#{resource.send(col).to_s.gsub('"', '""')}\""}.join(",") }.join("\n").html_safe %> | ||
<%- | ||
# FasterCSV is CSV in ruby1.9 | ||
csv_lib = defined?(FasterCSV) ? FasterCSV : CSV | ||
|
||
csv_output = csv_lib.generate do |csv| | ||
columns = active_admin_config.csv_builder.columns | ||
csv << columns.map(&:name) | ||
collection.each do |resource| | ||
csv << columns.map do |column| | ||
call_method_or_proc_on resource, column.data | ||
end | ||
end | ||
end | ||
%> | ||
<%= csv_output %> |
8 changes: 0 additions & 8 deletions
8
lib/active_admin/views/templates/active_admin_default/index.csv.arb
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.