GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of brennandunn/preference_fu
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/dynamix/preference_fu.git
commit  fa1572ff92588c47934c65dc0d76627b043f52d9
tree    cf04ba6debc8a1eb79942c19e4185913660f2162
parent  e2ca7f0fa788cb221b208a428abc747a14e82fe7
preference_fu / README
100644 81 lines (48 sloc) 3.063 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
PreferenceFuu
=============
 
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.
(for 32bit integer columns up to 32)
 
Feel free to email me with any suggestions or problems.
 
Blog: http://www.brennandunn.com
Email address: me@brennandunn.com
 
Modified by Martin Karlsch (martin@karlsch.com)
 
Setup
=====
 
Simply add an integer column to each table of the database that requires preferences. By default, the column used is 'preferences', but the column name can be modified by passing :column => 'new_name' to the has_preferences call.
 
To allow multiple preferences per model the option :accessor => 'some_name' can be passed. If :column was not specified the column name to use is derived from the value given to accessor.
 
schema like this:
  
  add_column :people, :preferences, :integer
    
 
Examples
========
 
Using PreferenceFu is very simple.
 
  class User < ActiveRecord::Base
  
    has_preferences :send_email, :change_theme, :delete_user, :create_user,
     :default => { :send_mail => true }
    
    has_preferences :birthday, :holiday, :party,
     :accessor => 'reminders'
  
  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 key:
  ...individually
  @user.preferences[:delete_user] = true
    
  ...mass assignment (useful with the params hash)
  @user.preferences = {:delete_user => true, :create_user => true}
  
Setting an option as true doesn't necessarily need to be done with the Boolean true - in fact, the Fixnum 1, and strings '1', 'y' and 'yes' are all valid. This is particularly helpful for checkbox form posts.
 
   @user.preferences[:create_user] = 'yes'
 
 
Fetching a key:
  @user.preferences[:change_theme] => false
  @user.preferences.change_theme? => false
  @user.reminders.birthday? => false
 
Getting the index of a key:
  @user.reminders.index(:party) => 4
  
 
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
Modifications Copyright (c) 2008 Martin Karlsch, released under the MIT license