Module: Nifty::Utils::ActiveRecord::RandomString::ModelExtensions
- Defined in:
- lib/nifty/utils/active_record/random_string.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.included(base) ⇒ Object
39 40 41 42 |
# File 'lib/nifty/utils/active_record/random_string.rb', line 39 def self.included(base) base.extend ClassMethods base.before_validation :generate_random_strings end |
Instance Method Details
#generate_random_strings ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/nifty/utils/active_record/random_string.rb', line 44 def generate_random_strings self.class.random_string_fields.each do |field, opts| if self.send(field).blank? if opts[:unique] until self.send(field) proposed_string = RandomString.random_string(opts[:type], opts) unless self.class.where(field => proposed_string).exists? self.send("#{field}=", proposed_string) end end else self.send("#{field}=", RandomString.random_string(opts[:type], opts)) end end end end |