MakeFlaggable is an extension for building a user-centric flagging system for Rails 3 and 4 applications. It currently supports ActiveRecord models.
This repository is unfortunately no longer maintained. If this library is still relevant and you want to maintain it, I am happy to hand this repository over.
add MakeFlaggable to your Gemfile
gem'make_flaggable'
afterwards execute
bundleinstall
generate the required migration file
railsgeneratemake_flaggable
migrate the database
rakedb:migrate
# Specify a model that can be flagged.classArticle<ActiveRecord::Basemake_flaggableend# Specify a model that can flag another model.classUser<ActiveRecord::Basemake_flaggerend# You can specify that a flagger can only flag a flaggable once.classUser<ActiveRecord::Basemake_flagger:flag_once=>trueend# The user can now flag the flaggable.# If the user already flagged the flaggable and :flag_once was set then an AlreadyFlaggedError is raised.user.flag!(article, reason) # The method without bang(!) does not raise the AlreadyFlaggedError when the user flags the flaggable more than once.# Instead it just returns false and ignores the flagging.# If :flag_once was not set then this method behaves like flag! method.user.flag(article, reason) # The user may unflag an already done flagging.# If the user never flagged the flaggable then an NotFlaggedError is raised.user.unflag!(article) # The method without bang(!) does not raise the NotFlaggedError, but just returns false if the user never flagged# the flaggable.user.unflag(article) # Get all flaggings of a flaggable.article.flaggings# Get the flagging with a specified flag.article.flaggings.with_flag(:flag_name) # Get the flagger of the flagging.flagging = article.flaggings.with_flag(:flag_name).firstuser = flagging.flagger# Returns true if the flagger flagged the flaggable, false otherwise.user.flagged?(article, :flag_name) # Get the reason of a flagging.flagging = article.flaggings.firstflagging.reason# Get the flagger of the flagging.flagging = article.flaggings.firstuser = flagging.flagger# Returns true if the flagger flagged the flaggable, false otherwise.user.flagged?(article) # Return true if the flaggable was flagged by the flagger, false otherwise.article.flagged_by?(user) # Returns true if the article was flagged by any flagger at all, false otherwise.article.flagged?# Flaggings can also be accessed by its flagger.flagger.flaggings# In order to obtain all flaggers of a resource, the flaggers method can be invoked on the class# Passing no argument would return flaggers that have flagged across any flaggable resourceUser.flaggers# An optional argument can be passed, restricting the detection to a particular resourceUser.flaggers(Article)
MakeFlaggable uses RSpec for testing and has a rake task for executing the provided specs
rakespec
or simply
rakeCopyright © 2010-2011 Kai Schlamp (www.medihack.org), released under the MIT license