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
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/acts_as_having_string_id.rb', line 10
def acts_as_having_string_id(options = {})
class_eval do
def self.acts_as_having_string_id?
true
end
attrib_type = ActsAsHavingStringId::StringId::Type.new(self)
attribute :id, attrib_type
self.reflections.each_value do |r|
if r.is_a?(ActiveRecord::Reflection::HasManyReflection)
r.klass.class_eval do
attribute r.foreign_key.to_sym, attrib_type
end
elsif r.is_a?(ActiveRecord::Reflection::BelongsToReflection)
if r.klass.respond_to?(:acts_as_having_string_id?) && \
r.klass.acts_as_having_string_id?
foreign_attrib_type = ActsAsHavingStringId::StringId::Type.new(r.klass)
attribute r.foreign_key.to_sym, foreign_attrib_type
end
end
end
def self.id_string(id)
_tea.encrypt(id).base62_encode
end
def self.id_int(id_string)
begin
id_int = id_string.base62_decode
rescue
return nil
end
_tea.decrypt(id_int)
end
end
end
|