Skip to content

Instantly share code, notes, and snippets.

@btc
Created August 7, 2012 21:16
Show Gist options
  • Save btc/3289427 to your computer and use it in GitHub Desktop.
Save btc/3289427 to your computer and use it in GitHub Desktop.
not overriding new properly
class Locale
# must provide session and request options
def self.new(options = {})
initialize_region(options) || NullLocale.new
end
# return the region object associated with this Locale
def region_name
@region.name
end
def region_id
@region.id
end
def valid?
true
end
private
def self.initialize_region(options = {})
raise ArgumentError, 'No session provided' if options[:session].nil?
raise ArgumentError, 'No request provided' if options[:request].nil?
@region = case
when Subdomain.matches?(options[:request])
region = Region.find_by_subdomain(options[:request].subdomain)
options[:session][:region_id] = region.id if region.present?
region # @region = region
else
Region.find_by_id(options[:session][:region_id])
end
end
end
class NullLocale < Locale
def region_name
''
end
def region_id
nil
end
def valid
false
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment