Created
September 20, 2008 02:35
-
-
Save adambair/11700 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 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