How to make "virtual ActiveRecord" be short?


#1

use ActiveRecord::ConnectionAdapters::Column can make a “virtual
ActiveRecord”,but how to make it short or have it other method?

class EmailForm < ActiveRecord::Base
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.to_s, null)
end

column :title, :string
column :body, :string

validates_presence_of :title
validates_presence_of :body

end


#2

I’ve got a mixin that approaches this from the other angle:

http://djur.desperance.net/ruby/fake_active_record.rb

It implements enough of the AR interface to make most validations
work; human_attribute_name is implemented so error_messages_for works.
All you have to do is ‘include FakeActiveRecord’ and define the
attributes with attr_accessor.


#3

removed_email_address@domain.invalid wrote:

I’ve got a mixin that approaches this from the other angle:

http://djur.desperance.net/ruby/fake_active_record.rb

It implements enough of the AR interface to make most validations
work; human_attribute_name is implemented so error_messages_for works.
All you have to do is ‘include FakeActiveRecord’ and define the
attributes with attr_accessor.

thank you.the error_message does not show:(“can not be blank”)

model:
require “fake_active_record”
class Fake < ActiveRecord::Base
include FakeActiveRecord
attr_accessor :title
validates_presence_of :title,:message=>“can not be blank”
end

view:
<%=error_messages_for :fake%>
<%form_tag :action=>“fake” do %>
<%fields_for :fake do |f|%>
<%=f.text_field :title%><%=error_message_on(:fake,:title)%>
<%end%>
<%=submit_tag%>
<%end%>

controller:

def fake
#if Fake.new(params[:fake])
#else
render :action=>“fake_new”
#end
end

def fake_new
@fake = Fake.new
end