Browse Source

Moved generation of geo data to observer to keep model small.

Martin Edenhofer 11 years ago
parent
commit
87a4d1eb34
3 changed files with 70 additions and 66 deletions
  1. 65 0
      app/models/observer/user/geo.rb
  2. 2 65
      app/models/user.rb
  3. 3 1
      config/application.rb

+ 65 - 0
app/models/observer/user/geo.rb

@@ -0,0 +1,65 @@
+# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
+require 'gmaps'
+
+class Observer::User::Geo < ActiveRecord::Observer
+  observe 'user'
+
+  def before_create(record)
+    check_geo(record)
+  end
+  def before_update(record)
+    check_geo(record)
+  end
+
+  # check if geo need to be updated
+  def check_geo(record)
+
+    location = ['street', 'zip', 'city', 'country']
+
+    # check if geo update is needed based on old/new location
+    if record.id
+      current = User.where( :id => record.id ).first
+	  return if !current
+
+      current_location = {}
+      location.each { |item|
+        current_location[item] = current[item]
+      }
+    end
+
+    # get full address
+    next_location = {}
+    location.each { |item|
+      next_location[item] = record[item]
+    }
+
+    # return if address hasn't changed and geo data is already available
+    return if ( current_location == next_location ) && record.preferences['lat'] && record.preferences['lng']
+
+    # geo update
+    self.geo_update(record)
+  end
+
+
+  # update geo data of user
+  def geo_update(record)
+    address = ''
+    location = ['street', 'zip', 'city', 'country']
+    location.each { |item|
+      if record[item] && record[item] != ''
+        address = address + ',' + record[item]
+      end
+    }
+
+    # return if no address is given
+    return if address == ''
+
+    # db lookup
+    latlng = Gmaps.geocode(address)
+    return if !latlng
+
+    # store data
+    record.preferences['lat'] = latlng[0]
+    record.preferences['lng'] = latlng[1]
+  end
+end

+ 2 - 65
app/models/user.rb

@@ -4,10 +4,8 @@ require 'digest/sha2'
 require 'organization'
 
 class User < ApplicationModel
-  include Gmaps
-
-  before_create   :check_name, :check_email, :check_login, :check_image, :check_geo, :check_password
-  before_update   :check_password, :check_image, :check_geo, :check_email, :check_login_update
+  before_create   :check_name, :check_email, :check_login, :check_image, :check_password
+  before_update   :check_password, :check_image, :check_email, :check_login_update
   after_create    :notify_clients_after_create
   after_update    :notify_clients_after_update
   after_destroy   :notify_clients_after_destroy
@@ -380,73 +378,12 @@ class User < ApplicationModel
     return user
   end
 
-  # update all users geo data
-  def self.geo_update_all
-    User.all.each { |user|
-      user.geo_update
-      user.save
-    }
-  end
-
-  # update geo data of one user
-  def geo_update
-    address = ''
-    location = ['street', 'zip', 'city', 'country']
-    location.each { |item|
-      if self[item] && self[item] != ''
-        address = address + ',' + self[item]
-      end
-    }
-
-    # return if no address is given
-    return if address == ''
-
-    # dp lookup
-    latlng = Gmaps.geocode(address)
-    if latlng
-      self.preferences['lat'] = latlng[0]
-      self.preferences['lng'] = latlng[1]
-    end
-  end
-
   def update_last_login
     self.last_login = Time.now
     self.save
   end
 
   private
-  def check_geo
-
-    # geo update if no user exists
-    if !self.id
-      self.geo_update
-      return
-    end
-
-    location = ['street', 'zip', 'city', 'country']
-
-    # get current user data
-    current = User.where( :id => self.id ).first
-    return if !current
-
-    # check if geo update is needed
-    current_location = {}
-    location.each { |item|
-      current_location[item] = current[item]
-    }
-
-    # get full address
-    next_location = {}
-    location.each { |item|
-      next_location[item] = self[item]
-    }
-
-    # return if address hasn't changed and geo data is already available
-    return if ( current_location == next_location ) && ( self.preferences['lat'] && self.preferences['lng'] )
-
-    # geo update
-    self.geo_update
-  end
 
   def check_name
 

+ 3 - 1
config/application.rb

@@ -42,7 +42,9 @@ module Zammad
       'observer::_ticket::_notification',
       'observer::_tag::_ticket_history',
       'observer::_ticket::_reset_new_state',
-      'observer::_ticket::_escalation_calculation'
+      'observer::_ticket::_escalation_calculation',
+      'observer::_user::_geo'
+
 
     # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
     # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]