Created
December 3, 2015 13:21
-
-
Save dwaller/5474304cfea354a9701d 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
require 'model_attribute' | |
module ActiveRecordMimic | |
def self.included(base) | |
base.instance_eval do | |
unless (class << base; self; end).ancestors.include?(ModelAttribute) | |
raise "ActiveRecordMimic aliases ModelAttribute methods so must be included after extending ModelAttribute" | |
end | |
alias_method :[], :read_attribute | |
alias_method :[]=, :write_attribute | |
# Forward these behaviours to base.class e.g Message.api_class | |
delegate :api_class, | |
:sti_class, | |
:base_class, | |
:to => base | |
def self.sti_class | |
self | |
end | |
def self.base_class | |
self | |
end | |
def self.primary_key | |
:id | |
end | |
end | |
end | |
def destroyed? | |
!!@destroyed | |
end | |
def new_record? | |
!id | |
end | |
def persisted? | |
!(new_record? || destroyed?) | |
end | |
# This allows us to use this object as a hash key, and for uniq'ing arrays | |
def hash | |
id.hash | |
end | |
# Action Pack uses this for constructing a URL to this object. | |
def to_param | |
id && id.to_s | |
end | |
def update_attribute(attr_name, new_val) | |
write_attribute(attr_name, new_val) | |
save | |
end | |
def changed? | |
changes.any? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment