Skip to content

Instantly share code, notes, and snippets.

@madpilot
Created December 1, 2010 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save madpilot/723095 to your computer and use it in GitHub Desktop.
Save madpilot/723095 to your computer and use it in GitHub Desktop.
Base Record - allows you to use validations without active model in rails 2.3.x
class BaseRecord
attr_accessor :values
def self.fields(fields)
BaseRecord.field_list = fields
end
def self.field_list=(field_list)
@field_list = field_list
end
def self.field_list
@field_list
end
def fields
BaseRecord.field_list
end
def initialize(params = {})
self.values = params
end
def method_missing(method_id, *params)
if method_id.id2name.last == "="
method = method_id.id2name[0..-2].intern
self.values[method] = params[0] if fields && fields.include?(method)
else
method = method_id.id2name.intern
return self.values[method] if fields && fields.include?(method)
end
end
def save
end
def save!
end
def update_attribute
end
def new_record?
true
end
def self.self_and_descendants_from_active_record
[self]
end
def self.human_name(options = {})
self.class.to_s.humanize
end
include ActiveRecord::Validations
def [](key)
return nil unless self.values
self.values[key]
end
def BaseRecord.human_attribute_name(attribute_key_name)
attribute_key_name.humanize
end
def attributes=(attributes)
return if attributes.nil?
attributes.stringify_keys!
attributes.each do |k, v|
send(k + "=", v)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment