Created
December 1, 2010 07:01
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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