Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

non persistent ActiveRecord (See related posts)

# = ActiveForm - non persistent ActiveRecord
#
# Simple base class to make AR objects without a corresponding database
# table.  These objects can still use AR validations but can't be saved
# to the database.  Use the +valid?+ method to validate.
#
# == Example
#
#   class FeedbackForm < ActiveForm
#     column :email
#     column :message
#     validates_presence_of :email, :message
#   end
#
class ActiveForm < ActiveRecord::Base
  def self.columns() @columns ||= []; end # :nodoc:
  
  # Define an attribute, takes the same arguments as
  # ActiveRecord::ConnectionAdapters::Column.new only in a
  # slightly different order.
  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
end

Comments on this post

jman posts on Apr 27, 2006 at 16:19
I had always wondered if there was a simple way to do this. Thanks so much!
paolo.dona posts on Aug 29, 2006 at 14:02
I would add a line like this:

def self.abstract_class; true; end

Not adding this line breakes the great ParagDave's annotate_model plugin.

You need to create an account or log in to post comments to this site.


Click here to browse all 4863 code snippets

Related Posts