Sha256: 4237ccd625be270147c1299ef45583dac8212fe91779e9de615c001746fcf7a4

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

# encoding: utf-8

module Mongoid
  module Document
    module Touch
      extend ActiveSupport::Concern
      
      included do
      end
      
      module InstanceMethods
      
        def touch( at_field = nil )
          unless self.frozen?
            to_touch = at_field || :updated_at
            if self.fields.include? to_touch.to_s
              return true if self.update_attribute(to_touch.to_sym, Time.now.utc)
              false
            else
              return false
            end
          else
            false
          end
        end
        
        def touch!( at_field = nil, force_fail = false )
          raise Errors::DocumentNotUpdated.new(self) if force_fail
          unless self.frozen?
            to_touch = at_field || :updated_at
            if self.fields.include? to_touch.to_s              
              return true if self.update_attribute(to_touch.to_sym, Time.now.utc)
              raise Errors::DocumentNotUpdated, self
            else
              raise Errors::MissingField.new(to_touch, self)
            end
          else
            raise Errors::FrozenInstance, self
          end
        end
        
      end
    
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid_touch-0.1.0 lib/mongoid/document/touch.rb