Skip to content

Instantly share code, notes, and snippets.

@adambair
Created September 20, 2008 02:35
Show Gist options
  • Save adambair/11700 to your computer and use it in GitHub Desktop.
Save adambair/11700 to your computer and use it in GitHub Desktop.
module Footnoted
def self.included(base)
base.extend(ClassMethods)
has_footnote
end
module ClassMethods
def has_footnote
instance_eval do
include InstanceMethods
end
has_many :annotations, :as => :notable, :dependent => :delete_all
has_one :footnote, :through => :annotations
before_save :save_footnote
# before_destroy :destroy_annotations
attr_accessor :footnote_id
end
end
module InstanceMethods
def save_footnote
puts @footnote_id
return if (@footnote_id.blank? || self.id.blank?)
Annotation.delete_all "footnote_id = #{@footnote_id} and notable_id = #{self.id}"
note = Footnote.find_by_id(@footnote_id)
Annotation.create(:notable => self, :footnote => note) unless note.nil?
end
def footnote_id
self.footnote ? self.footnote.id : nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment