Sha256: b2eba21718c438c395349e034b8c08a34bb4e72db94070dbf06b198265ce426d

Contents?: true

Size: 1.83 KB

Versions: 5

Compression:

Stored size: 1.83 KB

Contents

#require 'dm-core'
module Ixtlan

  module ModifiedBy    
    extend ::DataMapper::Chainable

    MODIFIED_BY_PROPERTIES = {
      :updated_by => lambda {|r, u| r.updated_by = u},
      :created_by => lambda {|r, u| r.created_by = u if r.new? }
    }.freeze

    def self.included(model)
      model.after :save do @current_user = nil; end
      model.extend(ClassMethods)
    end

    def current_user=(user)
      @current_user = user
    end

    private

    def current_user
      raise ::DataMapper::MissingCurrentUserError.new("current_user not set. it can be set like any other property") unless @current_user
      
      @current_user
    end

    chainable do
      def new(attributes = {}, &block)
        current_user= attributes.delete(:current_user)
        super(&block)
      end
    end

    chainable do
      def save(*args)
        set_modified_by if dirty?
        super()
      end
    end

    chainable do
      def update!(*args)
        set_modified_by if dirty?
        super()
      end
    end

    def set_modified_by
      MODIFIED_BY_PROPERTIES.each do |name, setter|
        if respond_to? name
          setter.call(self, current_user)
        end
      end
    end

    module ClassMethods
      def modified_by(type, *names)
        if(names.empty?)
          modified_by(type, :created_by, :updated_by)
        else
          names.each do |name|
            case name
            when *MODIFIED_BY_PROPERTIES.keys
              belongs_to name, :model => type.to_s
            else
              raise ::DataMapper::InvalidModifiedByName, "Invalid 'modified by' name '#{name}'"
            end
          end
        end
      end
    end

    class ::DataMapper::InvalidModifiedByName < RuntimeError; end
    class ::DataMapper::MissingCurrentUserError < RuntimeError; end

    ::DataMapper::Model.append_inclusions self
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ixtlan-0.2.4 lib/ixtlan/modified_by.rb
ixtlan-0.2.3 lib/ixtlan/modified_by.rb
ixtlan-0.2.2 lib/ixtlan/modified_by.rb
ixtlan-0.2.1 lib/ixtlan/modified_by.rb
ixtlan-0.2.0 lib/ixtlan/modified_by.rb