Coder Social home page Coder Social logo

make_flaggable's Introduction

MakeFlaggable

MakeFlaggable is an extension for building a user-centric flagging system for Rails 3 applications. It currently supports ActiveRecord models.

Installation

add MakeFlaggable to your Gemfile

gem 'make_flaggable'

afterwards execute

bundle install

generate the required migration file

rails generate make_flaggable

migrate the database

rake db:migrate

Usage

# Specify a model that can be flagged.
class Article < ActiveRecord::Base
  make_flaggable
end

# Specify a model that can flag another model.
class User < ActiveRecord::Base
  make_flagger
end

# You can specify that a flagger can only flag a flaggable once.
class User < ActiveRecord::Base
  make_flagger :flag_once => true
end

# 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).first
user = 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.first
flagging.reason

# Get the flagger of the flagging.
flagging = article.flaggings.first
user = 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

Testing

MakeFlaggable uses RSpec for testing and has a rake task for executing the provided specs

rake spec

or simply

rake

Copyright © 2010-2011 Kai Schlamp (www.medihack.org), released under the MIT license

make_flaggable's People

Watchers

 avatar  avatar

Forkers

jziggas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.