Greetings,
I just put together a plugin that allows you to easily specify default
values for AR models. This is useful when your defaults can''t (or you
don''t
want to) be specified by the database. I use it to populate attribute values
from user-configurable defaults. Eg:
class Person < ActiveRecord::Base
defaults :name => ''My name'', :age => 0
default :country do
Configuration.default_country
end
end
The default values are ignored if the attribute is given a specific value.
p = Person.new
p.name # "My name"
p = Person.new(:name => nil)
p.name # nil
There''s a few more examples here:
http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb
Install:
ruby script/plugin install
http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/
Cheers,
-Jonathan.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Posted at http://agilewebdevelopment.com/plugins/ activerecord_defaults :) -- Building an e-commerce site with Rails? http://www.agilewebdevelopment.com/rails-ecommerce On Oct 28, 2006, at 9:37 PM, Jonathan Viney wrote:> Greetings, > > I just put together a plugin that allows you to easily specify > default values for AR models. This is useful when your defaults > can''t (or you don''t want to) be specified by the database. I use it > to populate attribute values from user-configurable defaults. Eg: > > class Person < ActiveRecord::Base > defaults :name => ''My name'', :age => 0 > > default :country do > Configuration.default_country > end > end > > The default values are ignored if the attribute is given a specific > value. > > p = Person.new > p.name # "My name" > > p = Person.new(:name => nil) > p.name # nil > > There''s a few more examples here: http://svn.viney.net.nz/things/ > rails/plugins/active_record_defaults/lib/active_record_defaults.rb > > Install: > > ruby script/plugin install http://svn.viney.net.nz/things/rails/ > plugins/active_record_defaults/ > > Cheers, > -Jonathan. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Clever Jonathan Viney but why not at state defaults at migration. ? On 10/29/06, Benjamin Curtis <rails-4dtGyw5agdmakBO8gow8eQ@public.gmane.org> wrote:> > Posted at http://agilewebdevelopment.com/plugins/activerecord_defaults :) > -- > Building an e-commerce site with Rails? > http://www.agilewebdevelopment.com/rails-ecommerce > > > On Oct 28, 2006, at 9:37 PM, Jonathan Viney wrote: > > Greetings, > > I just put together a plugin that allows you to easily specify default > values for AR models. This is useful when your defaults can''t (or you don''t > want to) be specified by the database. I use it to populate attribute values > from user-configurable defaults. Eg: > > class Person < ActiveRecord::Base > defaults :name => ''My name'', :age => 0 > > default :country do > Configuration.default_country > end > end > > The default values are ignored if the attribute is given a specific value. > > > p = Person.new > p.name # "My name" > > p = Person.new(:name => nil) > p.name # nil > > There''s a few more examples here: > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb > > Install: > > ruby script/plugin install > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/ > > Cheers, > -Jonathan. > > > > > > >-- Never be afraid to try something new. Remember, amateurs built the ark; professionals built the Titanic! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Jonathan, I have a question. Did you implement through after_initialize? If so this can have the negative side-effect of setting nil values to the default values after Find? Or are you applying the defaults only through calls to New? J On 29-Oct-06, at 12:37 AM, Jonathan Viney wrote:> Greetings, > > I just put together a plugin that allows you to easily specify > default values for AR models. This is useful when your defaults > can''t (or you don''t want to) be specified by the database. I use it > to populate attribute values from user-configurable defaults. Eg: > > class Person < ActiveRecord::Base > defaults :name => ''My name'', :age => 0 > > default :country do > Configuration.default_country > end > end > > The default values are ignored if the attribute is given a specific > value. > > p = Person.new > p.name # "My name" > > p = Person.new(:name => nil) > p.name # nil > > There''s a few more examples here: http://svn.viney.net.nz/things/ > rails/plugins/active_record_defaults/lib/active_record_defaults.rb > > Install: > > ruby script/plugin install http://svn.viney.net.nz/things/rails/ > plugins/active_record_defaults/ > > Cheers, > -Jonathan. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
You can only define static defaults with migrations. This plugin allows the defaults to be determined when the model object is created. I use this to allow users to implement their own default values. -Jonathan. On 10/30/06, Leon Leslie <leonleslie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Clever Jonathan Viney but why not at state defaults at migration. ? > > > > On 10/29/06, Benjamin Curtis < rails-4dtGyw5agdmakBO8gow8eQ@public.gmane.org> wrote: > > > > Posted at http://agilewebdevelopment.com/plugins/activerecord_defaults:) > > -- > > Building an e-commerce site with Rails? > > http://www.agilewebdevelopment.com/rails-ecommerce > > > > > > On Oct 28, 2006, at 9:37 PM, Jonathan Viney wrote: > > > > Greetings, > > > > I just put together a plugin that allows you to easily specify default > > values for AR models. This is useful when your defaults can''t (or you don''t > > want to) be specified by the database. I use it to populate attribute values > > from user-configurable defaults. Eg: > > > > class Person < ActiveRecord::Base > > defaults :name => ''My name'', :age => 0 > > > > default :country do > > Configuration.default_country > > end > > end > > > > The default values are ignored if the attribute is given a specific > > value. > > > > p = Person.new > > p.name # "My name" > > > > p = Person.new(:name => nil) > > p.name # nil > > > > There''s a few more examples here: > > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb > > > > Install: > > > > ruby script/plugin install > > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/ > > > > Cheers, > > -Jonathan. > > > > > > > > > > > > > > > -- > Never be afraid to try something new. Remember, amateurs built the ark; > professionals built the Titanic! > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
No, after_initialize is not used. I simply aliased the original initialize method, and called it from within the one I defined. Existing records will remain unchanged. The default values are only used when creating new records. -Jonathan. On 10/30/06, Jodi Showers <jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote:> > Jonathan, I have a question. > Did you implement through after_initialize? If so this can have the > negative side-effect of setting nil values to the default values after Find? > > Or are you applying the defaults only through calls to New? > > J > > On 29-Oct-06, at 12:37 AM, Jonathan Viney wrote: > > Greetings, > > I just put together a plugin that allows you to easily specify default > values for AR models. This is useful when your defaults can''t (or you don''t > want to) be specified by the database. I use it to populate attribute values > from user-configurable defaults. Eg: > > class Person < ActiveRecord::Base > defaults :name => ''My name'', :age => 0 > > default :country do > Configuration.default_country > end > end > > The default values are ignored if the attribute is given a specific value. > > > p = Person.new > p.name # "My name" > > p = Person.new(:name => nil) > p.name # nil > > There''s a few more examples here: > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb > > Install: > > ruby script/plugin install > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/ > > Cheers, > -Jonathan. > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Thanx Jonathan. Good stuff.
I have a class, with a great number of attributes - would it be
possible to add a method call to assign all default values?
something like?
class Person < ActiveRecord::Base
defaults :set_defaults
def set_defaults
name = ''My name''
city = ''My city''
end
?
Jodi
On 29-Oct-06, at 8:53 PM, Jonathan Viney wrote:
> No, after_initialize is not used. I simply aliased the original
> initialize method, and called it from within the one I defined.
>
> Existing records will remain unchanged. The default values are only
> used when creating new records.
>
> -Jonathan.
>
> On 10/30/06, Jodi Showers
<jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote:
> Jonathan, I have a question.
>
> Did you implement through after_initialize? If so this can have the
> negative side-effect of setting nil values to the default values
> after Find?
>
> Or are you applying the defaults only through calls to New?
>
> J
>
> On 29-Oct-06, at 12:37 AM, Jonathan Viney wrote:
>
>> Greetings,
>>
>> I just put together a plugin that allows you to easily specify
>> default values for AR models. This is useful when your defaults
>> can''t (or you don''t want to) be specified by the
database. I use
>> it to populate attribute values from user-configurable defaults. Eg:
>>
>> class Person < ActiveRecord::Base
>> defaults :name => ''My name'', :age => 0
>>
>> default :country do
>> Configuration.default_country
>> end
>> end
>>
>> The default values are ignored if the attribute is given a
>> specific value.
>>
>> p = Person.new
>> p.name # "My name"
>>
>> p = Person.new(:name => nil)
>> p.name # nil
>>
>> There''s a few more examples here:
http://svn.viney.net.nz/things/
>> rails/plugins/active_record_defaults/lib/active_record_defaults.rb
>>
>> Install:
>>
>> ruby script/plugin install http://svn.viney.net.nz/things/rails/
>> plugins/active_record_defaults/
>>
>> Cheers,
>> -Jonathan.
>>
>>
>>
>>
>>
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
I''ve just updated the plugin to allow you to do this.
class Person < ActiveRecord::Base
def defaults
# do whatever you like in here
end
end
I could remove the class methods for defining defaults and move to this
method of simply having a defaults method on the model. This gives a bit
more flexibility, but does make specifying simple defaults for one or two
attributes take an extra line or two of code.
class Person < AR
defaults :name => ''Jonathan''
end
vs
class Person < AR
def defaults
self.name = ''Jonathan''
end
end
Thoughts?
-Jonathan.
On 11/2/06, Jodi Showers <jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org>
wrote:>
> Thanx Jonathan. Good stuff.
> I have a class, with a great number of attributes - would it be possible
> to add a method call to assign all default values?
>
> something like?
>
> class Person < ActiveRecord::Base
> defaults :set_defaults
>
> def set_defaults
> name = ''My name''
> city = ''My city''
> end
>
> ?
>
> Jodi
>
> On 29-Oct-06, at 8:53 PM, Jonathan Viney wrote:
>
> No, after_initialize is not used. I simply aliased the original initialize
> method, and called it from within the one I defined.
>
> Existing records will remain unchanged. The default values are only used
> when creating new records.
>
> -Jonathan.
>
> On 10/30/06, Jodi Showers
<jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote:
> >
> > Jonathan, I have a question.
> > Did you implement through after_initialize? If so this can have the
> > negative side-effect of setting nil values to the default values after
Find?
> >
> > Or are you applying the defaults only through calls to New?
> >
> > J
> >
> > On 29-Oct-06, at 12:37 AM, Jonathan Viney wrote:
> >
> > Greetings,
> >
> > I just put together a plugin that allows you to easily specify default
> > values for AR models. This is useful when your defaults can''t
(or you don''t
> > want to) be specified by the database. I use it to populate attribute
values
> > from user-configurable defaults. Eg:
> >
> > class Person < ActiveRecord::Base
> > defaults :name => ''My name'', :age => 0
> >
> > default :country do
> > Configuration.default_country
> > end
> > end
> >
> > The default values are ignored if the attribute is given a specific
> > value.
> >
> > p = Person.new
> > p.name # "My name"
> >
> > p = Person.new(:name => nil)
> > p.name # nil
> >
> > There''s a few more examples here:
> >
http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/lib/active_record_defaults.rb
> >
> > Install:
> >
> > ruby script/plugin install
> > http://svn.viney.net.nz/things/rails/plugins/active_record_defaults/
> >
> > Cheers,
> > -Jonathan.
> >
> >
> >
> >
> >
> >
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---
Fabulous Jonathan. I think that both options are valuable. I prefer the readability of defaults :name => ''Jonathan''. This is great for small assignments. The new method is for large numbers of assignments. I personally like both. good dry stuff JV. Jodi On 1-Nov-06, at 6:33 PM, Jonathan Viney wrote:> I''ve just updated the plugin to allow you to do this. > > class Person < ActiveRecord::Base > def defaults > # do whatever you like in here > end > end > > I could remove the class methods for defining defaults and move to > this method of simply having a defaults method on the model. This > gives a bit more flexibility, but does make specifying simple > defaults for one or two attributes take an extra line or two of code. > > class Person < AR > defaults :name => ''Jonathan'' > end > > vs > > class Person < AR > def defaults > self.name = ''Jonathan'' > end > end > > Thoughts? > > -Jonathan. > > On 11/2/06, Jodi Showers <jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote: > Thanx Jonathan. Good stuff. > > I have a class, with a great number of attributes - would it be > possible to add a method call to assign all default values? > > something like? > > class Person < ActiveRecord::Base > defaults :set_defaults > > def set_defaults > name = ''My name'' > city = ''My city'' > end > > ? > > Jodi > > On 29-Oct-06, at 8:53 PM, Jonathan Viney wrote: > >> No, after_initialize is not used. I simply aliased the original >> initialize method, and called it from within the one I defined. >> >> Existing records will remain unchanged. The default values are >> only used when creating new records. >> >> -Jonathan. >> >> On 10/30/06, Jodi Showers <jodi-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org> wrote: >> Jonathan, I have a question. >> >> Did you implement through after_initialize? If so this can have >> the negative side-effect of setting nil values to the default >> values after Find? >> >> Or are you applying the defaults only through calls to New? >> >> J >> >> On 29-Oct-06, at 12:37 AM, Jonathan Viney wrote: >> >>> Greetings, >>> >>> I just put together a plugin that allows you to easily specify >>> default values for AR models. This is useful when your defaults >>> can''t (or you don''t want to) be specified by the database. I use >>> it to populate attribute values from user-configurable defaults. Eg: >>> >>> class Person < ActiveRecord::Base >>> defaults :name => ''My name'', :age => 0 >>> >>> default :country do >>> Configuration.default_country >>> end >>> end >>> >>> The default values are ignored if the attribute is given a >>> specific value. >>> >>> p = Person.new >>> p.name # "My name" >>> >>> p = Person.new(:name => nil) >>> p.name # nil >>> >>> There''s a few more examples here: http://svn.viney.net.nz/things/ >>> rails/plugins/active_record_defaults/lib/active_record_defaults.rb >>> >>> Install: >>> >>> ruby script/plugin install http://svn.viney.net.nz/things/rails/ >>> plugins/active_record_defaults/ >>> >>> Cheers, >>> -Jonathan. >>> >>> >>> >>> >>> >> >> >> >> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---