Sha256: 1c35da4168169ad6986501242d32bc7bcb7c9f9f002181b8db28a14237ad6f40

Contents?: true

Size: 926 Bytes

Versions: 2

Compression:

Stored size: 926 Bytes

Contents

module LogBook::Plugin
  def self.included(base)
    base.send :extend, ClassMethods
    base.send :include, InstanceMethods
  end

  module ClassMethods

    def log_book(opts = {})
      after_create :log_book_event_on_create
      after_update :log_book_event_on_update
      before_destroy :log_book_event_on_destroy

      has_many :log_book_events, :class_name => "LogBook::Event", :as => :historizable, :dependent => (opts[:dependent] || :nullify)

      attr_accessor :log_book_historian
    end
  end

  module InstanceMethods
    def log_book_event_on_create
      LogBook.created(self.log_book_historian, self)
    end

    def log_book_event_on_update
      LogBook.updated(self.log_book_historian, self) unless changes.empty?
    end

    def log_book_event_on_destroy
      LogBook.destroyed(self.log_book_historian, self)
    end
  end
end

ActiveSupport.on_load(:active_record) do
  include LogBook::Plugin
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
log_book-0.1.7 lib/log_book/plugin.rb
log_book-0.1.1 lib/log_book/plugin.rb