equivalent / public_uid

Automatically generates random unique public id for record.
MIT License
76 stars 8 forks source link

UUID generator solution #13

Closed equivalent closed 5 years ago

equivalent commented 6 years ago

Think about how to implement UUID solution with this project

require 'securerandom' # standard Ruby lib
SecureRandom.uuid(4)
# => aueyoe92

It is possible to do something like this:

class Post < ActiveRecord::Base
 before_create :generate_random_id

 private 
 def generate_random_id(length = 4)
   self.id = SecureRandom.uuid(lenght)
 end 
end

(so we can create create custom string generator like this one https://github.com/equivalent/public_uid/blob/master/lib/public_uid/generators/range_string.rb%60 )

BUT rails 4 this should be automatic

create_table :posts, id: :uuid do |t|
  ...
end

So :thinking: how to merge this solution to public_uid project

Maybe just a readme note on UUID would be enough

equivalent commented 6 years ago
class AddTokenToUsers < ActiveRecord::Migration
  def change
    enable_extension 'uuid-ossp'
    add_column :users, :token, :uuid, default: 'uuid_generate_v4()'
  end
end
equivalent commented 5 years ago

ok after thinking about this this may not be the best approach for this gem. Core principle of the gem is the before_create hook that is setting the random string to public_uid column (and we use ORM to support as many DB solutions).

So yes the uuid coulumn is an option but it's more of a custom implementation to whoever wants it but it will lock the project only to one type of DB

And specially after implemetaion of SecureRandom.hex in v 1.3 this is not really needed https://github.com/equivalent/public_uid/issues/10