Module: EncodedId::Rails::Persists

Defined in:
lib/encoded_id/rails/persists.rb

Overview

Provides persistence of encoded IDs to database columns with validation and callbacks.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/encoded_id/rails/persists.rb', line 10

def self.included(base)
  base.extend ClassMethods

  base.validates :normalized_encoded_id, uniqueness: true, allow_nil: true
  base.validates :prefixed_encoded_id, uniqueness: true, allow_nil: true

  base.before_validation :prevent_update_of_normalized_encoded_id!, if: :normalized_encoded_id_changed?
  base.before_validation :prevent_update_of_prefixed_encoded_id!, if: :prefixed_encoded_id_changed?

  base.after_create :set_normalized_encoded_id!
  base.before_save :update_normalized_encoded_id!, if: :should_update_normalized_encoded_id?

  base.after_commit :check_encoded_id_persisted!, on: [:create, :update]
end

Instance Method Details

#check_encoded_id_persisted!Object

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/encoded_id/rails/persists.rb', line 85

def check_encoded_id_persisted!
  validate_id_for_encoded_id!

  klass = self.class
  klass_name = klass.name
  encoded_from_current_id = klass.encode_normalized_encoded_id(resolved_id)

  if normalized_encoded_id != encoded_from_current_id
    raise StandardError, "The persisted encoded ID #{normalized_encoded_id} for #{klass_name} is not the same as currently computing #{encoded_from_current_id}"
  end

  return if prefixed_encoded_id == encoded_id

  raise StandardError, "The persisted prefixed encoded ID (for #{klass_name} with id: #{id}, normalized_encoded_id: #{normalized_encoded_id}) is not correct: it is #{prefixed_encoded_id} instead of #{encoded_id}"
end

#dupObject

On duplication we need to reset the encoded ID to nil as this new record will have a new ID. We also prevent these changes from marking the record as dirty.



57
58
59
60
61
62
63
64
# File 'lib/encoded_id/rails/persists.rb', line 57

def dup
  copy = super
  copy.prefixed_encoded_id = nil
  copy.clear_prefixed_encoded_id_change
  copy.normalized_encoded_id = nil
  copy.clear_normalized_encoded_id_change
  copy
end

#prevent_update_of_normalized_encoded_id!Object

Raises:



112
113
114
# File 'lib/encoded_id/rails/persists.rb', line 112

def prevent_update_of_normalized_encoded_id!
  raise ActiveRecord::ReadonlyAttributeError, "You cannot update the normalized encoded ID '#{normalized_encoded_id}' of a record #{self.class.name} #{id}, if you need to refresh it use set_normalized_encoded_id!"
end

#prevent_update_of_prefixed_encoded_id!Object

Raises:



117
118
119
# File 'lib/encoded_id/rails/persists.rb', line 117

def prevent_update_of_prefixed_encoded_id!
  raise ActiveRecord::ReadonlyAttributeError, "You cannot update the prefixed encoded ID '#{prefixed_encoded_id}' of a record #{self.class.name} #{id}, if you need to refresh it use set_normalized_encoded_id!"
end

#resolved_idObject



67
68
69
70
71
# File 'lib/encoded_id/rails/persists.rb', line 67

def resolved_id
  validate_id_for_encoded_id!

  id #: Integer
end

#set_normalized_encoded_id!Object



74
75
76
# File 'lib/encoded_id/rails/persists.rb', line 74

def set_normalized_encoded_id!
  update_columns(normalized_encoded_id: self.class.encode_normalized_encoded_id(resolved_id), prefixed_encoded_id: encoded_id)
end

#should_update_normalized_encoded_id?Boolean

Returns:



102
103
104
# File 'lib/encoded_id/rails/persists.rb', line 102

def should_update_normalized_encoded_id?
  id_changed? || (normalized_encoded_id.blank? && persisted?)
end

#update_normalized_encoded_id!Object



79
80
81
82
# File 'lib/encoded_id/rails/persists.rb', line 79

def update_normalized_encoded_id!
  self.normalized_encoded_id = self.class.encode_normalized_encoded_id(resolved_id)
  self.prefixed_encoded_id = encoded_id
end

#validate_id_for_encoded_id!Object

Raises:



107
108
109
# File 'lib/encoded_id/rails/persists.rb', line 107

def validate_id_for_encoded_id!
  raise StandardError, "You cannot set the normalized ID of a record which is not persisted" if id.blank?
end