2013-06-20 17:04:09 +00:00
|
|
|
# Copyright (C) 2013-2013 Zammad Foundation, http://zammad-foundation.org/
|
2013-06-12 15:59:58 +00:00
|
|
|
|
2012-11-20 18:39:19 +00:00
|
|
|
require 'cache'
|
2013-04-20 08:58:27 +00:00
|
|
|
require 'user_info'
|
2012-11-18 11:26:24 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
class ApplicationModel < ActiveRecord::Base
|
|
|
|
self.abstract_class = true
|
|
|
|
|
2013-04-20 08:52:17 +00:00
|
|
|
before_create :cache_delete, :fill_up_user_create
|
|
|
|
before_update :cache_delete_before, :fill_up_user_update
|
2013-03-11 21:15:21 +00:00
|
|
|
before_destroy :cache_delete_before
|
2013-01-01 20:29:26 +00:00
|
|
|
after_create :cache_delete
|
|
|
|
after_update :cache_delete
|
|
|
|
after_destroy :cache_delete
|
|
|
|
|
2013-03-21 09:40:49 +00:00
|
|
|
@@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::Priority', 'Group', 'User' ]
|
|
|
|
|
2013-06-20 17:04:09 +00:00
|
|
|
# for import of other objects, remove 'id'
|
2013-02-17 20:59:57 +00:00
|
|
|
def self.attributes_protected_by_default
|
2013-03-21 09:40:49 +00:00
|
|
|
if Setting.get('import_mode') && @@import_class_list.include?( self.name.to_s )
|
2013-02-20 08:50:43 +00:00
|
|
|
['type']
|
|
|
|
else
|
2013-06-20 17:04:09 +00:00
|
|
|
['id','type', 'created_at', 'updated_at']
|
2013-02-20 08:50:43 +00:00
|
|
|
end
|
2013-02-17 20:59:57 +00:00
|
|
|
end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
def self.param_cleanup(params)
|
2013-06-19 20:44:18 +00:00
|
|
|
|
|
|
|
# only use object attributes
|
2012-09-20 12:08:02 +00:00
|
|
|
data = {}
|
|
|
|
self.new.attributes.each {|item|
|
|
|
|
if params.has_key?(item[0])
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts 'use ' + item[0].to_s + '-' + params[item[0]].to_s
|
2012-09-20 12:08:02 +00:00
|
|
|
data[item[0].to_sym] = params[item[0]]
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-06-19 20:44:18 +00:00
|
|
|
# we do want to set this via database
|
|
|
|
self.param_validation(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.param_validation(data)
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
# we do want to set this via database
|
|
|
|
data.delete( :updated_at )
|
|
|
|
data.delete( :created_at )
|
|
|
|
data.delete( :updated_by_id )
|
|
|
|
data.delete( :created_by_id )
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2013-04-20 10:35:54 +00:00
|
|
|
# set created_by_id & updated_by_id if not given based on UserInfo
|
2013-04-20 08:52:17 +00:00
|
|
|
def fill_up_user_create
|
|
|
|
if self.class.column_names.include? 'updated_by_id'
|
|
|
|
if UserInfo.current_user_id
|
|
|
|
if self.updated_by_id && self.updated_by_id != UserInfo.current_user_id
|
2013-04-20 10:35:54 +00:00
|
|
|
puts "NOTICE create - self.updated_by_id is different: #{self.updated_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
|
2013-04-20 08:52:17 +00:00
|
|
|
end
|
|
|
|
self.updated_by_id = UserInfo.current_user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if self.class.column_names.include? 'created_by_id'
|
|
|
|
if UserInfo.current_user_id
|
|
|
|
if self.created_by_id && self.created_by_id != UserInfo.current_user_id
|
2013-04-20 10:35:54 +00:00
|
|
|
puts "NOTICE create - self.created_by_id is different: #{self.created_by_id.to_s}/#{UserInfo.current_user_id.to_s}"
|
2013-04-20 08:52:17 +00:00
|
|
|
end
|
|
|
|
self.created_by_id = UserInfo.current_user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-04-20 10:35:54 +00:00
|
|
|
|
|
|
|
# set updated_by_id if not given based on UserInfo
|
2013-04-20 08:52:17 +00:00
|
|
|
def fill_up_user_update
|
|
|
|
return if !self.class.column_names.include? 'updated_by_id'
|
|
|
|
if UserInfo.current_user_id
|
|
|
|
self.updated_by_id = UserInfo.current_user_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
def cache_update(o)
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts 'u ' + self.class.to_s
|
2012-04-16 08:04:49 +00:00
|
|
|
if self.respond_to?('cache_delete') then self.cache_delete end
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts 'g ' + group.class.to_s
|
2012-04-16 08:04:49 +00:00
|
|
|
if o.respond_to?('cache_delete') then o.cache_delete end
|
|
|
|
end
|
2013-03-11 21:15:21 +00:00
|
|
|
def cache_delete_before
|
|
|
|
old_object = self.class.where( :id => self.id ).first
|
|
|
|
if old_object
|
|
|
|
old_object.cache_delete
|
|
|
|
end
|
|
|
|
self.cache_delete
|
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
def cache_delete
|
2012-04-16 09:45:02 +00:00
|
|
|
key = self.class.to_s + '::' + self.id.to_s
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.delete( key.to_s )
|
2013-01-04 18:42:20 +00:00
|
|
|
key = self.class.to_s + ':f:' + self.id.to_s
|
|
|
|
Cache.delete( key.to_s )
|
2013-03-11 21:15:21 +00:00
|
|
|
if self[:name]
|
|
|
|
key = self.class.to_s + '::' + self.name.to_s
|
|
|
|
Cache.delete( key.to_s )
|
|
|
|
key = self.class.to_s + ':f:' + self.name.to_s
|
|
|
|
Cache.delete( key.to_s )
|
|
|
|
end
|
|
|
|
if self[:login]
|
|
|
|
key = self.class.to_s + '::' + self.login.to_s
|
|
|
|
Cache.delete( key.to_s )
|
|
|
|
key = self.class.to_s + ':f:' + self.login.to_s
|
|
|
|
Cache.delete( key.to_s )
|
|
|
|
end
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2013-01-04 18:42:20 +00:00
|
|
|
def self.cache_set(data_id, data, full = false)
|
|
|
|
if !full
|
|
|
|
key = self.to_s + '::' + data_id.to_s
|
|
|
|
else
|
|
|
|
key = self.to_s + ':f:' + data_id.to_s
|
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.write( key.to_s, data )
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-01-04 18:42:20 +00:00
|
|
|
def self.cache_get(data_id, full = false)
|
|
|
|
if !full
|
|
|
|
key = self.to_s + '::' + data_id.to_s
|
|
|
|
else
|
|
|
|
key = self.to_s + ':f:' + data_id.to_s
|
|
|
|
end
|
2012-07-29 15:27:01 +00:00
|
|
|
Cache.get( key.to_s )
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2012-12-27 20:17:33 +00:00
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
def self.lookup(data)
|
|
|
|
if data[:id]
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts "GET- + #{self.to_s}.#{data[:id].to_s}"
|
2013-01-01 20:29:26 +00:00
|
|
|
cache = self.cache_get( data[:id] )
|
|
|
|
return cache if cache
|
|
|
|
|
2013-06-12 15:59:58 +00:00
|
|
|
# puts "Fillup- + #{self.to_s}.#{data[:id].to_s}"
|
2013-01-01 20:29:26 +00:00
|
|
|
record = self.where( :id => data[:id] ).first
|
|
|
|
self.cache_set( data[:id], record )
|
|
|
|
return record
|
|
|
|
elsif data[:name]
|
|
|
|
cache = self.cache_get( data[:name] )
|
|
|
|
return cache if cache
|
|
|
|
|
2013-01-04 08:00:16 +00:00
|
|
|
records = self.where( :name => data[:name] )
|
|
|
|
records.each {|record|
|
|
|
|
if record.name == data[:name]
|
|
|
|
self.cache_set( data[:name], record )
|
|
|
|
return record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return
|
2013-01-04 18:42:20 +00:00
|
|
|
elsif data[:login]
|
|
|
|
cache = self.cache_get( data[:login] )
|
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
records = self.where( :login => data[:login] )
|
|
|
|
records.each {|record|
|
|
|
|
if record.login == data[:login]
|
|
|
|
self.cache_set( data[:login], record )
|
|
|
|
return record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return
|
2013-01-01 20:29:26 +00:00
|
|
|
else
|
2013-01-04 18:42:20 +00:00
|
|
|
raise "Need name, id or login for lookup()"
|
2013-01-01 20:29:26 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-27 20:17:33 +00:00
|
|
|
def self.create_if_not_exists(data)
|
2013-01-04 18:42:20 +00:00
|
|
|
if data[:id]
|
|
|
|
record = self.where( :id => data[:id] ).first
|
|
|
|
return record if record
|
|
|
|
elsif data[:name]
|
2013-01-04 08:00:16 +00:00
|
|
|
records = self.where( :name => data[:name] )
|
|
|
|
records.each {|record|
|
|
|
|
return record if record.name == data[:name]
|
|
|
|
}
|
2013-01-04 18:42:20 +00:00
|
|
|
elsif data[:login]
|
|
|
|
records = self.where( :login => data[:login] )
|
|
|
|
records.each {|record|
|
|
|
|
return record if record.login == data[:login]
|
|
|
|
}
|
2012-12-27 20:17:33 +00:00
|
|
|
elsif data[:locale] && data[:source]
|
2013-01-04 08:00:16 +00:00
|
|
|
records = self.where( :locale => data[:locale], :source => data[:source] )
|
|
|
|
records.each {|record|
|
|
|
|
return record if record.source == data[:source]
|
|
|
|
}
|
2012-12-27 20:17:33 +00:00
|
|
|
end
|
|
|
|
self.create(data)
|
|
|
|
end
|
2012-12-30 09:45:01 +00:00
|
|
|
|
|
|
|
def self.create_or_update(data)
|
|
|
|
if data[:name]
|
2013-01-04 08:00:16 +00:00
|
|
|
records = self.where( :name => data[:name] )
|
|
|
|
records.each {|record|
|
|
|
|
if record.name == data[:name]
|
2013-02-07 23:03:46 +00:00
|
|
|
record.update_attributes( data )
|
2013-01-04 08:00:16 +00:00
|
|
|
return record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
record = self.new( data )
|
|
|
|
record.save
|
2012-12-30 09:45:01 +00:00
|
|
|
return record
|
|
|
|
else
|
|
|
|
raise "Need name for create_or_update()"
|
|
|
|
end
|
|
|
|
end
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|