Module: ActsAsHavingStringId::ClassMethods

Defined in:
lib/acts_as_having_string_id.rb

Instance Method Summary collapse

Instance Method Details

#_teaObject



41
42
43
44
# File 'lib/acts_as_having_string_id.rb', line 41

def _tea
  pass_phrase = name + Rails.application.secrets.string_id_key
  @_tea ||= ActsAsHavingStringId::TEA.new(pass_phrase)
end

#acts_as_having_string_id(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_having_string_id.rb', line 10

def acts_as_having_string_id(options = {})
  class_eval do
    attrib_type = ActsAsHavingStringId::StringId::Type.new(self)
    attribute :id, attrib_type

    self.reflections.each_value do |r|
      # Attribute all foreign keys pointing here as well
      unless r.is_a? ActiveRecord::Reflection::ThroughReflection
        r.klass.class_eval do
          attribute r.foreign_key.to_sym, attrib_type
        end
      end
    end

    def self.id_string(id)
      # Return the string representation of id
      _tea.encrypt(id).base62_encode
    end

    def self.id_int(id_string)
      # Return the id from a string representation
      begin
        id_int = id_string.base62_decode
      rescue
        return nil
      end
      _tea.decrypt(id_int)
    end
  end
end