Skip to content

Instantly share code, notes, and snippets.

@swalberg
Created May 8, 2012 02:14
Show Gist options
  • Save swalberg/2632035 to your computer and use it in GitHub Desktop.
Save swalberg/2632035 to your computer and use it in GitHub Desktop.
module ActsAsHistorical
extend ActiveSupport::Concern
included do
end
module ClassMethods
def acts_as_historical(options = {})
has_many :histories, :as => :historical, :dependent => :destroy
include MyInstanceMethods
end
end
module MyInstanceMethods
module ArrayProxy
def <<(entry)
@__parent__.add_history(entry)
super
end
end
def add_history(text)
histories.create(:entry => text)
end
def clear_history
histories.destroy_all
end
def history
histories.map(&:entry).tap { |h| h.extend(ArrayProxy).instance_variable_set(:@__parent__, self) }
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment