Acts As Flaggable released 18
Here’s my first stab at a rails plugin, small as it may be.
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 “spam” or “inappropriate”. There’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 “spam”).
To install:
script/plugin install http://svn.baconbear.com/rails_plugins/acts_as_flaggable/trunk
To prepare your database, create a migration with the following:
def self.up
create_table :flags, :force => true do |t|
t.column :flag, :string, :default => ""
t.column :comment, :string, :default => ""
t.column :created_at, :datetime, :null => false
t.column :flaggable_id, :integer, :default => 0, :null => false
t.column :flaggable_type, :string, :limit => 15,
:default => "", :null => false
t.column :user_id, :integer, :default => 0, :null => false
end
add_index :flags, ["user_id"], :name => "fk_flags_user"
end
def self.down
drop_table :flags
end
To prepare your model:
class Model < 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
To use:
p = Post.find(someid)
p.add_flag Flag.new(:flag => :spam, :comment => 'some comment')
If you notice I used add_flag instead of the typical << operator allowed by rails. Currently it’s a requirement that you use add_flag if you need the flagged callback to be called. I’ll probably fix this in a future checkin.
Avid plugin users will recognize a lot of the code and README from the Acts As Commentable 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!
Trackbacks
Use the following link to trackback from your own site:
http://www.baconbear.com/articles/trackback/6

Sounds useful, thanks!
Would it be possible to make the addflag method responsible for creating the new flag object, rather than the user having to to it ‘manually’ when calling addflag? So “p.add_flag Flag.new(:flag => :spam, :comment => ‘some comment’)” would become; “p.add_flag :flag => :spam, :comment => ‘some comment’”
this seems neater to me.
This is the authorized online keno guide that will explain to you all you need to know about online keno and internet casinos.
online casino schule
online casino schule
Flag.new(:flag => params[:reason].to_sym, :comments => params[:comment])