Skip to content

Instantly share code, notes, and snippets.

@austinthecoder
Created March 21, 2012 16:15
Show Gist options
  • Save austinthecoder/2149170 to your computer and use it in GitHub Desktop.
Save austinthecoder/2149170 to your computer and use it in GitHub Desktop.
class Post
extend ActiveModel::Naming
include ActiveModel::Validations
include ActiveModel::Conversion
validates :title, :presence => true
def self.find(id)
new.tap { |l| l.record = self::Record.find(id) }
end
def initialize(attrs = {})
self.attributes = attrs
end
attr_writer :record
delegate :id, :title, :title=, :persisted?, :to_param, :destroy, :to => :record
def save
valid? && record.save! && true
end
def attributes=(attrs = {})
attrs.each { |k, v| self.public_send "#{k}=", v } if attrs
end
private
def record
@record ||= self::Record.new
end
class Record < ActiveRecord::Base
set_table_name :posts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment