9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/tokenize_attributes/attribute.rb', line 9
def tokenized_attribute(attrb, method = nil)
self.send(:define_method, "reset_tokenization_for_#{attrb}") do
loop do
token = method.try(:call) || TokenizeAttributes.configuration.tokenizer.call
self.public_send("#{attrb}=", token)
break unless self.class.where(attrb => token).exists?
end
end
before_validation do |record|
record.public_send("reset_tokenization_for_#{attrb}") if record.public_send(attrb).blank?
end
end
|