Skip to content

Instantly share code, notes, and snippets.

@lmmendes
Created March 25, 2011 16:42
Show Gist options
  • Save lmmendes/887162 to your computer and use it in GitHub Desktop.
Save lmmendes/887162 to your computer and use it in GitHub Desktop.
Rails 3 tableless model implementation
# 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