<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Flvorful Bloggage: New Rails Plugin - Acts As Noteable</title>
    <link>http://blog.flvorful.com/articles/2007/06/10/new-rails-plugin-acts-as-noteable</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>New Rails Plugin - Acts As Noteable</title>
      <description>&lt;p&gt;So I was at work and I needed to add notes to multiple models in the same project (before i only had to worry about one model with notes, so it was pretty straightforward).  So i bit down and knocked out this little plugin.&lt;/p&gt;

&lt;p&gt;I borrowed heavily from Acts As Rateable by &lt;a href="http://www.juixe.com/techknow/index.php/2006/07/05/acts-as-rateable-plugin/"&gt;Juixe&lt;/a&gt;&lt;/p&gt;

    &lt;h3&gt;Installation&lt;/h3&gt;

    &lt;p&gt;Run the following command: &lt;/p&gt;

&lt;code&gt;script/plugin install http://acts-as-noteable.googlecode.com/svn/trunk/acts_as_noteable &lt;/code&gt;

    &lt;p&gt;The install will try and copy some scripts over, this works sometimes. If you get a rake error do the following:&lt;/p&gt;

    &lt;ol&gt;
    &lt;li&gt;cd vendor/plugins/acts_as_noteable&lt;/li&gt;
        &lt;li&gt;rake update_scripts&lt;/li&gt;

    &lt;/ol&gt;

    &lt;p&gt;Create a new rails migration:&lt;/p&gt;

    &lt;p&gt;&lt;code&gt;script/generate migration add_notes_to_project&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;and add the following self.up and self.down methods &lt;/p&gt;

&lt;pre&gt;
    def self.up
        create_table "notes", :force =&amp;amp;gt; true do |t|
            t.column "from", :string, :limit =&amp;amp;gt; 50, :default =&amp;amp;gt; "" 
            t.column "body", :text, :default =&amp;amp;gt; "" 
            t.column "created_at", :datetime, :null =&amp;amp;gt; false 
            t.column "noteable_id", :integer, :default =&amp;amp;gt; 0, :null =&amp;amp;gt; false 
            t.column "noteable_type", :string, :limit =&amp;amp;gt; 15, :default =&amp;amp;gt; "", :null =&amp;amp;gt; false 
        end 
    end 

    def self.down
        drop_table :notes 
    end 
 &lt;/pre&gt;

    &lt;p&gt;(one day i&amp;#8217;ll add a migration script to the plugin to automatically add this :) )&lt;/p&gt;

    &lt;p&gt;After that run &amp;#8220;rake migrate&amp;#8221; to create the table.&lt;/p&gt;

    &lt;p&gt;Now all you need to do is call acts_as_noteable in your model:&lt;/p&gt;

&lt;pre&gt;
class Model &amp;amp;lt; ActiveRecord::Base
  acts_as_noteable
end
&lt;/pre&gt;

    &lt;p&gt;Now your model is notable, but that&amp;#8217;s not all.&lt;/p&gt;

    &lt;p&gt;For only 19.95 you get a Helper method to help you create a nice form.  Ok, so i am not actually charging, but whatever.&lt;/p&gt;

    &lt;p&gt;Here&amp;#8217;s how the helper works.&lt;/p&gt;

    &lt;p&gt;In the view you want to display notes in put the following:&lt;/p&gt;

    &lt;p&gt;&lt;code&gt;&amp;amp;lt;%= display_notes(object) %&amp;amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;This will create a div filled with the note entries for the object and a &lt;span class="caps"&gt;&lt;span class="caps"&gt;AJAX&lt;/span&gt;&lt;/span&gt; form to add new notes to the object as well as delete notes from the object.  The add and delete methods are stored in the notes_controller.rb that gets copied over into your app/controllers directory.&lt;/p&gt;

    &lt;p&gt;The full implementation for this method is:&lt;/p&gt;

    &lt;p&gt;&lt;code&gt;&amp;amp;lt;%= display_notes(obj, partial_name = "notes/note", controller_name = "notes") %&amp;amp;gt;&lt;/code&gt;&lt;/p&gt;

    &lt;p&gt;I added the partial name and controller name in case you have multiple controllers in different folders, this way you can display notes for objects whose controllers are buried deep in a folder.&lt;/p&gt;

    &lt;p&gt;Example:  I have an &amp;#8220;admin&amp;#8221; folder for all the backend stuff and I needed a way to reference the notes controller from different places, otherwise &lt;span class="caps"&gt;&lt;span class="caps"&gt;RAILS&lt;/span&gt;&lt;/span&gt; will assume you are trying to reference a controller from the current folder your in.&lt;/p&gt;

    &lt;p&gt;After you have called display_notes, your done.&lt;/p&gt;

&lt;p&gt;Note**  This plugin has not been tested with Rails 2.0 yet&lt;p&gt;
    &lt;p&gt;peace&lt;/p&gt;
&lt;p&gt;&amp;#8212;jake&lt;/p&gt;</description>
      <pubDate>Sun, 10 Jun 2007 13:51:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c73aed0a-4d25-4ec8-9581-c1b3424d74ae</guid>
      <author>jake</author>
      <link>http://blog.flvorful.com/articles/2007/06/10/new-rails-plugin-acts-as-noteable</link>
      <category>Rails</category>
      <category>noteable</category>
      <category>plugins</category>
      <category>rails</category>
      <category>ruby</category>
      <trackback:ping>http://blog.flvorful.com/articles/trackback/6</trackback:ping>
    </item>
  </channel>
</rss>
