From 87a4d1eb34aace4853a2792f27aeed2a498c6d2e Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 6 Aug 2013 12:29:35 +0200 Subject: [PATCH] Moved generation of geo data to observer to keep model small. --- app/models/observer/user/geo.rb | 65 ++++++++++++++++++++++++++++++++ app/models/user.rb | 67 +-------------------------------- config/application.rb | 4 +- 3 files changed, 70 insertions(+), 66 deletions(-) create mode 100644 app/models/observer/user/geo.rb diff --git a/app/models/observer/user/geo.rb b/app/models/observer/user/geo.rb new file mode 100644 index 000000000..0c7aa5ab4 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index e5121b29a..393e47d7a 100644 --- a/app/models/user.rb +++ b/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 diff --git a/config/application.rb b/config/application.rb index 83aacad44..506e6a9ab 100644 --- a/config/application.rb +++ b/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]