Sha256: 0ae595dc813daad42652f6275cc2ca55b1f9275e66a48d409920ca6fe79ca79c

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

module ActiveMetadata

  CONFIG = File.exists?('config/active_metadata.yml') ? YAML.load_file('config/active_metadata.yml')[Rails.env] : {}
 
  ## Define ModelMethods
  module Base
    
    require 'mongoid' if ActiveMetadata::CONFIG['persists_with'] == 'mongoid'
    require 'mongoid_paperclip' if ActiveMetadata::CONFIG['persists_with'] == 'mongoid'
    require 'paperclip' if ActiveMetadata::CONFIG['persists_with'] == 'active_record'
    require "active_metadata/persistence/persistence"

    def self.included(klass)
      klass.class_eval do
        extend Config
      end
    end

    module Config

      def acts_as_metadata *args
        after_save :save_history
        
        class_variable_set("@@metadata_id_from", args.empty? ? nil : args[0][:metadata_id_from])

        include ActiveMetadata::Base::InstanceMethods
        include ActiveMetadata::Persistence   
             
      end

    end

    module InstanceMethods

      def metadata_id
        metadata_id_from = self.class.class_variable_get("@@metadata_id_from")
        return self.id if metadata_id_from.nil?
        receiver = self
        metadata_id_from.each do |item|
          receiver = receiver.send item
        end
        receiver.id
      end
      
      def current_user_id
        if User.respond_to?(:current) && !User.current.nil?
            User.current.id
        else
          nil
        end      
      end  
                
    end # InstanceMethods
  end

end

::ActiveRecord::Base.send :include, ActiveMetadata::Base

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_metadata-0.1.9 lib/active_metadata/base.rb