Sha256: 75a58b531f85564cccf3d43a65642444be36aaf5014cc4d74cb4f66c867deb7b

Contents?: true

Size: 843 Bytes

Versions: 14

Compression:

Stored size: 843 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/opennorth/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

14 entries across 14 versions & 1 rubygems

Version Path
pupa-0.1.11 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.10 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.9 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.8 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.7 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.6 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.5 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.4 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.3 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.2 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.1 lib/pupa/models/concerns/timestamps.rb
pupa-0.1.0 lib/pupa/models/concerns/timestamps.rb
pupa-0.0.13 lib/pupa/models/concerns/timestamps.rb
pupa-0.0.12 lib/pupa/models/concerns/timestamps.rb