amco / dolly

Not an ORM for CouchDB in rails.
8 stars 3 forks source link

Add slugable documents #185

Closed javierg closed 2 years ago

javierg commented 2 years ago

Documents with slug are the ones that inherit the id from a property or a list of properties. This ensure uniqueness in the property or set of properties defined as slug, it is a cheap way to make uniq indexes.

To do a document slugable you will include Dolly::Slugable into the document definition

class Foo < Dolly::Document
  include Dolly::Slugable

This will add a callback call set_slug which will run after the initial document initialization. One catch with this is that once the initial slug is defined, the id wont change. So any after change to the slugable value will not update the document id. The callback is added like

set_slug :set_default_id, unless: :persisted?

You also need to define a method with the slugable properties

def slugable_properties
  %i[email]
end

This code will set the email as slug.

All this code is mimicing what we do on implemented dolly projects and they should be supported with this code.

Another thing is that dolly is a non rails gem, so it doesn't have ActiveSupport dependencies, some of our implementations uses parametrized to set the slug id, this still works, but if dolly is added into a none rails project, the id will not be parameterized.