Module: EncodedId::Rails::Model

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

Overview

Main module for adding encoded ID functionality to ActiveRecord models.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#encoded_id_memoized_with_idObject

: untyped



65
66
67
# File 'lib/encoded_id/rails/model.rb', line 65

def encoded_id_memoized_with_id
  @encoded_id_memoized_with_id
end

Class Method Details

.included(base) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/encoded_id/rails/model.rb', line 27

def self.included(base)
  base.extend(EncoderMethods)
  base.extend(FinderMethods)
  base.extend(QueryMethods)
  base.extend(ClassMethods)

  # Conditionally include PathParam based on global configuration
  if EncodedId::Rails.configuration.model_to_param_returns_encoded_id
    base.include(EncodedId::Rails::PathParam)
  end
end

Instance Method Details

#annotation_for_encoded_idObject

By default the annotation is the model name (it will be parameterized)

Raises:

  • (StandardError)


130
131
132
133
134
# File 'lib/encoded_id/rails/model.rb', line 130

def annotation_for_encoded_id
  name = self.class.name
  raise StandardError, "The default annotation requires the model class to have a name" if name.nil?
  name.underscore
end

#check_and_clear_memoizationObject



76
77
78
# File 'lib/encoded_id/rails/model.rb', line 76

def check_and_clear_memoization
  clear_encoded_id_cache! if encoded_id_memoized_with_id && encoded_id_memoized_with_id != id
end

#clear_encoded_id_cache!Object



68
69
70
71
72
73
# File 'lib/encoded_id/rails/model.rb', line 68

def clear_encoded_id_cache!
  [:@encoded_id_hash, :@encoded_id, :@slugged_encoded_id].each do |var|
    remove_instance_variable(var) if instance_variable_defined?(var)
  end
  self.encoded_id_memoized_with_id = nil
end

#dupObject

When duplicating an ActiveRecord object, we want to reset the memoized encoded IDs



144
145
146
147
148
# File 'lib/encoded_id/rails/model.rb', line 144

def dup
  super.tap do |new_record|
    new_record.clear_encoded_id_cache!
  end
end

#encoded_idObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/encoded_id/rails/model.rb', line 91

def encoded_id
  return unless id
  check_and_clear_memoization
  return @encoded_id if defined?(@encoded_id)

  encoded = encoded_id_hash
  config = EncodedId::Rails.configuration
  annotated_by = config.annotation_method_name
  return @encoded_id = encoded unless annotated_by && encoded

  separator = config.annotated_id_separator
  self.encoded_id_memoized_with_id = id
  @encoded_id = EncodedId::Rails::AnnotatedId.new(id_part: encoded, annotation: send(annotated_by.to_s), separator: separator).annotated_id
end

#encoded_id_hashObject



81
82
83
84
85
86
87
88
# File 'lib/encoded_id/rails/model.rb', line 81

def encoded_id_hash
  return unless id
  check_and_clear_memoization
  return @encoded_id_hash if defined?(@encoded_id_hash)

  self.encoded_id_memoized_with_id = id
  @encoded_id_hash = self.class.encode_encoded_id(id)
end

#name_for_encoded_id_slugObject

By default, trying to generate a slug without defining how will raise. You either override this method per model, pass an alternate method name to #slugged_encoded_id or setup an alias to another model method in your ApplicationRecord class

Raises:

  • (StandardError)


139
140
141
# File 'lib/encoded_id/rails/model.rb', line 139

def name_for_encoded_id_slug
  raise StandardError, "You must define a method to generate the slug for the encoded ID of #{self.class.name}"
end

#reload(options = nil) ⇒ Object



123
124
125
126
127
# File 'lib/encoded_id/rails/model.rb', line 123

def reload(options = nil)
  result = super
  clear_encoded_id_cache!
  result
end

#slugged_encoded_idObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/encoded_id/rails/model.rb', line 107

def slugged_encoded_id
  return unless id
  check_and_clear_memoization
  return @slugged_encoded_id if defined?(@slugged_encoded_id)

  config = EncodedId::Rails.configuration
  with = config.slug_value_method_name
  separator = config.slugged_id_separator
  encoded = encoded_id
  return unless encoded

  self.encoded_id_memoized_with_id = id
  @slugged_encoded_id = EncodedId::Rails::SluggedId.new(id_part: encoded, slug_part: send(with.to_s), separator: separator).slugged_id
end