Module: GlobalUid::ActiveRecordExtension

Defined in:
lib/global_uid/active_record_extension.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
# File 'lib/global_uid/active_record_extension.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
  base.before_create :global_uid_before_create
end

Instance Method Details

#global_uid_before_createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/global_uid/active_record_extension.rb', line 10

def global_uid_before_create
  return if GlobalUid::Base.global_uid_options[:disabled]
  return if self.class.global_uid_disabled

  global_uid = nil
  realtime = Benchmark::realtime do
    global_uid = self.class.generate_uid
  end

  if GlobalUid::Base.global_uid_options[:dry_run]
    ActiveRecord::Base.logger.info("GlobalUid dry-run: #{self.class.name}\t#{global_uid}\t#{"%.4f" % realtime}")
    return
  end

  self.id = global_uid
end