Sha256: def243996d84c03c94e96ec5def472d2e3b13de972d729cf504332a6669c284f

Contents?: true

Size: 858 Bytes

Versions: 4

Compression:

Stored size: 858 Bytes

Contents

module MotionPrime
  module ModelTimestampsMixin
    extend ::MotionSupport::Concern

    def self.included(base)
      base.class_attribute :_timestamp_attributes
    end

    def save!
      time = Time.now
      trigger_timestamp(:save, time)
      trigger_timestamp(:create, time) if new_record?
      super
    end

    def trigger_timestamp(action_name, time)
      field = (_timestamp_attributes || {})[action_name]
      return unless field
      self.send(:"#{field}=", time)
    end

    module ClassMethods
      def timestamp_attributes(actions = nil)
        self._timestamp_attributes ||= {}
        actions ||= {save: :saved_at, create: :created_at}
        actions.each do |action_name, field|
          self._timestamp_attributes[action_name.to_sym] = field
          self.attribute field, type: :time
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
motion-prime-1.0.7 motion-prime/models/_timestamps_mixin.rb
motion-prime-1.0.6 motion-prime/models/_timestamps_mixin.rb
motion-prime-1.0.5 motion-prime/models/_timestamps_mixin.rb
motion-prime-1.0.4 motion-prime/models/_timestamps_mixin.rb