Permalink
Browse files

download csv of comments

  • Loading branch information...
1 parent c0a6c96 commit 7ead33114f8d989e280a565d5e7e238ad5088933 Michael Hale committed Jul 23, 2008
View
@@ -34,6 +34,7 @@ The snippets "comments", "comment" and "comment_form" are created by the migrati
== Contributors
+* Michael Hale of http://halethegeek.com/, sponsored by http://ignitesocialmedia.com/.
* Nathaniel Talbott of http://terralien.com/, sponsored by http://ignitesocialmedia.com/.
* John Croisant
* Jon Leighton
@@ -10,7 +10,16 @@ def index
nil
end
@page = Page.find(params[:page_id]) if params[:page_id]
- @comments = @page.nil? ? Comment.paginate(:page => params[:page], :order => "created_at DESC", :conditions => conditions) : @page.comments.paginate(:page => params[:page], :conditions => conditions)
+ @comments = if @page.nil?
+ Comment.paginate(:page => params[:page], :order => "created_at DESC", :conditions => conditions)
+ else
+ @page.comments.paginate(:page => params[:page], :conditions => conditions)
+ end
+
+ respond_to do |format|
+ format.html
+ format.csv { render :text => @comments.to_csv }
+ end
end
def destroy
@@ -5,6 +5,10 @@ class Comment < ActiveRecord::Base
before_save :auto_approve
before_save :apply_filter
+ def export_columns(format = nil)
+ %w[approved? author content created_at referrer]
+ end
+
def self.per_page
50
end
@@ -1,4 +1,11 @@
-<h1><%= @page.nil? ? "#{params[:status].titleize if params[:status]} Comments" : "#{@page.comments_count} #{pluralize(@page.comments_count, "Comment")} on #{link_to(@page.title, page_edit_url(@page.id))}" %></h1>
+<h1>
+ <% if @page.nil? %>
+ <%= "#{params[:status].titleize if params[:status]} Comments |
+ #{link_to('Download CSV', {:format => 'csv', :status => params[:status]})}" %>
+ <% else %>
+ <%= "#{@page.comments_count} #{pluralize(@page.comments_count, "Comment")} on #{link_to(@page.title, page_edit_url(@page.id))}" %>
+ <% end %>
+</h1>
<p class="comment-nav">
View:
View
@@ -7,6 +7,8 @@ class CommentsExtension < Radiant::Extension
define_routes do |map|
map.with_options(:controller => 'admin/comments') do |comments|
+ comments.comments_status 'admin/comments/:status'
+ comments.comments_status 'admin/comments/:status.:format'
comments.resources :comments, :path_prefix => "/admin", :name_prefix => "admin_", :member => {:approve => :get, :unapprove => :get}
comments.admin_page_comments 'admin/pages/:page_id/comments/:action'
comments.admin_page_comment 'admin/pages/:page_id/comments/:id/:action'
@@ -37,7 +39,7 @@ def has_visible_comments?
admin.page.index.add :node, "index_view_comments"
end
- admin.tabs.add "Comments", "/admin/comments?status=unapproved", :visibility => [:all]
+ admin.tabs.add "Comments", "/admin/comments/unapproved", :visibility => [:all]
{ 'notification' => 'false',
'notification_from' => '',
@@ -47,6 +49,35 @@ def has_visible_comments?
'akismet_url' => '',
'filters_enabled' => 'true',
}.each{|k,v| Radiant::Config.create(:key => "comments.#{k}", :value => v) unless Radiant::Config["comments.#{k}"]}
+
+ require "fastercsv"
+
+ ActiveRecord::Base.class_eval do
+ def self.to_csv(*args)
+ find(:all).to_csv(*args)
+ end
+
+ def export_columns(format = nil)
+ self.class.content_columns.map(&:name) - ['created_at', 'updated_at']
+ end
+
+ def to_row(format = nil)
+ export_columns(format).map { |c| self.send(c) }
+ end
+ end
+
+ Array.class_eval do
+ def to_csv(options = {})
+ return "" if first.nil?
+ if all? { |e| e.respond_to?(:to_row) }
+ header_row = first.export_columns(options[:format]).to_csv
+ content_rows = map { |e| e.to_row(options[:format]) }.map(&:to_csv)
+ ([header_row] + content_rows).join
+ else
+ FasterCSV.generate_line(self, options)
+ end
+ end
+ end
end
def deactivate

0 comments on commit 7ead331

Please sign in to comment.