app/models/dc_user.rb in drg_cms-0.4.54 vs app/models/dc_user.rb in drg_cms-0.4.57
- old
+ new
@@ -25,10 +25,11 @@
# ActiveSupport::Concern definition for DcUser class.
#########################################################################
module DcUserConcern
extend ActiveSupport::Concern
included do
+@@countries = nil
include Mongoid::Document
include Mongoid::Timestamps
include ActiveModel::SecurePassword
@@ -86,9 +87,26 @@
def do_before_save
self.name = "#{self.title} #{self.first_name} #{self.middle_name + ' ' unless self.middle_name.blank?}#{self.last_name}".strip
# to ensure unique e-mail
self.email = "unknown@#{self.id}" if self.email.to_s.strip.size < 5
end
+
+##########################################################################
+# Will return all possible values for country field ready for input in select field.
+# Values are loaded from github when method is first called.
+##########################################################################
+def self.choices4_country()
+ if @@countries.nil?
+ uri = URI.parse("https://raw.githubusercontent.com/umpirsky/country-list/master/country/cldr/en/country.json")
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.use_ssl = true
+
+ request = Net::HTTP::Get.new(uri.request_uri)
+ response = http.request(request)
+ @@countries = JSON.parse(response.body).to_a.inject([]) {|result, e| result << [e[1], e[0]] }
+ end
+ @@countries
+end
end
end
########################################################################
\ No newline at end of file