Sha256: 31ff48aeb75f873bf3fe23edf1da3b57e052fa7dd175e471976b12c5813daa8f

Contents?: true

Size: 844 Bytes

Versions: 5

Compression:

Stored size: 844 Bytes

Contents

module Pupa
  module Concerns
    # Adds the Popolo `created_at` and `updated_at` properties to a model. The
    # `created_at` and `updated_at` properties will automatically be set when
    # the object is inserted into or updated in the database.
    module Timestamps
      extend ActiveSupport::Concern

      included do
        attr_accessor :created_at, :updated_at
        dump :created_at, :updated_at

        set_callback(:create, :before) do |object|
          object.created_at = Time.now.utc
        end

        set_callback(:save, :before) do |object|
          # The object may not set created_at.
          # @see https://github.com/jpmckinney/pupa-ruby/issues/17
          object.created_at = object.document['created_at'] if object.document
          object.updated_at = Time.now.utc
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
pupa-0.2.4 lib/pupa/models/concerns/timestamps.rb
pupa-0.2.3 lib/pupa/models/concerns/timestamps.rb
pupa-0.2.2 lib/pupa/models/concerns/timestamps.rb
pupa-0.2.1 lib/pupa/models/concerns/timestamps.rb
pupa-0.2.0 lib/pupa/models/concerns/timestamps.rb