lib/auth_helpers.rb in josevalim-auth_helpers-0.1.2 vs lib/auth_helpers.rb in josevalim-auth_helpers-0.2.0

- old
+ new

@@ -1,34 +1,22 @@ module AuthHelpers - # Helper that find or initialize an object by attribute only if the given value is not blank. - # If it's blank, create a new object using :new. + # Helper that find or initialize an object by attribute only if the given value + # is not blank. If it's blank, create a new object using :new. # def self.find_or_initialize_by_unless_blank(klass, attr, value) if value.blank? klass.new else klass.send(:"find_or_initialize_by_#{attr}", value) end end - # Helpers that generates a unique code for the given attribute by checking in - # the database if the code already exists. + # Creates a new record, assigning the perishable token and an error message. # - def self.generate_unique_string_for(klass, attr, length=40) - begin - value = AuthHelpers.random_string(length) - end while klass.send(:"find_by_#{attr}", value) - - value + def self.new_with_perishable_token_error(klass, message=:invalid, options={}) + record = klass.new(options) + record.perishable_token = options[:perishable_token] + record.errors.add(:perishable_token, message, :default => :invalid) + record end - # Create a random string with the given length using letters and numbers. - # - def self.random_string(length) - chars = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a - - newpass = '' - 1.upto(length) { |i| newpass << chars.rand } - - return newpass - end end