Sha256: 39dbad8a6f26dd10529a5f8018dce6aadef8de10cbb37690bef0cd55522c67db

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

module DataMapper
  module Extensions
    
    module ActiveRecordImpersonation
      
      def self.included(base)
        base.extend(ClassMethods)
      end
      
      def save
        session.save(self)
      end
      
      def reload!
        session.find(self.class, id, :select => session.mappings[self.class].columns.map(&:name), :reload => true)
      end
      
      def reload
        reload!
      end
      
      def destroy!
        session.destroy(self)
      end
      
      module ClassMethods
        
        def all(options = {}, &b)
          find(:all, options, &b)
        end
        
        def first(options = {}, &b)
          find(:first, options, &b)
        end
        
        def delete_all
          database.delete_all(self)
        end
        
        def truncate!
          database.truncate(self)
        end
        
        def find(*args, &b)
          DataMapper::database.find(self, *args, &b)
        end
        
        def find_by_sql(*args)
          DataMapper::database.query(*args)
        end
        
        def [](id_or_hash)
          if id_or_hash.kind_of?(Hash)
            find(:first, id_or_hash)
          else
            find(id_or_hash)
          end
        end
        
        def create(attributes)
          instance = self.new(attributes)
          instance.save
          instance
        end
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
datamapper-0.1.0 lib/data_mapper/extensions/active_record_impersonation.rb