Sha256: 75d8ba397706eff9922c0a7d7f5810f173b0020feecb75e8e8fc3e8c91a69b0e

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require 'active_record'
require 'fingerprints/active_record'
require 'fingerprints/version'

module Fingerprints
  module Extensions

    OPTIONS = {
      :class_name => 'User'
    }

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods

      def has_fingerprints(options = {})
        options.reverse_merge!(OPTIONS)

        class_eval <<-"EOV"
          class << self
            def fingerprint
              Thread.current["fingerprint_for_#{self.class}"]
            end
            def fingerprint=(val)
              Thread.current["fingerprint_for_#{self.class}"] = val
            end
          end
        EOV
      end

      def leaves_fingerprints(options = {})
        options.reverse_merge!(OPTIONS)

        include Fingerprints::Extensions::InstanceMethods

        belongs_to :creator, :class_name => options[:class_name], :foreign_key => 'created_by'
        belongs_to :updater, :class_name => options[:class_name], :foreign_key => 'updated_by'
        before_create :fingerprint_created_by
        before_update :fingerprint_updated_by
        define_method('fingerprint_created_by') {|*args| set_fingerprint_for(:created_by, options) }
        define_method('fingerprint_updated_by') {|*args| set_fingerprint_for(:updated_by, options) }
      end
    end 

    module InstanceMethods
      def set_fingerprint_for(field, options = {})
        klass = options[:class_name].constantize
        raise(NoMethodError, "HasFingerprints for #{self.class} expected #{options[:class_name]} to respond to :fingerprint") unless klass.respond_to? :fingerprint
        value = klass.fingerprint
        value = value.id if value.is_a? klass
        self.send("#{field}=", value)
      end
      protected :set_fingerprint_for
    end

  end
end

ActiveRecord::Base.send :include, Fingerprints::Extensions

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fingerprints-0.0.1 lib/fingerprints.rb