Created
July 9, 2015 07:09
-
-
Save cblunt/ee25065c1f512ae6066a to your computer and use it in GitHub Desktop.
Revisions
-
cblunt created this gist
Jul 9, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ class Model < ActiveRecord::Base include UuidConcern # uuid attribute is `:uuid` by default. Optionally pass in attribute name, e.g `has_uuid :unique_token` has_uuid end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ module UuidConcern extend ActiveSupport::Concern included do def self.has_uuid(attribute: :uuid) # Load securerandom only when has_secure_token is used. require 'active_support/core_ext/securerandom' define_method("regenerate_#{attribute}") { update! attribute => self.class.generate_uuid } before_create { self.send("#{attribute}=", self.class.generate_uuid) unless self.send("#{attribute}?")} end private def self.generate_uuid SecureRandom.uuid end end end