Created
June 15, 2009 19:08
-
-
Save jimmybaker/130274 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NextNumber < ActiveRecord::Base | |
class << self | |
VALID_CLASSES = [PurchaseOrder, SalesOrder] | |
def get_next_number_for(object) | |
throw Exception.new("Unsaved object not allowed") if object.new_record? | |
throw Exception.new("Invalid object type for NextNumber#get_next_number_for") unless VALID_CLASSES.include?(object.class) | |
begin | |
next_number = NextNumber.find_by_sequential_type(object.class.to_s) | |
next_number.sequential_id = object.id | |
next_number.save | |
next_number.lock_version | |
rescue ActiveRecord::StaleObjectError => e | |
retry | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment