assignable_values 0.4.1 adds secondary default values

Updated . Posted . Visible to the public.

assignable_values Show archive.org snapshot now lets you define a secondary default that is only used if the primary default value is not assignable:

class Song < ActiveRecord::Base
  assignable_values_for :year, :default => 1999, :secondary_default => lambda { Date.today.year } do
    (Date.today.year - 2) .. Date.today.year
  end
end

If called in 2013 the code above will fall back to:

Song.new.year # => 2013

This is especially useful in authorization scenarios with Consul Show archive.org snapshot where you want powerful defaults for superusers, but don't want to make records invalid by default for users with lesser privileges.

Henning Koch
Last edit
License
Source code in this card is licensed under the MIT License.
Posted by Henning Koch to makandra dev (2012-08-02 15:27)