Sha256: 2601f7cc306e4315c5d18f52991bf7d92d9b49ab948e2ebec555c5e9a1ec5186

Contents?: true

Size: 1020 Bytes

Versions: 1

Compression:

Stored size: 1020 Bytes

Contents

# encoding: utf-8

module Mongoid
  module Document
    module Touch
      extend ActiveSupport::Concern

      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

Version data entries

1 entries across 1 versions & 1 rubygems

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