Acts As Flaggable updated 4
Just checked in a small update to Acts As Flaggable that includes the following
- Incorporates suggestion from bitbutter to allow add_flag to take either a Flag object (as before) or a Hash. If passed a Hash it creates a Flag object from the hash you passed in.
- Added user_has_flagged? instance method to tell you if a user has flagged an object at all or flagged it with a specific flag
Grab it from trunk in the normal way!
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!
