A place where the Joyent community can gather, help each other out, and stay informed.
You are not logged in.
Pages: 1
I'm creating a form that people will fill in. I want to use Active Record style validation, e.g. validate_numericality and so on, but without having to create a model, and table in the database, as this this in only then going to be e-mailed using Action Mailer.
Any ideas? How would you do it? Or is creating a model, table, etc. the only route, I guess apart from sticking some regexps in the action method.
Thanks,
Roman
Last edited by applepro (2006-04-02 18:34:22)
Offline
I did this awihle back and duped Active Record into thinking there was a model:
ActiveRecord::Base.class_eval do
alias_method :save, :valid?
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, null)
end
end
class Wishlist < ActiveRecord::Base
# Add whichever columns you might need here, they will be mocked
# as ActiveRecord database rows.
COLUMNS = [ :id, :from, :from_name, :to, :body, :products ]
# Extension of AR::Base above lets me mock these database columns,
# allowing me to have AR goodness without a real database.
self.class_eval do
COLUMNS.each { |c| column("#{c}", :string) }
end
Offline
here is another option that i came across. don't remember from where. Add active_form.rb to your models dir.
# Note ".valid?" method must occur on object for validates_associated
class ActiveForm def initialize(attributes = nil) if attributes attributes.each do |key,value| send(key.to_s + '=', value) end end yield self if block_given? end def [](key) instance_variable_get("@#{key}") end def method_missing( method_id, *args ) if md = /_before_type_cast$/.match(method_id.to_s) attr_name = md.pre_match return self[attr_name] if self.respond_to?(attr_name) end super endprotected
def raise_not_implemented_error(*params) ValidatingModel.raise_not_implemented_error(*params) end def self.human_attribute_name(attribute_key_name) attribute_key_name.humanize end def new_record? true end # these methods must be defined before include alias save raise_not_implemented_error alias update_attribute raise_not_implemented_errorpublic
include ActiveRecord::Validationsprotected
# the following methods must be defined after include so that they overide # methods previously included alias save! raise_not_implemented_error class << self def raise_not_implemented_error(*params) raise NotImplementedError end alias validates_uniqueness_of raise_not_implemented_error alias create! raise_not_implemented_error alias validate_on_create raise_not_implemented_error alias validate_on_update raise_not_implemented_error alias save_with_validation raise_not_implemented_error end
endrequire 'dispatcher'
class << self if ! method_defined?(:form_original_reset_application!) alias :form_original_reset_application! :reset_application! def reset_application! form_original_reset_application! Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm) end end end
class Dispatcher
end
then here is how you use it:
class ContactEmail < ActiveForm
attr_accessor :email, :subject, :body
validates_presence_of :email, :subject, :body
validates_format_of :email,
:with => /[-!#$&'*+\/=?`{|}~.\w]+@[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*(\.[a-zA-Z0-9]([-a-zA-Z0-9]*[a-zA-Z0-9])*)+$/,
:message => ' appears to be invalid'
end
Offline
This answered the exact same question I had. Thanks a bunch.
The only thing, the second solution works great except for one part.
require 'dispatcher'
class Dispatcher class << self if ! method_defined?(:form_original_reset_application!) alias :form_original_reset_application! :reset_application! def reset_application! form_original_reset_application! Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm) end end endend
I have no clue what this does. I had to comment it out since it seemed to screw up my rails app. With it after something referenced the ActiveForm object my rails app would no longer respond to valid controller actions.
Anyways thanks for the help.
Offline
Pages: 1