DZone 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

Snippets has posted 5883 posts at DZone. View Full User Profile

Non Persistent ActiveRecord

04.27.2006
| 5869 views |
  • submit to reddit
        
# = 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

Snippets Manager replied on Tue, 2006/08/29 - 1:45pm

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.

Snippets Manager replied on Thu, 2006/04/27 - 4:18pm

I had always wondered if there was a simple way to do this. Thanks so much!