{"id":84,"date":"2008-11-29T17:13:05","date_gmt":"2008-11-29T23:13:05","guid":{"rendered":"http:\/\/www.pervasivecode.com\/blog\/?p=84"},"modified":"2008-11-29T17:22:41","modified_gmt":"2008-11-29T23:22:41","slug":"ephemeralmodel-for-rails-222-form-validation-without-a-db-table","status":"publish","type":"post","link":"http:\/\/www.pervasivecode.com\/blog\/2008\/11\/29\/ephemeralmodel-for-rails-222-form-validation-without-a-db-table\/","title":{"rendered":"EphemeralModel, for Rails 2.2.2 form validation without a DB table"},"content":{"rendered":"<p>I recently upgraded <a href=\"http:\/\/www.whatyouate.com\/\">WhatYouAte.com<\/a> to Rails 2.2.2. I had been using advice from the Rails Wiki&#8217;s <a href=\"http:\/\/wiki.rubyonrails.com\/rails\/pages\/HowToUseValidationsWithoutExtendingActiveRecord\">HowToUseValidationsWithoutExtendingActiveRecord<\/a> page. I was using a class based on the RailsWeenie code (that site is down now) and it stopped working. Here&#8217;s a new replacement hack that works almost identically.<br \/>\n<!--more--><\/p>\n<pre>\r\n# Usage: in the faux-model class, include EphemeralModel and ActiveRecord::Validations\r\n# Then just say \"column :foo\" to set up a property called 'foo' for validation\r\nmodule EphemeralModel\r\n    # Stubbing methods from ActiveRecord::Base that ActiveRecord::Validations may call\r\n    def EphemeralModel.included(mod)\r\n        def mod.self_and_descendents_from_active_record; [self]; end\r\n        def mod.human_attribute_name(arg); arg.to_s; end\r\n        def mod.human_name; self.name; end\r\n        def mod.column(attr_name)\r\n            attr_name = attr_name.to_s\r\n            column_names.push(attr_name) unless column_names.include?(attr_name)\r\n            attr_accessor attr_name\r\n        end\r\n        mod.module_eval do\r\n            def self.column_names\r\n                cn = self.instance_variable_get(:@column_names)\r\n                if cn.nil?\r\n                    cn = self.instance_variable_set(:@column_names, [])\r\n                end\r\n                cn\r\n            end\r\n        end\r\n    end\r\n    def save; end\r\n    def save!; end \r\n    def update_attribute; end\r\n    def new_record?; end\r\n\r\n\r\n    def initialize(initial_attributes = nil)\r\n        initial_attributes.each{|k,v| instance_variable_set(\"@#{k}\".to_sym, v)} if initial_attributes\r\n    end\r\n\r\n    def attributes\r\n        @attributes = {}\r\n        self.class.column_names.each do |colname|\r\n            @attributes[colname]= instance_variable_get(\"@#{colname}\".to_sym)\r\n        end\r\n        @attributes\r\n    end\r\n\r\n    def attribute_names\r\n        self.class.column_names\r\n    end\r\n    \r\n    def [](name); attributes[name.to_s]; end\r\n\r\n    def []=(name, value)\r\n        instance_variable_set(\"@#{name}\".to_sym, value)\r\n        # print \"set #{name} to \" + instance_variable_get(\"@#{name}\".to_sym) + \".\\n\" # DEBUG\r\n    end\r\nend\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I recently upgraded WhatYouAte.com to Rails 2.2.2. I had been using advice from the Rails Wiki&#8217;s HowToUseValidationsWithoutExtendingActiveRecord page. I was using a class based on the RailsWeenie code (that site is down now) and it stopped working. Here&#8217;s a new replacement hack that works almost identically.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,20,1],"tags":[],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-ruby","category-ruby-on-rails","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/84"}],"collection":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":0,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.pervasivecode.com\/blog\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}