Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.