Sha256: 05167611397e0d721eb78dd4d9284335b9218646cbdaa6bb5270d32896c82b8e

Contents?: true

Size: 1.09 KB

Versions: 15

Compression:

Stored size: 1.09 KB

Contents

module ActiveRecord
  module Timestamp
    unless method_defined? :touch
      # Saves the record with the updated_at/on attributes set to the current time.
      # If the save fails because of validation errors, an ActiveRecord::RecordInvalid exception is raised.
      # If an attribute name is passed, that attribute is used for the touch instead of the updated_at/on attributes.
      #
      # Examples:
      #
      # product.touch # updates updated_at
      # product.touch(:designed_at) # updates the designed_at attribute
      def touch(attribute = nil)
        current_time = current_time_from_proper_timezone

        if attribute
          write_attribute(attribute, current_time)
        else
          write_attribute('updated_at', current_time) if respond_to?(:updated_at)
          write_attribute('updated_on', current_time) if respond_to?(:updated_on)
        end

        save!
      end
    end

    unless method_defined? :current_time_from_proper_timezone
      def current_time_from_proper_timezone
        self.class.default_timezone == :utc ? Time.now.utc : Time.now
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
programmable-ventouse-0.0.10 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.2 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.3 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.4 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.5 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.7 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.8 lib/ventouse/ar_touch.rb
programmable-ventouse-0.0.9 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.0 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.1 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.2 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.3 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.4 lib/ventouse/ar_touch.rb
programmable-ventouse-0.1.5 lib/ventouse/ar_touch.rb
ventouse-0.1.6 lib/ventouse/ar_touch.rb