Trevor Squires
2006-Mar-01 17:25 UTC
[Rails] validations without AR - going crazy trying to find link
Hi, in the past few months someone posted an entry on their blog about how to do validations in non-AR classes and I cant find it any more. Anyone have a link? Thanks, Trevor -- Trevor Squires http://somethinglearned.com
David Blundell
2006-Mar-01 17:39 UTC
[Rails] validations without AR - going crazy trying to find link
I can''t find the original blog that I read a while ago but I use it like this: Make a ValidatingBase that the non-AR classes inherit from. class ValidatingBase def save; end def update_attribute; end def new_record?; end def self.human_attribute_name(arg) arg.to_s end include ActiveRecord::Validations def [](key) instance_variable_get(key) end include ActiveRecord::Validations def initialize(attributes = nil) return nil if attributes.nil? attributes.each do |key, value| method("#{key}=").call(value) end end end class Example < ValidatingBase attr_accessor :request_postcode, :request_telephone_number validates_presence_of :request_telephone_number protected def validate errors.add(:request_postcode, "must be a valid postcode") unless request_postcode.blank? || request_postcode =~ /\b[A-PR-UWYZ][A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW-Z]{2}/ end end Hope this helps, David On Wed, 2006-03-01 at 09:24 -0800, Trevor Squires wrote:> Hi, > > in the past few months someone posted an entry on their blog about > how to do validations in non-AR classes and I cant find it any more. > > Anyone have a link? > > Thanks, > Trevor-- David Blundell 100 Percent IT
Trevor Squires
2006-Mar-01 17:58 UTC
[Rails] validations without AR - going crazy trying to find link
Hi David, well, you have no idea how embarrassing it is but the code you gave me is the same code uploaded to the wiki ages ago. http://wiki.rubyonrails.org/rails/pages/ HowToUseValidationsWithoutExtendingActiveRecord And wouldn''t you know it, someone updated that page with a link to the (different) technique I was looking for - the one from technoweenie. Thanks for your help, it got me there in the end. Regards, Trevor On 1-Mar-06, at 9:39 AM, David Blundell wrote:> I can''t find the original blog that I read a while ago but I use it > like > this: > > Make a ValidatingBase that the non-AR classes inherit from. > > class ValidatingBase > def save; end > def update_attribute; end > def new_record?; end > def self.human_attribute_name(arg) > arg.to_s > end > include ActiveRecord::Validations > def [](key) > instance_variable_get(key) > end > include ActiveRecord::Validations > def initialize(attributes = nil) > return nil if attributes.nil? > > attributes.each do |key, value| > method("#{key}=").call(value) > end > end > end > > class Example < ValidatingBase > attr_accessor :request_postcode, :request_telephone_number > validates_presence_of :request_telephone_number > protected > def validate > errors.add(:request_postcode, "must be a valid postcode") > unless request_postcode.blank? || request_postcode =~ /\b[A-PR-UWYZ] > [A-HK-Y0-9][A-HJKSTUW0-9]?[ABEHMNPRVWXY0-9]? {1,2}[0-9][ABD-HJLN-UW- > Z]{2}/ > end > end > > Hope this helps, > > David-- Trevor Squires http://somethinglearned.com
Peter Donald
2006-Mar-01 23:21 UTC
[Rails] validations without AR - going crazy trying to find link
Hi, It may have been the following entry; http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects and the plugin is available via; svn: http://www.realityforge.org/svn/code/active-form/trunk On 3/2/06, Trevor Squires <trevor@protocool.com> wrote:> Hi, > > in the past few months someone posted an entry on their blog about > how to do validations in non-AR classes and I cant find it any more. > > Anyone have a link?-- Cheers, Peter Donald
Trevor Squires
2006-Mar-02 02:55 UTC
[Rails] validations without AR - going crazy trying to find link
Hi Peter, thanks for the pointer. By the way (and in case you''re not already aware), when 1.1 comes out you can remove the hacks to Dispatcher by doing: class ActiveForm include Reloadable::Subclasses # rest of class definition here end Regards, Trevor -- Trevor Squires http://somethinglearned.com On 1-Mar-06, at 3:21 PM, Peter Donald wrote:> Hi, > > It may have been the following entry; > > http://www.realityforge.org/articles/2005/12/02/validations-for-non- > activerecord-model-objects > > and the plugin is available via; > > svn: http://www.realityforge.org/svn/code/active-form/trunk > > On 3/2/06, Trevor Squires <trevor@protocool.com> wrote: >> Hi, >> >> in the past few months someone posted an entry on their blog about >> how to do validations in non-AR classes and I cant find it any more. >> >> Anyone have a link? > > -- > Cheers, > > Peter Donald > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Stuart Rackham
2006-Mar-02 03:54 UTC
[Rails] Re: validations without AR - going crazy trying to find link
Trevor Squires wrote:> Hi, > > in the past few months someone posted an entry on their blog about > how to do validations in non-AR classes and I cant find it any more. > > Anyone have a link? > > Thanks, > Trevor > > -- > Trevor Squires > http://somethinglearned.comI derive table-less models from this AR class (based on the tip posted by techno-weenie): 8<----------------------------------------------------- # Memory based table-less model to validate input forms that don''t have an # underlying table. # # These act like strongly typed structures and inherit ActiveRecords # validations which make them very useful for form validation. # # Based on tip by technoweenie: # http://rails.techno-weenie.net/tip/2005/11/19/validate_your_forms_with_a_table_less_model # class TransientRecord < ActiveRecord::Base def initialize(attributes=nil) super # Explicit assignment of the id attribute is necessary because the default # ActiveRecord (1.0.0) behavior is to omit id from the @attributes hash # when a new record is instantiated. Until the id entry is added to # @attributes with an explicit assignment it won''t be processed by the # attributes or attributes= methods. self.id = nil if self.class.columns_hash[''id''] self.attributes = attributes if attributes end def self.columns() @columns ||= []; end def self.column(name, sql_type = nil, default = nil, null = true) columns << ActiveRecord::ConnectionAdapters::Column.new( name.to_s, default, sql_type.to_s, null) end # Because super does not process id attribute (see note in initialize). def attributes=(attributes) attributes.each { |k,v| self[k] = v } end end 8<----------------------------------------------------- Derive a concrete table-less model with something like, for example: class ContactSearch < TransientRecord column :id, :integer column :first_name, :string column :last_name, :string column :from, :date column :to, :date validates_numericality_of :id, :allow_nil => true : end Use it in your controller with something like, for example: @contact_search = ContactSearch.new(params[:contact_search]) if not @contact_search.valid? render :action => ''search'' else : Cheers, Stuart -- Stuart Rackham -- Posted via http://www.ruby-forum.com/.