I recently upgraded WhatYouAte.com to Rails 2.2.2. I had been using advice from the Rails Wiki’s HowToUseValidationsWithoutExtendingActiveRecord page. I was using a class based on the RailsWeenie code (that site is down now) and it stopped working. Here’s a new replacement hack that works almost identically.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# Usage: in the faux-model class, include EphemeralModel and ActiveRecord::Validations # Then just say "column :foo" to set up a property called 'foo' for validation module EphemeralModel # Stubbing methods from ActiveRecord::Base that ActiveRecord::Validations may call def EphemeralModel.included(mod) def mod.self_and_descendents_from_active_record; [self]; end def mod.human_attribute_name(arg); arg.to_s; end def mod.human_name; self.name; end def mod.column(attr_name) attr_name = attr_name.to_s column_names.push(attr_name) unless column_names.include?(attr_name) attr_accessor attr_name end mod.module_eval do def self.column_names cn = self.instance_variable_get(:@column_names) if cn.nil? cn = self.instance_variable_set(:@column_names, []) end cn end end end def save; end def save!; end def update_attribute; end def new_record?; end def initialize(initial_attributes = nil) initial_attributes.each{|k,v| instance_variable_set("@#{k}".to_sym, v)} if initial_attributes end def attributes @attributes = {} self.class.column_names.each do |colname| @attributes[colname]= instance_variable_get("@#{colname}".to_sym) end @attributes end def attribute_names self.class.column_names end def [](name); attributes[name.to_s]; end def []=(name, value) instance_variable_set("@#{name}".to_sym, value) # print "set #{name} to " + instance_variable_get("@#{name}".to_sym) + ".\n" # DEBUG end end |
One thought on “EphemeralModel, for Rails 2.2.2 form validation without a DB table”