No commit activity in last 3 years
No release in over 3 years
Virtual attributes helper for models in Rails3
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

VirtualAttributes

DESCRIPTION

Usually you can use attr_accessor to define virtual attribute. You could even use attr_accessor_with_default to set default value.

But in any case you are unable to typecast values.

Let's assume you receive data from form submit.

> params[:user].inspect
=> {'enabled' => 'true', 'priority' => '0.5'}

Having attributes defined as table columns values would be actual true and 0.5.

But with attr_accessor fields will still be strings.

This is what virtual_attribute handles.

Origins

Inspired with virtual_attribute gem by Philip Roberts I tried to use ActiveRecord to typecast.

Usage

class User < ActiveRecord::Base
  virtual_attribute :enabled,  :type => :boolean, :default => false
  virtual_attribute :priority, :type => :float,   :default => 1.0
end