No commit activity in last 3 years
No release in over 3 years
Green eggs and spam presents the user with a simple question: What color is this image? You'll supply the images and a key of which one's which, and the gem will help with the rest.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 2.1.0
>= 1.3.3

Runtime

>= 3.0.0
 Project Readme

Green Eggs and Spam Build Status

A simple way to filter spam in your rails forms. GreenEggsAndSpam presents users with an easy question: What color is this image?

You'll supply the images and a key of which one's which. The gem will handle the rest.

Usage

Install the gem just like you would any other:

(sudo) gem install green_eggs_and_spam

# or with bundler

gem 'green_eggs_and_spam', '>= 0.2.1'

Create a handful of color coded images and name them something other than their color or design. 1.png, banana.png or firetruck.png for example. Tell GreenEggsandSpam which ones which with an initializer:

# config/initializers/green_eggs_and_spam.rb

GreenEggsAndSpam.options[:key_index] = { "1" => "blue", "banana" => "yellow", "firetruck" => "red" }

Or just use the defaults, making your images 1.png, 2.png, 3.png:

{ "1" => "red", "2" => "green", "3" => "blue" }

Include the helper in your form:

= form_for @comment, :url => comment_path do |f|
  %p
    = f.label :comment
    = f.text_field :comment
    
  // Here's what your interested in:
  %p
    = anti_spam_form "What color is this piece of bacon?"
   
    // the first optional argument is your custom question, the second is the form options hash
    
  = f.submit 'send'

Chances are your form is interacting with a model. If that's the case, hook up the validator to your model like so:

class Egg < ActiveRecord::Base
  
  validates_anti_spam
  
  # with options
  # validates_anti_spam :on => :create, :unless => proc{|r| # some logic }
  
end

Next, setup your controller with the has_anti_spam method. This will prepare the controller and give you access to the helper methods.

class EggsController < ApplicationController
  
  has_anti_spam
  
  def create

    # merge the antispam params into your model's params before validation
    @egg = Egg.new(params[:egg].merge(:antispam => params[:antispam]))
    
    # validate as usual
    if @egg.valid? && @egg.save
      # do something
    end
    
  end
  
end

That's it!

But what if my form isn't validating a model? No big deal, just use the anti_spam_valid? helper method in your controller.

class CommentsController < ApplicationController
  
  has_anti_spam
  
  def create
    
    if anti_spam_valid?
      # do something
    end
    
  end
  
end

Customization

So you're using .gif's or you're images aren't stored in /images/antispam. Here's some available options for the form helper.

# the defaults
{
  :alt => 'AntiSpam Image',
  :class => 'antispam-image',
  :path => '/images/antispam',
  :extension => 'jpg'
}

# inline
anti_spam_form, "Your custom color question?", { :extension => 'png', :path => '/images' }
  
# or set globally in your initializer
GreenEggsAndSpam.options[:form_options] = { :extension => 'png', :path => '/images' }

Demo

If you'd like to see GreenEggsAndSpam in action, there is a demo app running on heroku at http://green-eggs-and-spam-demo.heroku.com. There's a similar demo/test-app located in test/dummy if you'd like to run it locally.

git://github.com/citrus/green_eggs_and_spam.git
cd green_eggs_and_spam
bundle install
cd test/dummy
rake db:migrate
rails s

Testing

Tests can be run with rake test or just rake.

git://github.com/citrus/green_eggs_and_spam.git
cd green_eggs_and_spam
bundle
rake

License

Copyright (c) 2011 Spencer Steffen, released under the New BSD License All rights reserved.