Created
May 8, 2012 02:14
-
-
Save swalberg/2632035 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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