public
Description: Allows the storage of a number of boolean fields with just one table column
Homepage: http://www.brennandunn.com
Clone URL: git://github.com/brennandunn/preference_fu.git
Brennan Dunn (author)
Mon Mar 24 12:17:53 -0700 2008
commit  125325df48be85980773ecc2d43dd16f1bb94849
tree    dba531fb4ba988b32188a18d2a5e639780c40668
parent  00967b8928183c68d6c6e6df356a0b9d4f564616
name age message
file MIT-LICENSE Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
file README Mon Mar 24 12:17:53 -0700 2008 Fixed README [Brennan Dunn]
file Rakefile Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
file init.rb Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
file install.rb Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
directory lib/ Mon Mar 24 11:51:16 -0700 2008 Added Enumerable support [Brennan Dunn]
directory tasks/ Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
directory test/ Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
file uninstall.rb Mon Mar 24 11:29:47 -0700 2008 Initial functioning version - still no unit tes... [Brennan Dunn]
README
PreferenceFu
============

This plugin, greatly inspired by Jim Morris' blog post 
(http://blog.wolfman.com/articles/2007/08/07/bit-vector-preferences) aims to make it easy and flexible to store boolean 
preferences for an ActiveRecord model. This can be also used as a very quick way to setup an ACL.

Because the values are stored within a bit vector, a virtually unlimited number of preferences can be created without 
additional  migrations.

Feel free to email me with any suggestions or problems.

Blog: http://www.brennandunn.com
Email address: me@brennandunn.com


Setup
=====

Simply add an integer column to each table of the database that requires preferences. By default, the column used is 
'preferences', but by defining a method called preferences_column this can be modified.

  def preferences_column
    'other_column'
  end
    

Examples
========

Using PreferenceFu is very simple.

  class User < ActiveRecord::Base
  
    has_preferences :send_email, :change_theme, :delete_user, :create_user
    
    set_default_preference :send_email, true
  
  end
  
For new AR objects, all preference options will be set to false. This can be overwritten using set_default_preference. I 
really recommend you read the 'Warning' section below.

Setting a preference:
  ...individually
  @user.preferences[:delete_user] = true 
  
  ...mass assignment (useful with the params hash)
  @user.preferences = {:delete_user => true, :create_user => true}
  

Fetching a preference:
  @user.preferences[:change_theme]  => false
  

Enumerable...
  @user.preferences.size  => 4
  
  @user.preferences.each do |key, value|
    puts "#{key} is set to #{value}"
  end
  

Warning
=======

This works by taking the index of the splat supplied in has_preferences as the power of two, summing all values, and 
storing the sum in the preferences column. Because of this, the first item in the splat will be identified by 1, the 
second by 2, the third by 4, etc. Once you start using PreferenceFu in production, add new options to the *end* of the 
splat. At the moment, there's no safe way to delete a preference item at the moment. Any advice is welcome! 

  

Copyright (c) 2008 Brennan Dunn, released under the MIT license