Project

preferable

0.01
Repository is archived
No commit activity in last 3 years
No release in over 3 years
Great for storing user preferences
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

Preferable

Simple filtering for ActiveRecord. Sanitizes simple and readable query parameters -great for building APIs & HTML filters.

Installation

Add this line to your Gemfile:

gem "preferable"

Then, bundle:

$ bundle install

Add a new migration to your project. Example:

$ rails g migration AddPreferencesToUsers preferences:text

# Rails 2 & 3.0
class AddPreferencesToUsers < ActiveRecord::Migration
  def self.up
    add_column :users, :preferences, :text
  end

  def self.down
    remove_column :users, :preferences, :text
  end
end

# Rails >=3.1
class AddPreferencesToUsers < ActiveRecord::Migration
  def change
    add_column :users, :preferences, :text
  end
end

Usage Examples:

Specify simple preferences, with defaults:

class User < ActiveRecord::Base

  preferable do
    integer :theme_id
    boolean :newsletter, :default => false
    string  :font_color, :default => "444444"
  end

end

Read and write preferences:

user = User.find(1)
user.preferences[:newsletter] # => false

# Set single preferences (with type casting)
user.preferences[:newsletter] = '1'
user.preferences[:newsletter] # => true

# or, bulk-set (e.g. from forms)
user.preferences = { :newsletter => '1' }

# Makes preferences persistent
user.save

Specify conditions (if/unless):

class User < ActiveRecord::Base

  preferable do
    string  :font_color, :default => "444444", :if => lambda {|v| v =~ /^[A-F0-9]{6}$/ }
  end

end

user = User.find(1)
user.preferences[:font_color] = 'INVALID'
user.preferences[:font_color] # => '444444'

Store even arrays (with internal casting):

class User < ActiveRecord::Base

  preferable do
    array  :popular_words, :cast => :string
  end

end

user = User.find(1)
user.preferences[:popular_words] = 'hello'
user.preferences[:popular_words] # => ['hello']

License

Copyright (C) 2011 Dimitrij Denissenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.