Created
March 21, 2012 16:15
-
-
Save austinthecoder/2149170 to your computer and use it in GitHub Desktop.
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 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