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-04-16 08:04:49 +00:00
|
|
|
class ApplicationModel < ActiveRecord::Base
|
2013-12-26 21:51:25 +00:00
|
|
|
include ApplicationModel::Assets
|
2013-09-25 19:50:28 +00:00
|
|
|
include ApplicationModel::HistoryLogBase
|
2013-09-28 00:07:11 +00:00
|
|
|
include ApplicationModel::ActivityStreamBase
|
2014-01-27 22:59:41 +00:00
|
|
|
include ApplicationModel::SearchIndexBase
|
2013-09-25 19:50:28 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
self.abstract_class = true
|
|
|
|
|
2013-11-14 13:21:57 +00:00
|
|
|
before_create :check_attributes_protected, :check_limits, :cache_delete, :fill_up_user_create
|
|
|
|
before_update :check_limits, :cache_delete_before, :fill_up_user_update
|
2013-09-25 19:50:28 +00:00
|
|
|
before_destroy :cache_delete_before, :destroy_dependencies
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
after_create :cache_delete
|
|
|
|
after_update :cache_delete
|
|
|
|
after_destroy :cache_delete
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
after_create :activity_stream_create
|
|
|
|
after_update :activity_stream_update
|
|
|
|
after_destroy :activity_stream_destroy
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
after_create :history_create
|
|
|
|
after_update :history_update
|
|
|
|
after_destroy :history_destroy
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
after_create :search_index_update
|
|
|
|
after_update :search_index_update
|
|
|
|
after_destroy :search_index_destroy
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
# create instance accessor
|
2013-09-29 16:40:42 +00:00
|
|
|
class << self
|
2014-01-27 22:59:41 +00:00
|
|
|
attr_accessor :activity_stream_support_config, :history_support_config, :search_index_support_config
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2013-09-29 22:45:58 +00:00
|
|
|
attr_accessor :history_changes_last_done
|
|
|
|
|
2013-03-21 09:40:49 +00:00
|
|
|
@@import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::Priority', 'Group', 'User' ]
|
|
|
|
|
2013-08-22 21:15:23 +00:00
|
|
|
def check_attributes_protected
|
2013-03-21 09:40:49 +00:00
|
|
|
if Setting.get('import_mode') && @@import_class_list.include?( self.name.to_s )
|
2013-08-22 21:15:23 +00:00
|
|
|
# do noting, use id as it is
|
2013-02-20 08:50:43 +00:00
|
|
|
else
|
2013-08-22 21:15:23 +00:00
|
|
|
self[:id] = nil
|
2013-02-20 08:50:43 +00:00
|
|
|
end
|
2013-02-17 20:59:57 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
remove all not used model attributes of params
|
|
|
|
|
|
|
|
result = Model.param_cleanup(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params with valid attributes of model
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-09-20 12:08:02 +00:00
|
|
|
def self.param_cleanup(params)
|
2013-06-19 20:44:18 +00:00
|
|
|
|
2013-09-20 11:40:58 +00:00
|
|
|
if params == nil
|
|
|
|
raise "No params for #{self.to_s}!"
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
2013-09-30 23:37:45 +00:00
|
|
|
set rellations of model based on params
|
|
|
|
|
|
|
|
result = Model.param_set_associations(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def param_set_associations(params)
|
|
|
|
|
|
|
|
# set relations
|
|
|
|
self.class.reflect_on_all_associations.map { |assoc|
|
|
|
|
real_key = assoc.name.to_s[0,assoc.name.to_s.length-1] + '_ids'
|
|
|
|
if params.has_key?( real_key.to_sym )
|
2013-10-01 06:17:01 +00:00
|
|
|
list_of_items = params[ real_key.to_sym ]
|
|
|
|
if params[ real_key.to_sym ].class != Array
|
|
|
|
list_of_items = [ params[ real_key.to_sym ] ]
|
|
|
|
end
|
2013-09-30 23:37:45 +00:00
|
|
|
list = []
|
2013-10-01 06:17:01 +00:00
|
|
|
list_of_items.each {|item|
|
2013-09-30 23:37:45 +00:00
|
|
|
list.push( assoc.klass.find(item) )
|
|
|
|
}
|
|
|
|
self.send( assoc.name.to_s + '=', list )
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
|
|
|
|
|
|
|
|
result = Model.param_validation(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params without listed attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-06-19 20:44:18 +00:00
|
|
|
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 )
|
2013-08-22 21:15:23 +00:00
|
|
|
if data.respond_to?('permit!')
|
|
|
|
data.permit!
|
|
|
|
end
|
2012-09-20 12:08:02 +00:00
|
|
|
data
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
set created_by_id & updated_by_id if not given based on UserInfo (current session)
|
|
|
|
|
|
|
|
Used as before_create callback, no own use needed
|
|
|
|
|
|
|
|
result = Model.fill_up_user_create(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
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
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
set updated_by_id if not given based on UserInfo (current session)
|
|
|
|
|
|
|
|
Used as before_update callback, no own use needed
|
|
|
|
|
|
|
|
result = Model.fill_up_user_update(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
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-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
lookup model from cache (if exists) or retrieve it from db, id, name or login possible
|
|
|
|
|
|
|
|
result = Model.lookup( :id => 123 )
|
|
|
|
result = Model.lookup( :name => 'some name' )
|
|
|
|
result = Model.lookup( :login => 'some login' )
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
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
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
create model if not exists (check exists based on id, name, login or locale)
|
|
|
|
|
|
|
|
result = Model.create_if_not_exists( attributes )
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=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
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
create or update model (check exists based on name, login or locale)
|
|
|
|
|
|
|
|
result = Model.create_or_update( attributes )
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=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
|
2013-07-19 14:21:44 +00:00
|
|
|
elsif data[:login]
|
|
|
|
records = self.where( :login => data[:login] )
|
|
|
|
records.each {|record|
|
|
|
|
if record.login.downcase == data[:login].downcase
|
|
|
|
record.update_attributes( data )
|
|
|
|
return record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
record = self.new( data )
|
|
|
|
record.save
|
|
|
|
return record
|
2013-08-17 20:12:27 +00:00
|
|
|
elsif data[:locale]
|
|
|
|
records = self.where( :locale => data[:locale] )
|
|
|
|
records.each {|record|
|
|
|
|
if record.locale.downcase == data[:locale].downcase
|
|
|
|
record.update_attributes( data )
|
|
|
|
return record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
record = self.new( data )
|
|
|
|
record.save
|
|
|
|
return record
|
2012-12-30 09:45:01 +00:00
|
|
|
else
|
2013-08-17 20:12:27 +00:00
|
|
|
raise "Need name, login or locale for create_or_update()"
|
2012-12-30 09:45:01 +00:00
|
|
|
end
|
|
|
|
end
|
2013-06-29 00:13:28 +00:00
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
notify_clients_after_create after model got created
|
|
|
|
|
|
|
|
used as callback in model file
|
|
|
|
|
|
|
|
class OwnModel < ApplicationModel
|
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_destroy :notify_clients_after_destroy
|
|
|
|
|
|
|
|
[...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-06-29 00:13:28 +00:00
|
|
|
def notify_clients_after_create
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
|
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2013-08-21 18:35:22 +00:00
|
|
|
Sessions.broadcast(
|
2013-06-29 00:13:28 +00:00
|
|
|
:event => class_name + ':created',
|
|
|
|
:data => { :id => self.id, :updated_at => self.updated_at }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
notify_clients_after_update after model got updated
|
|
|
|
|
|
|
|
used as callback in model file
|
|
|
|
|
|
|
|
class OwnModel < ApplicationModel
|
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_destroy :notify_clients_after_destroy
|
|
|
|
|
|
|
|
[...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-06-29 00:13:28 +00:00
|
|
|
def notify_clients_after_update
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
puts "#{self.class.name.downcase} UPDATED " + self.updated_at.to_s
|
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2013-08-21 18:35:22 +00:00
|
|
|
Sessions.broadcast(
|
2013-06-29 00:13:28 +00:00
|
|
|
:event => class_name + ':updated',
|
|
|
|
:data => { :id => self.id, :updated_at => self.updated_at }
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
notify_clients_after_destroy after model got destroyed
|
|
|
|
|
|
|
|
used as callback in model file
|
|
|
|
|
|
|
|
class OwnModel < ApplicationModel
|
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_destroy :notify_clients_after_destroy
|
|
|
|
|
|
|
|
[...]
|
|
|
|
|
|
|
|
=end
|
2013-06-29 00:13:28 +00:00
|
|
|
def notify_clients_after_destroy
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
|
|
|
puts "#{self.class.name.downcase} DESTOY " + self.updated_at.to_s
|
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2013-08-21 18:35:22 +00:00
|
|
|
Sessions.broadcast(
|
2013-06-29 00:13:28 +00:00
|
|
|
:event => class_name + ':destroy',
|
|
|
|
:data => { :id => self.id, :updated_at => self.updated_at }
|
|
|
|
)
|
|
|
|
end
|
2013-09-25 19:50:28 +00:00
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
serve methode to configure and enable search index support for this model
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
search_index_support :ignore_attributes => {
|
|
|
|
:create_article_type_id => true,
|
|
|
|
:create_article_sender_id => true,
|
|
|
|
:article_count => true,
|
|
|
|
}
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.search_index_support(data = {})
|
|
|
|
@search_index_support_config = data
|
|
|
|
end
|
2013-09-25 19:50:28 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
update search index, if configured - will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.search_index_update
|
2013-11-14 13:21:57 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
def search_index_update
|
|
|
|
return if !self.class.search_index_support_config
|
|
|
|
search_index_update_backend
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete search index object, will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.search_index_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_destroy
|
|
|
|
return if !self.class.search_index_support_config
|
|
|
|
SearchIndexBackend.remove( self.class.to_s, self.id )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
reload search index with full data
|
|
|
|
|
|
|
|
Model.search_index_reload
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.search_index_reload
|
|
|
|
return if !@search_index_support_config
|
2014-01-30 00:06:04 +00:00
|
|
|
self.all.order('created_at DESC').each { |item|
|
2014-01-27 22:59:41 +00:00
|
|
|
item.search_index_update_backend
|
2013-11-14 13:21:57 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
serve methode to configure and enable activity stream support for this model
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
activity_stream_support :role => 'Admin'
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.activity_stream_support(data = {})
|
|
|
|
@activity_stream_support_config = data
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
log object create activity stream, if configured - will be executed automatically
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.activity_stream_create
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def activity_stream_create
|
2013-10-05 12:56:03 +00:00
|
|
|
return if !self.class.activity_stream_support_config
|
2013-09-28 00:07:11 +00:00
|
|
|
activity_stream_log( 'created', self['created_by_id'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
log object update activity stream, if configured - will be executed automatically
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.activity_stream_update
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def activity_stream_update
|
2013-10-05 12:56:03 +00:00
|
|
|
return if !self.class.activity_stream_support_config
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
return if !self.changed?
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
# default ignored attributes
|
|
|
|
ignore_attributes = {
|
|
|
|
:created_at => true,
|
|
|
|
:updated_at => true,
|
|
|
|
:created_by_id => true,
|
|
|
|
:updated_by_id => true,
|
|
|
|
}
|
|
|
|
if self.class.activity_stream_support_config[:ignore_attributes]
|
|
|
|
self.class.activity_stream_support_config[:ignore_attributes].each {|key, value|
|
|
|
|
ignore_attributes[key] = value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
log = false
|
|
|
|
self.changes.each {|key, value|
|
|
|
|
|
|
|
|
# do not log created_at and updated_at attributes
|
|
|
|
next if ignore_attributes[key.to_sym] == true
|
|
|
|
|
|
|
|
log = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return if !log
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
activity_stream_log( 'updated', self['updated_by_id'] )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
delete object activity stream, will be executed automatically
|
2013-09-28 00:07:11 +00:00
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.activity_stream_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def activity_stream_destroy
|
2014-01-27 22:59:41 +00:00
|
|
|
return if !self.class.activity_stream_support_config
|
2013-09-28 00:07:11 +00:00
|
|
|
ActivityStream.remove( self.class.to_s, self.id )
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
serve methode to configure and enable history support for this model
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
history_support
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
history_support :ignore_attributes => { :article_count => true }
|
|
|
|
end
|
2013-09-25 19:50:28 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
def self.history_support(data = {})
|
|
|
|
@history_support_config = data
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
log object create history, if configured - will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.history_create
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def history_create
|
|
|
|
return if !self.class.history_support_config
|
2013-09-29 17:22:13 +00:00
|
|
|
#puts 'create ' + self.changes.inspect
|
2013-09-29 16:40:42 +00:00
|
|
|
self.history_log( 'created', self.created_by_id )
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
log object update history with all updated attributes, if configured - will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.history_update
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def history_update
|
|
|
|
return if !self.class.history_support_config
|
2013-09-25 19:50:28 +00:00
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
return if !self.changed?
|
|
|
|
|
|
|
|
# return if it's no update
|
|
|
|
return if self.new_record?
|
|
|
|
|
|
|
|
# new record also triggers update, so ignore new records
|
|
|
|
changes = self.changes
|
2013-09-29 22:45:58 +00:00
|
|
|
if self.history_changes_last_done
|
|
|
|
self.history_changes_last_done.each {|key, value|
|
|
|
|
if changes.has_key?(key) && changes[key] == value
|
|
|
|
changes.delete(key)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
self.history_changes_last_done = changes
|
2013-09-29 17:22:13 +00:00
|
|
|
#puts 'updated ' + self.changes.inspect
|
2013-09-29 22:45:58 +00:00
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
return if changes['id'] && !changes['id'][0]
|
|
|
|
|
2013-09-29 21:37:49 +00:00
|
|
|
# default ignored attributes
|
2013-09-29 16:40:42 +00:00
|
|
|
ignore_attributes = {
|
|
|
|
:created_at => true,
|
|
|
|
:updated_at => true,
|
|
|
|
:created_by_id => true,
|
|
|
|
:updated_by_id => true,
|
|
|
|
}
|
2013-09-29 22:45:58 +00:00
|
|
|
if self.class.history_support_config[:ignore_attributes]
|
|
|
|
self.class.history_support_config[:ignore_attributes].each {|key, value|
|
|
|
|
ignore_attributes[key] = value
|
|
|
|
}
|
|
|
|
end
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
changes.each {|key, value|
|
|
|
|
|
|
|
|
# do not log created_at and updated_at attributes
|
|
|
|
next if ignore_attributes[key.to_sym] == true
|
|
|
|
|
|
|
|
# get attribute name
|
|
|
|
attribute_name = key.to_s
|
|
|
|
if attribute_name[-3,3] == '_id'
|
|
|
|
attribute_name = attribute_name[ 0, attribute_name.length-3 ]
|
|
|
|
end
|
2013-09-29 17:22:13 +00:00
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
value_id = []
|
2013-09-29 22:45:58 +00:00
|
|
|
value_str = [ value[0], value[1] ]
|
2013-09-29 16:40:42 +00:00
|
|
|
if key.to_s[-3,3] == '_id'
|
|
|
|
value_id[0] = value[0]
|
|
|
|
value_id[1] = value[1]
|
|
|
|
|
|
|
|
if self.respond_to?( attribute_name )
|
|
|
|
relation_class = self.send(attribute_name).class
|
2013-09-29 21:37:49 +00:00
|
|
|
if relation_class && value_id[0]
|
2013-09-29 16:40:42 +00:00
|
|
|
relation_model = relation_class.lookup( :id => value_id[0] )
|
|
|
|
if relation_model
|
|
|
|
if relation_model['name']
|
2013-09-29 22:45:58 +00:00
|
|
|
value_str[0] = relation_model['name']
|
2013-09-29 16:40:42 +00:00
|
|
|
elsif relation_model.respond_to?('fullname')
|
2013-09-29 22:45:58 +00:00
|
|
|
value_str[0] = relation_model.send('fullname')
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
2013-09-29 21:37:49 +00:00
|
|
|
end
|
|
|
|
if relation_class && value_id[1]
|
2013-09-29 16:40:42 +00:00
|
|
|
relation_model = relation_class.lookup( :id => value_id[1] )
|
|
|
|
if relation_model
|
|
|
|
if relation_model['name']
|
2013-09-29 22:45:58 +00:00
|
|
|
value_str[1] = relation_model['name']
|
2013-09-29 16:40:42 +00:00
|
|
|
elsif relation_model.respond_to?('fullname')
|
2013-09-29 22:45:58 +00:00
|
|
|
value_str[1] = relation_model.send('fullname')
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
data = {
|
|
|
|
:history_attribute => attribute_name,
|
2013-09-29 22:45:58 +00:00
|
|
|
:value_from => value_str[0].to_s,
|
|
|
|
:value_to => value_str[1].to_s,
|
2013-09-29 16:40:42 +00:00
|
|
|
:id_from => value_id[0],
|
|
|
|
:id_to => value_id[1],
|
|
|
|
}
|
2013-09-29 17:22:13 +00:00
|
|
|
#puts "HIST NEW #{self.class.to_s}.find(#{self.id}) #{data.inspect}"
|
2013-09-29 16:40:42 +00:00
|
|
|
self.history_log( 'updated', self.updated_by_id, data )
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete object history, will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.history_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def history_destroy
|
2014-01-27 22:59:41 +00:00
|
|
|
return if !self.class.history_support_config
|
2013-09-25 19:50:28 +00:00
|
|
|
History.remove( self.class.to_s, self.id )
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
check string/varchar size and cut them if needed
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def check_limits
|
|
|
|
self.attributes.each {|attribute|
|
|
|
|
next if !self[ attribute[0] ]
|
|
|
|
next if self[ attribute[0] ].class != String
|
|
|
|
next if self[ attribute[0] ].empty?
|
|
|
|
column = self.class.columns_hash[ attribute[0] ]
|
|
|
|
limit = column.limit
|
|
|
|
if column && limit
|
|
|
|
current_length = attribute[1].to_s.length
|
|
|
|
if limit < current_length
|
|
|
|
puts "WARNING: cut string because of database length #{self.class.to_s}.#{attribute[0]}(#{limit} but is #{current_length}:#{attribute[1].to_s})"
|
|
|
|
self[attribute[0]] = attribute[1][ 0, limit ]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-09-29 16:40:42 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
destory object dependencies, will be executed automatically
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def destroy_dependencies
|
2013-09-25 19:50:28 +00:00
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|