<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Random Thoughts &#187; ActsAsCSVable</title>
	<atom:link href="http://peterleonhardt.com/blog/category/rails_plugins/acts-as-csvable/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterleonhardt.com/blog</link>
	<description>A gathering place for ponderings</description>
	<lastBuildDate>Wed, 15 Jul 2009 01:45:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>ActsAsCSVable v1.0</title>
		<link>http://peterleonhardt.com/blog/2008/09/15/acts-as-csvable/</link>
		<comments>http://peterleonhardt.com/blog/2008/09/15/acts-as-csvable/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 17:03:16 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[ActsAsCSVable]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://peterleonhardt.com/blog/2008/09/15/acts-as-csvable/</guid>
		<description><![CDATA[It&#8217;s finally here: ActsAsCSVable! Important Update! on June 25th, 2009 ActsAsCSVable is now on GitHub.com and all development has moved to git. http://github.com/pjleonhardt/ActsAsCSVable/tree/master Git Repository: git://github.com/pjleonhardt/ActsAsCSVable.git History I developed a plugin while working as a Rails Developer, CSVExportable, which was originally based off of some code from Bryan Helmkamp. CSVExportable was good, but it was [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s finally here: ActsAsCSVable! </p>
<p class="alt">
<strong>Important Update!</strong> on June 25th, 2009<br />
ActsAsCSVable is now on GitHub.com and all development has moved to git.<br />
<a href="http://github.com/pjleonhardt/ActsAsCSVable/tree/master">http://github.com/pjleonhardt/ActsAsCSVable/tree/master</a><br />
Git Repository: git://github.com/pjleonhardt/ActsAsCSVable.git</p>
<p><strong>History</strong><br />
I developed a plugin while working as a Rails Developer, <a href="http://peterleonhardt.com/blog/2008/08/20/introducing-csvexportable/">CSVExportable</a>, which was originally based off of some <a href="http://www.brynary.com/2007/4/28/export-activerecords-to-csv">code from Bryan Helmkamp</a>.</p>
<p>CSVExportable was good, but it was an evolutionary design. As such, it was hodgepodged together with no since of organization. I decided to rewrite the plugin from scratch, and design it a little bit better this time around (since I knew where I would end up this time around). It also needed a name change, since it does both CSV <em>exporting and importing</em>. I&#8217;ve seen a few Rails plugins that will do CSV exporting, but I haven&#8217;t found one that does both, or even importing.  Importing is definitely tricky, but I think that if you follow a few guidelines, it can be useful in the right situations.</p>
<p><strong>Installation</strong><br />
Subversion: <a href="http://svn.peterleonhardt.com/rails_plugins/acts_as_csvable/trunk">Trunk </a>| <a href="http://svn.peterleonhardt.com/rails_plugins/acts_as_csvable/branches/stable/1.0">Stable v1.0</a> | <a href="http://svn.peterleonhardt.com/rails_plugins/acts_as_csvable/development">Development Application</a><br />
Project Management: <a href="http://redmine.peterleonhardt.com/projects/show/acts-as-csvable">Redmine</a></p>
<p><strong>Usage</strong></p>
<pre class="brush: ruby;">
   #in your model
    acts_as_csv_exportable :fancy_naming, [{:first =&gt; &quot;first_name&quot;}, {:last =&gt; &quot;last_name&quot;}, {:email =&gt; &quot;email_address&quot;}, {:address =&gt; &quot;mailing_address&quot;}]
    acts_as_csv_exportable :detailed, [:first_name, :last_name, :email_address, :mailing_address, :formatted_date]
    acts_as_csv_exportable :default, [:id, :first_name, :last_name]

    acts_as_csv_importable :default, [:id, :first_name, :last_name]
    acts_as_csv_importable :new_projects, [:name, :details, :\owner_username]

    def formatted_date
      self.date.strftime(&quot;%Y/%M/%D&quot;)
    end

    def owner_username=(username)
      self.owner = Users.find_by_username(username)
    end

  # your contrller
  def index
    @people = Person.find(:all)

    respond_to do |wants|
      wants.csv { render :text =&gt; @people.to_csv(:columns =&gt; [:first_name, :last_name, :date_of_birth]) }
      # or
      wants.csv { render :text =&gt; @people.to_csv(:template =&gt; :fancy) }
      # or
      wants.csv { render :text =&gt; @peope.to_csv } #renders the :default template
    end
  end

  #/people/import
  def import
    file = params[:csv_uplodad]
    template = params[:upload_template]
    projects = Project.from_csv(file, template)

    if projects.all?(&amp;:valid?)
      projects.each(&amp;:save)
    else
      # Options options options...
      # 1) Save valid rows, and re-export invalid rows
      # 2) Save nothing, and tell user which rows were invalid
      # 3) Save nothing and tell them rows are invalid
      # 4) Up to you!
    end
  end
</pre>
<p>I only ask that if you use the plugin, let me know! I love to keep track of how its being used. The more response, the more likely I am to improve it and write more plugins! Leave a message below, or send me an email. I&#8217;d love to hear your thoughts for improvement, as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterleonhardt.com/blog/2008/09/15/acts-as-csvable/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Introducing CSVExportable</title>
		<link>http://peterleonhardt.com/blog/2008/08/20/introducing-csvexportable/</link>
		<comments>http://peterleonhardt.com/blog/2008/08/20/introducing-csvexportable/#comments</comments>
		<pubDate>Wed, 20 Aug 2008 06:28:30 +0000</pubDate>
		<dc:creator>Pete</dc:creator>
				<category><![CDATA[ActsAsCSVable]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Ruby On Rails]]></category>

		<guid isPermaLink="false">http://peterleonhardt.com/blog/2008/08/20/introducing-csvexportable/</guid>
		<description><![CDATA[This plugin has been replaced by ActsAsCSVExportable, check it out! I am finally releasing my first Rails Plugin: CSVExportable ! As many great things in the Rails community (and Rails itself) this plugin was born out of necessity. The need of the plugin in my projects defined its functionality and the design decisions taken to [...]]]></description>
			<content:encoded><![CDATA[<p><strong>This plugin has been replaced by <a href="http://peterleonhardt.com/blog/2008/09/15/acts-as-csvable/">ActsAsCSVExportable</a>, check it out!</strong></p>
<p>I am finally releasing my first Rails Plugin: CSVExportable !</p>
<p>As many great things in the Rails community (and Rails itself) this plugin was born out of necessity. The need of the plugin in my projects defined its functionality and the design decisions taken to make it. Of course the project is open source, and free to modify and redistribute with credits intact.</p>
<p>Current version: 0.9<br />
Download (SVN): <a href="http://svn.peterleonhardt.com/rails_plugins/acts_as_csvable/branches/stable/0.9/">Stable 0.9</a><br />
Follow: <a href="http://redmine.peterleonhardt.com/projects/show/acts-as-csvable>Redmine</a><br />
Credits: I developed this plugin while working as part of the CLA Webteam at the University of Minnesota. It is a modified version of code released by Bryan Helmkamp, redistributed under terms of the license of the original code.</p>
<p><strong>Introduction</strong></p>
<p>It is nearly inevitable when working in a corporate or University environment to run into the need to be able to export, manage, and import data through CSV. Business analysts, accountants, etc, seems to like excel very much and why not, it provides them much power and is a system that has worked for them for a good number of years. This plugin is designed to be able to easily create an interface between your application models and excel.</p>
<p>CSVExportable has the capability of <em>exporting </em>and <em>importing </em>data from your ActiveRecord models. This can be accomplished through the usage of templates defined in the model, the default columns (content columns) or defined in the controller when the export method is called.<br />
<span id="more-17"></span><br />
<strong>Simple Usage</strong></p>
<p>Here is a very simple usage for exporting all of the items belonging to a certain category.</p>
<pre class="brush: ruby;">
def index
  @category = Category.find(params[:id])
  @items = @category.items

  responds_to do |wants|
    wants.html
    wants.csv { render :text =&gt; @items.to_csv }
  end
end
</pre>
<p>This will prompt the browser to download the csv file to the users computer. The CSV MimeType and responsiveness to the responds_to method is injected by the plugin. This creates an &#8216;out-of-the-box&#8217; functionality of csv exporting of any ActiveRecord Model.</p>
<p>You can do more customized things such as defining a template in the model:</p>
<pre class="brush: ruby;">
#item.rb
  csv_export_columns :advanced, [{:item_id =&gt; :id}, {:name =&gt; :name}, {:description =&gt; :description}, {:status =&gt; &quot;status.titleize&quot;}]

 #categories_controller.rb
 wants.csv { render :text =&gt; @items.to_csv(:template =&gt; :advanced) }
</pre>
<p> This adds an import/export template named <em>advanced</em> to the Item model, usable in both to_csv and from_csv. In our template, we have passed an array of hashes. We pass an array to maintain positioning, each hash is a key => val pair. The key is the text to be used in the header column of the csv. The value of the hash will be the method called to populate the csv for each item. As you can see you can chain methods together (&#8220;status.titleize&#8221;). You cannot, however, call a method with arguments. If you want to format a date or similar, create a method in your model that will output the appropriate format and use that in your template call. </p>
<p>Instead of specifying an export template, you can also simply supply the columns you wish to export:</p>
<pre class="brush: ruby;">
  @items.to_csv(:columns =&gt; [{:column1 =&gt; :some_method}, {:column_2 =&gt; :another_method}])
</pre>
<p>For more information on the plugin, there is a substantial documentation included with the source. It is rdoc format, so you can easily generate the doc locally. Please let me know if you have any questions and I&#8217;m completely open to suggestions.</p>
<p><strong>Future Plans</strong><br />
There isn&#8217;t much that I&#8217;d like to do with this plugin at the moment.<br />
For the 1.0 release I will be cleaning up the plugin a little bit and the API might change slightly (for template definition).<br />
A 2.0 release, if deemed necessary, will be a completely rewrite from what I have learned, attempting to keep the API the same as the 1.0 release for reverse compatibility.<br />
A Name change might be in order since importing has been added to the plugin</p>
]]></content:encoded>
			<wfw:commentRss>http://peterleonhardt.com/blog/2008/08/20/introducing-csvexportable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

