Sha256: 74d4676fe798d81de092e04a278221da931b2eae50aaa590c241d4808bbf5995

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

module Traka
  module IsTrakable
    extend ActiveSupport::Concern
 
    included do
    end
 
    module ClassMethods
      def is_trakable(options={})
        cattr_accessor :traka_uuid
        self.traka_uuid = (options[:traka_uuid] || :uuid).to_s

        before_create :set_uuid, :record_create
        before_update :record_update
        before_destroy :record_destroy

        include Traka::IsTrakable::LocalInstanceMethods
      end
    end

    module LocalInstanceMethods
      private

      def set_uuid
        write_attribute(self.class.traka_uuid, SecureRandom.hex(20))
      end

      def record_create
        record_traka_change("create")
      end

      def record_update
        record_traka_change("update")
      end

      def record_destroy
        record_traka_change("destroy")
      end

      def record_traka_change(action_type)
        Traka::Change.create(:klass => self.class.to_s,
                             :uuid => self.attributes[self.class.traka_uuid],
                             :action_type => action_type)
      end
    end
  end
end

ActiveRecord::Base.send :include, Traka::IsTrakable

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
traka-0.0.4 lib/traka/is_trakable.rb
traka-0.0.3 lib/traka/is_trakable.rb
traka-0.0.2 lib/traka/is_trakable.rb