Forum: Ruby on Rails validations without AR - going crazy trying to find link

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-01 18:26
(Received via mailing list)
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
688da81a2cba50707fc9de6866c43938?d=identicon&s=25 David Blundell (Guest)
on 2006-03-01 18:39
(Received via mailing list)
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-01 18:58
(Received via mailing list)
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:

>   def self.human_attribute_name(arg)
>     attributes.each do |key, value|
>     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
67b6389be42524fbd776e44fd35c3d7e?d=identicon&s=25 Peter Donald (Guest)
on 2006-03-02 00:21
(Received via mailing list)
Hi,

It may have been the following entry;

http://www.realityforge.org/articles/2005/12/02/va...

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
27c170f482104299af279902be0a9c26?d=identicon&s=25 Trevor Squires (Guest)
on 2006-03-02 03:55
(Received via mailing list)
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
E74a88a96ce8caecbc7d265e4763e26c?d=identicon&s=25 Stuart Rackham (Guest)
on 2006-03-02 04:54
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.com

I 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/vali...
#
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
This topic is locked and can not be replied to.