Can Be Flagged

Allows for flags to be added to multiple and different models.

Resources

Install

  • To install as a gem (recommended):

** gem install can_be_flagged or ** Just add “gem ‘can_be_flagged’” to your Gemfile and “bundle install”

Generate your flag model:

** Rails 3: rails g flags

Then migrate your database:

** rake db:migrate

Usage

* Mark your ActiveRecord model can_be_flagged.

class Post < ActiveRecord::Base
	can_be_flagged
end

* Add a flag to a model instance

flaggable = Post.create(:text => "Don't be offended by this.")
flaggable.flags.create(:comment => "I am offended by that!")

* I like to add a counter cache column to my flaggable types:

add_column :posts, :flags_count, :integer

class Flag < ActiveRecord::Base
  belongs_to :flaggable, :polymorphic=>true, :counter_cache=>true
end

* You can have different types of flags

class Article
  can_be_flagged :offensive, :spam
end

* This gives you methods like this:
@article.offensive_flags.create(:comment => "gross!")
@article.spam_flags.create(:comment => "Don't sell your pharmaceuticals on this site!")
Aricle.find_spam_flags_for(@article)

* You can also add a callback

class Article
  can_be_flagged :after_add => :flagged

  def flagged
    # send an email
    # maybe delete this article if there are too many flags
  end

end

* In fact you can pass any options that are accepted by has_many:

class Article
  can_be_flagged :dependent=>:nullify
end

Credits

Juxie - The code for this plugin is heavily influenced by Acts As Commentable.

Bacon Bear? - Frankenstien’d some features from acts_as_flaggable

Contributors

Dan Hixon

License

Flag This is Copyright © 2010-2011 Dan Hixon. It is free software, and may be redistributed under the terms specified in the MIT-LICENSE file.