Sha256: 51dfbeea9498e0002e7eccee96b59f7e713dba4fdb1c517735cf44c79e376eea

Contents?: true

Size: 1.59 KB

Versions: 6

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

module ROM
  module Plugins
    module Schema
      # A plugin for automatically adding timestamp fields
      # to the schema definition
      #
      # @example
      #   schema do
      #     use :timestamps
      #   end
      #
      #   # using non-default names
      #   schema do
      #     use :timestamps, attributes: %i(created_on updated_on)
      #   end
      #
      #   # using other types
      #   schema do
      #     use :timestamps, type: Types::Date
      #   end
      #
      # @api public
      module Timestamps
        DEFAULT_TIMESTAMPS = %i(created_at updated_at).freeze

        # @api private
        def self.apply(schema, options)
          type = options.fetch(:type, Types::Time)
          names = options.fetch(:attributes, DEFAULT_TIMESTAMPS)
          attributes = names.map do |name|
            ROM::Schema.build_attribute_info(
              type.meta(source: schema.name),
              name: name
            )
          end

          schema.attributes.concat(
            schema.class.attributes(attributes, schema.attr_class)
          )
        end

        # @api private
        module DSL
          # Sets non-default timestamp attributes
          #
          # @example
          #   schema do
          #     use :timestamps
          #     timestamps :create_on, :updated_on
          #   end
          #
          # @api public
          def timestamps(*names)
            options = plugin_options(:timestamps)
            options[:attributes] = names unless names.empty?

            self
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rom-core-5.1.2 lib/rom/plugins/schema/timestamps.rb
rom-core-5.1.1 lib/rom/plugins/schema/timestamps.rb
rom-core-5.1.0 lib/rom/plugins/schema/timestamps.rb
rom-core-5.0.2 lib/rom/plugins/schema/timestamps.rb
rom-core-5.0.1 lib/rom/plugins/schema/timestamps.rb
rom-core-5.0.0 lib/rom/plugins/schema/timestamps.rb