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

assumes serial primary keys #6

Open
alexkalderimis opened this issue Oct 14, 2013 · 0 comments
Open

assumes serial primary keys #6

alexkalderimis opened this issue Oct 14, 2013 · 0 comments

Comments

@alexkalderimis
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant