Class: City
- Inherits:
-
Pattana::ApplicationRecord
- Object
- ActiveRecord::Base
- Pattana::ApplicationRecord
- City
- Includes:
- ActiveModel::Serializers::JSON
- Defined in:
- app/models/city.rb
Class Method Summary collapse
- .parse_row_data(row_data) ⇒ Object
-
.save_row_data(row_data) ⇒ Object
Import Methods.
Instance Method Summary collapse
- #can_be_deleted? ⇒ Boolean
-
#can_be_edited? ⇒ Boolean
Permission Methods ——————.
-
#display_name ⇒ Object
-
Return full name == Examples >>> city.display_name => “India”.
-
-
#display_operational ⇒ Object
-
Return Yes or No == Examples >>> city.display_operational => “Yes”.
-
-
#display_show_in_api ⇒ Object
-
Return Yes or No == Examples >>> city.display_show_in_api => “India”.
-
-
#set_country ⇒ Object
Callback Methods ——————.
Class Method Details
.parse_row_data(row_data) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/models/city.rb', line 51 def self.parse_row_data(row_data) # Initializing error hash for displaying all errors altogether error_object = Kuppayam::Importer::ErrorHash.new if row_data[:name].blank? name = "" else name = self.clean_string(row_data[:name], :name) country_iso_code = self.clean_string(row_data[:iso_alpha_2], :iso_alpha_2) # Sometimes the name could be just 2 letters. # In that case, just to let it go, add the region or country name to it name = "#{name}-#{country_iso_code}" if name && name.size <= 2 end city = City.find_by_name(name) || City.new(name: name) region = nil begin # Some data sheet would have an iso code for city (iso_code) # Some other would have just their country name (country) or country iso code (iso_alpha_2) and region name (region) if row_data[:iso_code] city.iso_code = self.clean_string(row_data[:iso_code], :iso_code) region_iso_code = city.iso_code.split("-")[0..1].join("-") region = Region.where("iso_code = ?", region_iso_code).first if region_iso_code elsif row_data[:region_id] && country_iso_code region_id = self.clean_string(row_data[:region_id], :region_id) region_code1 = "#{country_iso_code}-#{region_id}" region_code2 = "#{country_iso_code}-0#{region_id}" region = Region.where("iso_code = ? or iso_code = ?", region_code1, region_code2).first elsif row_data[:region] && country_iso_code region_name = self.clean_string(row_data[:region], :region) region = Region.where("name = ?", region_name).first || Region.new(name: region_name) end # Add region if found city.region = region if region # At least add country if that exists. city.country = region.country if region && region.country city.country = Country.where("iso_alpha_2 = ?", country_iso_code).first if city.country.nil? && !country_iso_code.blank? rescue Exception => e puts "Error while parsing country and region - #{e.}".red end city.alternative_names = self.clean_string(row_data[:alternative_names], :alternative_names)[0..255] if row_data[:alternative_names] city.population = self.clean_string(row_data[:population], :population) if row_data[:population] city.priority = 1000 city.show_in_api = true if city.country && ["United Arab Emirates", "United States", "Saudi Arabia", "Jordan", "India"].include?(city.country.name) city.latitude = self.clean_string(row_data[:latitude], :latitude) if row_data[:latitude] && row_data[:latitude] != "NULL" city.longitude = self.clean_string(row_data[:longitude], :longitude) if row_data[:longitude] && row_data[:longitude] != "NULL" city.show_in_api = true if city.country && city.country.show_in_api unless city.valid? summary = "Error while saving city: #{city.name}" details = "Error! #{city.errors..to_sentence}" error_object.errors << { summary: summary, details: details } end return city, error_object end |
.save_row_data(row_data) ⇒ Object
Import Methods
39 40 41 42 43 44 45 46 47 48 49 |
# File 'app/models/city.rb', line 39 def self.save_row_data(row_data) begin city, error_object = self.parse_row_data(row_data) city.save if city.errors.blank? rescue Exception => e summary = "uncaught #{e} exception while handling connection: #{e.}" details = "Stack trace: #{e.backtrace.map {|l| " #{l}\n"}.join}" error_object.errors << { summary: summary, details: details } end return error_object end |
Instance Method Details
#can_be_deleted? ⇒ Boolean
134 135 136 137 |
# File 'app/models/city.rb', line 134 def can_be_deleted? return false if operational? true end |
#can_be_edited? ⇒ Boolean
Permission Methods
130 131 132 |
# File 'app/models/city.rb', line 130 def can_be_edited? true end |
#display_name ⇒ Object
-
Return full name
Examples
>>> city.display_name
=> "India"
146 147 148 |
# File 'app/models/city.rb', line 146 def display_name self.name end |
#display_operational ⇒ Object
-
Return Yes or No
Examples
>>> city.display_operational
=> "Yes"
162 163 164 |
# File 'app/models/city.rb', line 162 def display_operational self.operational ? "Yes" : "No" end |
#display_show_in_api ⇒ Object
-
Return Yes or No
Examples
>>> city.display_show_in_api
=> "India"
154 155 156 |
# File 'app/models/city.rb', line 154 def display_show_in_api self.show_in_api ? "Yes" : "No" end |
#set_country ⇒ Object
Callback Methods
123 124 125 |
# File 'app/models/city.rb', line 123 def set_country self.country = self.region.country if self.region && self.region.country end |