Skip to content
This repository was archived by the owner on Apr 17, 2018. It is now read-only.
This repository was archived by the owner on Apr 17, 2018. It is now read-only.

assumes serial primary keys #6

Description

@alexkalderimis

If you try to version a resource that does not user a serial primary key, you run into integrity issues due to the tight coupling between property option values and the construction of the primary key (https://github.com/datamapper/dm-is-versioned/blob/master/lib/dm-is-versioned/is/versioned.rb#L88). If a natural key is used, then the original key is ignored in the version table.

There is no method used for the consuming class to override this behaviour - this could be easily remedied by extracting the property argument creation logic to a new method that can be overrriden (https://github.com/datamapper/dm-is-versioned/blob/master/lib/dm-is-versioned/is/versioned.rb#L78-L88), eg:

module ClassMethods

  def define_version_property(property)
    type = case property
      when DataMapper::Property::Discriminator then Class
      when DataMapper::Property::Serial        then Integer
    else
      property.class
    end
    options = property.options.merge(:key => property.name == @on)
    options[:key] = true if options.delete(:serial)
    return property.name, type, options
  end

  def const_missing(name)
    if name == :Version
      model = DataMapper::Model.new(name, self)
      properties.each do |property|
        name, type, options = define_version_property property
        model.property(property.name, type, options)
      end
      model
    else
      super
    end
  end

end # ClassMethods

And then the consumer need only override define_version_property to adjust the options hash

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions