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
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/has_public_id/activerecord/mixin.rb', line 22
def has_public_id(attribute_name, *args)
return if respond_to?(:public_id_attribute)
options = args.
class_attribute :public_id_attr, :public_id_options
class << self
def initialize_public_ids!
self.where(self.public_id_attr => nil).find_each do |obj|
obj.update_attribute(self.public_id_attr, self.new_public_id)
end
end
def find_by_public_id(public_id)
where(self.public_id_attr => public_id).first
end
def find_by_public_id!(public_id)
if public_id.blank?
raise ::ActiveRecord::RecordNotFound.new("Couldn't find #{self.name} without an ID")
end
where(self.public_id_attr => public_id).first or
raise ::ActiveRecord::RecordNotFound.new("Couldn't find #{self.name} with #{public_id}")
end
def new_public_id
new_id = ::HasPublicId::Util.new_public_id(self, self.public_id_options)
while(self.public_id_options[:check_unique])
break unless where(self.public_id_attr => new_id).exists?
new_id = ::HasPublicId::Util.new_public_id(self, self.public_id_options)
end
return new_id
end
end
self.public_id_attr = attribute_name
self.public_id_options = options
include ::HasPublicId::ActiveRecord::InstanceMethods
after_initialize :initialize_public_id
end
|