<?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>The Bacon Bear Blog: Acts As Flaggable released</title>
    <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Acts As Flaggable released</title>
      <description>&lt;p&gt;Here&amp;#8217;s my first stab at a rails plugin, small as it may be.&lt;/p&gt;

&lt;p&gt;Acts As Flaggable is a model modifier which allows flags to be associated with records.  Authors of community driven sites may find this useful for quickly adding the ability to mark records as &amp;#8220;spam&amp;#8221; or &amp;#8220;inappropriate&amp;#8221;.  There&amp;#8217;s also facilities in place to automatically take a specific action automatically if a record has been flagged a certain way too many times (Think auto-removing posts that have been marked as &amp;#8220;spam&amp;#8221;).&lt;/p&gt;

&lt;p&gt;To install:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;script/plugin install http://svn.baconbear.com/rails_plugins/acts_as_flaggable/trunk
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To prepare your database, create a migration with the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def self.up
   create_table :flags, :force =&gt; true do |t|
     t.column :flag, :string, :default =&gt; ""
     t.column :comment, :string, :default =&gt; ""
     t.column :created_at, :datetime, :null =&gt; false
     t.column :flaggable_id, :integer, :default =&gt; 0, :null =&gt; false
     t.column :flaggable_type, :string, :limit =&gt; 15,
       :default =&gt; "", :null =&gt; false
     t.column :user_id, :integer, :default =&gt; 0, :null =&gt; false
   end

   add_index :flags, ["user_id"], :name =&gt; "fk_flags_user"
 end

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

&lt;p&gt;To prepare your model:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class Model &lt; ActiveRecord::Base
    acts_as_flaggable

        protected
        # Flagged method is called after every add_flag.  This callback method
        # is totally optional and does not have to be included in the model
        def flagged(flag, flag_count)
            # Add code here to take action when flag reaches a certain flag_count
        end
 end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To use:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;p = Post.find(someid)
p.add_flag Flag.new(:flag =&gt; :spam, :comment =&gt; 'some comment')
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you notice I used add_flag instead of the typical &amp;lt;&amp;lt; operator allowed by rails.  Currently it&amp;#8217;s a requirement that you use add_flag if you need the flagged callback to be called.  I&amp;#8217;ll probably fix this in a future checkin.&lt;/p&gt;

&lt;p&gt;Avid plugin users will recognize a lot of the code and README from the &lt;a href="http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/"&gt;Acts As Commentable&lt;/a&gt; plugin.  Thanks to the clear and readable manner Acts As Commentable was written in, it was a fairly easy change to morph the commentable plugin into the flaggable plugin I have here.  So, special thanks to Juixe for releasing a well written plugin!&lt;/p&gt;</description>
      <pubDate>Mon, 04 Dec 2006 01:32:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:d58bcfdf-7e21-4239-99d9-91077f547d84</guid>
      <author>Jeff</author>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released</link>
      <category>rails</category>
      <category>plugins</category>
      <category>acts_as_flaggable</category>
      <trackback:ping>http://www.baconbear.com/articles/trackback/6</trackback:ping>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Tom</title>
      <description>As Soph already mentioned, there is a spelling error in flag.rb. This will cause errors (and they sure are ugly when unit testing). To fix:

change:
  validates_prescence_of :flag
to
  validates_presence_of :flag
</description>
      <pubDate>Thu, 25 Oct 2007 16:19:05 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:20ad8418-afc8-4883-a7c2-966947d21499</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1756</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Tom</title>
      <description>As Soph already mentioned, there is a spelling error in flag.rb. This will cause errors (and they sure are ugly when unit testing). To fix:

change:
  validates_prescence_of :flag
to
  validates_presence_of :flag
</description>
      <pubDate>Thu, 25 Oct 2007 16:18:58 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:51525893-5654-4641-8410-3d4447b76a1f</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1755</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Karen</title>
      <description>If you want the :flag to come from a form field, the method to_sym is useful! In my case, I'm using  a select box to populate flag reasons. Here's an example of the controller converting the selected value to a symbol: 

&lt;br /&gt;&lt;br /&gt;
Flag.new(:flag =&gt; params[:reason].to_sym, :comments =&gt; params[:comment])</description>
      <pubDate>Mon, 06 Aug 2007 13:17:55 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:6dac6198-1441-4b3b-a47a-e9e46b450854</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1735</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Dallas Slieker</title>
      <description>I agree with Chris Blackburn. Certainly an annoyance.</description>
      <pubDate>Fri, 29 Jun 2007 14:10:09 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:3ba82909-6c07-4860-b991-3b1623169940</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1728</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Chris Blackburn</title>
      <description>Jeff, it would be helpful if you setup your repository so people don't have to rename the folder from 'trunk' to 'acts_as_flaggable' after they install it.  It is just a peeve of mine but annoying nonetheless.</description>
      <pubDate>Fri, 25 May 2007 14:15:04 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:7cb0c5c5-eda3-4948-9027-1df226ab2296</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1727</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Soph</title>
      <description>I just installed the plugin and it looks like, that there is a spelling error. In "lib/flag.rb"

The 9th Line is: 

"validates_prescence_of :flag"

it is not prescence it should spelled presence

"validates_presence_of :flag"</description>
      <pubDate>Sun, 04 Feb 2007 10:49:04 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:3b497f8b-cdb8-489b-960b-1658c74386fa</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-1722</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by Jeff</title>
      <description>I think I like that!  I'll add it in when I get the chance.</description>
      <pubDate>Tue, 05 Dec 2006 16:44:03 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:2a238bb6-9b4e-4ad3-be20-c14e03029ae7</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-8</link>
    </item>
    <item>
      <title>"Acts As Flaggable released" by bitbutter</title>
      <description>&lt;p&gt;Sounds useful, thanks!&lt;/p&gt;

&lt;p&gt;Would it be possible to make the add&lt;em&gt;flag method responsible for creating the new flag object, rather than the user having to to it &amp;#8216;manually&amp;#8217; when calling add&lt;/em&gt;flag?
So 
&amp;#8220;p.add_flag Flag.new(:flag =&gt; :spam, :comment =&gt; &amp;#8216;some comment&amp;#8217;)&amp;#8221;
would become;
&amp;#8220;p.add_flag :flag =&gt; :spam, :comment =&gt; &amp;#8216;some comment&amp;#8217;&amp;#8221;&lt;/p&gt;

&lt;p&gt;this seems neater to me.&lt;/p&gt;</description>
      <pubDate>Tue, 05 Dec 2006 04:26:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:69e9d0d2-d6b4-4835-99d8-7ed3bb7ee5db</guid>
      <link>http://www.baconbear.com/articles/2006/12/04/acts-as-flaggable-released#comment-7</link>
    </item>
  </channel>
</rss>
