Sha256: e5574bcf682ef40363e5d30d21be55b7b3ddffb382aeb47bae5313c52c98a078

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

# -*- encoding : utf-8 -*-
module Mongoid
  module Userstamp
    extend ActiveSupport::Concern

    autoload :Version, 'mongoid/userstamp/version'
    autoload :Railtie, 'mongoid/userstamp/railtie'
    autoload :Config, 'mongoid/userstamp/config'
    autoload :User, 'mongoid/userstamp/user'

    included do
      include Mongoid::Timestamps

      field Mongoid::Userstamp.configuration.updated_column, :type => Object
      field Mongoid::Userstamp.configuration.created_column, :type => Object

      before_save :set_updator
      before_create :set_creator

      define_method Mongoid::Userstamp.configuration.updated_accessor do
        Mongoid::Userstamp.configuration.user_model.find(self.send(Mongoid::Userstamp.configuration.updated_column))
      end

      define_method Mongoid::Userstamp.configuration.created_accessor do
        Mongoid::Userstamp.configuration.user_model.find(self.send(Mongoid::Userstamp.configuration.created_column))
      end

      protected
        def set_updator
          column = "#{Mongoid::Userstamp.configuration.updated_column.to_s}=".to_sym
          return unless Mongoid::Userstamp.configuration.user_model.respond_to? column

          self.send(column, Mongoid::Userstamp.configuration.user_model.current.try(:id))
        end

        def set_creator
          column = "#{Mongoid::Userstamp.configuration.created_column.to_s}=".to_sym
          return unless Mongoid::Userstamp.configuration.user_model.respond_to? column

          self.send(column, Mongoid::Userstamp.configuration.user_model.current.try(:id))
        end
    end

    class << self
      def configure(&block)
        @configuration = Mongoid::Userstamp::Config.new(&block)
      end

      def configuration
        @configuration ||= Mongoid::Userstamp::Config.new
      end 
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_userstamp-0.1.2 lib/mongoid/userstamp.rb