Created
March 25, 2011 16:42
-
-
Save lmmendes/887162 to your computer and use it in GitHub Desktop.
Rails 3 tableless model implementation
This file contains hidden or 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
# based on a Ryan Bates article http://railscasts.com/episodes/219-active-model | |
class Tableless | |
include ActiveModel::Validations | |
include ActiveModel::Conversion | |
extend ActiveModel::Naming | |
def self.attr_accessor(*vars) | |
@attributes ||= [] | |
@attributes.concat( vars ) | |
super | |
end | |
def self.attributes | |
@attributes | |
end | |
def initialize(attributes={}) | |
attributes && attributes.each do |name, value| | |
send("#{name}=", value) if respond_to? name.to_sym | |
end | |
end | |
def persisted? | |
false | |
end | |
def self.inspect | |
"#<#{ self.to_s} #{ self.attributes.collect{ |e| ":#{ e }" }.join(', ') }>" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment