Created
November 5, 2012 03:51
-
-
Save pnicholls/4015223 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| def generate_site_code | |
| # Remove all non letters characters | |
| site_name = name.gsub(/[^a-zA-Z]/, '') | |
| # Make sure the site name is at least 5 characters | |
| site_name += Array(('a'..'z')).shuffle[0, (5 - site_name.length)].join if site_name.length < 5 | |
| # The site code is made up of the country and state code plus the first 5 characters of the site name | |
| code = country.code[0..1] + (city.state.code? ? city.state.code[0..2] : country.description[0..2]) + site_name[0..4] | |
| self.code = code.upcase | |
| # Check to make sure the code is unique, if not change the site name slightly | |
| while Site.where("code = ?", self.code).count > 0 | |
| site_name = site_name[0..2] + Array(('a'..'z')).shuffle[0..1].join | |
| code = country.code + city.state.code + site_name | |
| self.code = code.upcase | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment