Class: Region

Inherits:
Pattana::ApplicationRecord show all
Defined in:
app/models/region.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.save_row_data(hsh) ⇒ Object

Import Methods



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/region.rb', line 28

def self.save_row_data(hsh)

  return if hsh[:name].blank?

  iso_code = self.clean_string(hsh[:iso_code])
  region = Region.where("iso_code = ?", iso_code).first || Region.new(iso_code: iso_code)
  region.name = self.clean_string(hsh[:name]) if hsh[:name]
  
  begin
    country = nil
    country_iso_alpha_2 = region.iso_code.split("-").first.strip
    country = Country.where("iso_alpha_2 = ?", country_iso_alpha_2).first if country_iso_alpha_2
    region.country = country if country
  rescue Exception => e
    puts "Error while parsing iso code for country - #{e.message}".red
  end

  region.latitude = self.clean_string(hsh[:latitude]) if hsh[:latitude] && hsh[:latitude] != "NULL"
  region.longitude = self.clean_string(hsh[:longitude]) if hsh[:longitude] && hsh[:longitude] != "NULL"

  region.show_in_api = true if region.country && region.country.show_in_api
  
  # Initializing error hash for displaying all errors altogether
  error_object = Kuppayam::Importer::ErrorHash.new

  if region.valid?
    begin
      region.save!
    rescue Exception => e
      summary = "uncaught #{e} exception while handling connection: #{e.message}"
      details = "Stack trace: #{e.backtrace.map {|l| "  #{l}\n"}.join}"
      error_object.errors << { summary: summary, details: details }        
    end
  else
    summary = "Error while saving region: #{region.name}"
    details = "Error! #{region.errors.full_messages.to_sentence}"
    error_object.errors << { summary: summary, details: details }
  end
  return error_object
end

Instance Method Details

#can_be_deleted?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
# File 'app/models/region.rb', line 80

def can_be_deleted?
  return false if self.cities.any?
  return false if operational?
  true
end

#can_be_edited?Boolean

Permission Methods


Returns:

  • (Boolean)


76
77
78
# File 'app/models/region.rb', line 76

def can_be_edited?
  true
end

#display_nameObject

  • Return full name

Examples

>>> region.display_name
=> "India"


93
94
95
# File 'app/models/region.rb', line 93

def display_name
  self.name
end

#display_operationalObject

  • Return Yes or No

Examples

>>> region.display_operational
=> "Yes"


109
110
111
# File 'app/models/region.rb', line 109

def display_operational
  self.operational ? "Yes" : "No"
end

#display_show_in_apiObject

  • Return Yes or No

Examples

>>> region.display_show_in_api
=> "No"


101
102
103
# File 'app/models/region.rb', line 101

def display_show_in_api
  self.show_in_api ? "Yes" : "No"
end