can_flag plugin sees the light of day

Courtenay : March 17th, 2008

Following the previous article, I took the existing "acts_as_flaggable" plugin, extended it, rewrote most of it, added some generators and test harness from Acts as Authenticated, and voila -- an experimental plugin.

I've gone a similar route to actsas_authenticated and attachmentfu, in that some code is in the plugin, and a chunk of it is generated and installed into your application. I'm considering an alternate way of generating enough code that you can delete the plugin (or reinstall later). Anyway..

If you're on git, you can clone the source from

git clone git://github.com/courtenay/can_flag.git

and symlink it to your vendor/plugins directory.

Or, download as a tarball from

http://github.com/courtenay/can_flag/tarball/master

and pop it in your vendor/plugins directory.

What does it do?

First, you'll need to follow the directions in the README.

script/generate can_flag flags

This will create 'flags_controller' and associated files, route and views. It will also create a "_flag" partial in app/views/layouts, which you can modify and include throughout your application like

<%= render :partial => "layouts/flag", :object => @topic %>

You'll then need to set up your user model

class User < ActiveRecord::Base
  can_flag
end

and set up your article, topic, or whatever other content model you want to be flaggable. You can set this in as many classes as you like. There is also an "after_flagged" callback that you can use in the content model.

class Content < ActiveRecord::Base
  can_be_flagged :reasons => [ :spam, :troll, :inappropriate ]

  # optional callback
  def after_flagged
    # do something cool
    # censor! if flags.size > 5
    # user.suspend! if user.flaggings.size > 5 
  end
end

Of course the actions you perform in the 'after_flagged' callback are entirely optional and application-specific. The "Flag" model lives in the plugin directory. If you want to modify it, you can copy it into app/models.

You'll have a few new associations.

content has_many :flags
user has_many :flags # they reported
user has_many :flagging # they created this filth

Have a play with this very quickly produced code and let me know how you go.

On my todo list:

  • generate specs for the generated code
  • generate tests for those old-school test/unit peeps
  • write a few more tests for the actual generation and models.
  • add in user expert rankings as an option, so that good censors (aka prudes) can have more power
  • better feedback on flags, like "good" or "bad", rather than just "delete" as a way of clearing
  • add in positive as well as negative (let's not overlap with acts_as_rated)

1 Response to “can_flag plugin sees the light of day”

  1. Helder Says:

    Wow, that was fast!! Looks like great stuff. I'll try it out asap and report back. Thanks!

Sorry, comments are closed for this article.