2016-10-19 03:11:36 +00:00
|
|
|
# Copyright (C) 2012-2016 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
|
2015-02-01 12:36:22 +00:00
|
|
|
before_update :check_limits, :fill_up_user_update
|
|
|
|
before_destroy :destroy_dependencies
|
2013-09-25 19:50:28 +00:00
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
after_create :cache_delete
|
|
|
|
after_update :cache_delete
|
2015-02-01 12:08:11 +00:00
|
|
|
after_touch :cache_delete
|
2013-01-01 20:29:26 +00:00
|
|
|
after_destroy :cache_delete
|
|
|
|
|
2014-02-05 12:22:14 +00:00
|
|
|
after_create :attachments_buffer_check
|
|
|
|
after_update :attachments_buffer_check
|
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
after_create :activity_stream_create
|
|
|
|
after_update :activity_stream_update
|
2014-08-26 14:24:44 +00:00
|
|
|
before_destroy :activity_stream_destroy
|
2013-09-28 00:07:11 +00:00
|
|
|
|
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
|
2016-06-14 11:27:15 +00:00
|
|
|
after_touch :search_index_update
|
2014-01-27 22:59:41 +00:00
|
|
|
after_destroy :search_index_destroy
|
|
|
|
|
2014-08-26 14:24:44 +00:00
|
|
|
before_destroy :recent_view_destroy
|
2014-08-23 22:29:04 +00:00
|
|
|
|
2013-09-28 00:07:11 +00:00
|
|
|
# create instance accessor
|
2013-09-29 16:40:42 +00:00
|
|
|
class << self
|
2016-08-19 11:02:34 +00:00
|
|
|
attr_accessor :activity_stream_support_config, :history_support_config, :search_index_support_config, :attributes_with_associations_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-08-22 21:15:23 +00:00
|
|
|
def check_attributes_protected
|
2015-04-30 15:25:04 +00:00
|
|
|
|
2015-05-08 13:47:27 +00:00
|
|
|
import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ]
|
|
|
|
|
2015-04-30 15:25:04 +00:00
|
|
|
# do noting, use id as it is
|
2015-05-08 13:47:27 +00:00
|
|
|
return if !Setting.get('system_init_done')
|
2016-03-08 06:32:58 +00:00
|
|
|
return if Setting.get('import_mode') && import_class_list.include?(self.class.to_s)
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
self[:id] = nil
|
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)
|
|
|
|
|
2015-02-06 16:13:13 +00:00
|
|
|
for object creation, ignore id's
|
|
|
|
|
|
|
|
result = Model.param_cleanup(params, true)
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
returns
|
|
|
|
|
|
|
|
result = params # params with valid attributes of model
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
def self.param_cleanup(params, new_object = false)
|
|
|
|
|
|
|
|
if params.respond_to?('permit!')
|
|
|
|
params.permit!
|
|
|
|
end
|
2013-06-19 20:44:18 +00:00
|
|
|
|
2015-05-07 10:11:45 +00:00
|
|
|
if params.nil?
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "No params for #{self}!"
|
2013-09-20 11:40:58 +00:00
|
|
|
end
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
data = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
params.each { |key, value|
|
2016-06-06 15:26:37 +00:00
|
|
|
data[key.to_sym] = value
|
|
|
|
}
|
|
|
|
|
2015-02-06 16:13:13 +00:00
|
|
|
# ignore id for new objects
|
2016-06-06 15:26:37 +00:00
|
|
|
if new_object && params[:id]
|
|
|
|
data.delete(:id)
|
2015-02-06 16:13:13 +00:00
|
|
|
end
|
|
|
|
|
2013-06-19 20:44:18 +00:00
|
|
|
# only use object attributes
|
2016-06-06 15:26:37 +00:00
|
|
|
clean_params = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
new.attributes.each { |attribute, _value|
|
2016-06-06 15:26:37 +00:00
|
|
|
next if !data.key?(attribute.to_sym)
|
|
|
|
clean_params[attribute.to_sym] = data[attribute.to_sym]
|
2012-09-20 12:08:02 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 20:44:18 +00:00
|
|
|
# we do want to set this via database
|
2016-06-06 15:26:37 +00:00
|
|
|
param_validation(clean_params)
|
2013-06-19 20:44:18 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
2016-03-16 07:05:03 +00:00
|
|
|
set relations of model based on params
|
2013-09-30 23:37:45 +00:00
|
|
|
|
2014-05-16 13:09:17 +00:00
|
|
|
model = Model.find(1)
|
|
|
|
result = model.param_set_associations(params)
|
2013-09-30 23:37:45 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = true|false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def param_set_associations(params)
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# set relations by id/verify if ref exists
|
2013-09-30 23:37:45 +00:00
|
|
|
self.class.reflect_on_all_associations.map { |assoc|
|
2016-06-06 15:26:37 +00:00
|
|
|
real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
|
|
|
|
real_ids = real_ids.to_sym
|
|
|
|
next if !params.key?(real_ids)
|
|
|
|
list_of_items = params[real_ids]
|
|
|
|
if params[real_ids].class != Array
|
|
|
|
list_of_items = [ params[real_ids] ]
|
|
|
|
end
|
|
|
|
list = []
|
2016-06-30 20:04:48 +00:00
|
|
|
list_of_items.each { |item_id|
|
2016-06-06 15:26:37 +00:00
|
|
|
next if !item_id
|
|
|
|
lookup = assoc.klass.lookup(id: item_id)
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# complain if we found no reference
|
|
|
|
if !lookup
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "No value found for '#{assoc.name}' with id #{item_id.inspect}"
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
list.push item_id
|
|
|
|
}
|
|
|
|
#p "SEND #{real_ids} = #{list.inspect}"
|
|
|
|
send("#{real_ids}=", list)
|
|
|
|
}
|
2015-05-07 09:04:40 +00:00
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# set relations by name/lookup
|
|
|
|
self.class.reflect_on_all_associations.map { |assoc|
|
|
|
|
real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
|
|
|
|
next if !respond_to?(real_ids)
|
|
|
|
real_values = assoc.name.to_s[0, assoc.name.to_s.length - 1] + 's'
|
|
|
|
real_values = real_values.to_sym
|
|
|
|
next if !respond_to?(real_values)
|
|
|
|
next if !params[real_values]
|
|
|
|
next if params[real_values].class != Array
|
2015-05-07 09:04:40 +00:00
|
|
|
list = []
|
2016-06-06 15:26:37 +00:00
|
|
|
class_object = assoc.klass
|
2016-06-30 20:04:48 +00:00
|
|
|
params[real_values].each { |value|
|
2016-06-06 15:26:37 +00:00
|
|
|
lookup = nil
|
|
|
|
if class_object == User
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(login: value)
|
|
|
|
end
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(email: value)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
lookup = class_object.lookup(name: value)
|
|
|
|
end
|
|
|
|
|
|
|
|
# complain if we found no reference
|
|
|
|
if !lookup
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
list.push lookup.id
|
2015-05-07 09:04:40 +00:00
|
|
|
}
|
2016-06-06 15:26:37 +00:00
|
|
|
#p "SEND #{real_ids} = #{list.inspect}"
|
|
|
|
send("#{real_ids}=", list)
|
2013-09-30 23:37:45 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-06-08 04:56:05 +00:00
|
|
|
get relations of model based on params
|
2014-05-16 13:09:17 +00:00
|
|
|
|
|
|
|
model = Model.find(1)
|
|
|
|
attributes = model.attributes_with_associations
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
hash with attributes and association ids
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def attributes_with_associations
|
|
|
|
|
2016-08-18 21:43:07 +00:00
|
|
|
key = "#{self.class}::aws::#{id}"
|
|
|
|
cache = Cache.get(key)
|
|
|
|
return cache if cache
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# get relations
|
2014-05-16 13:09:17 +00:00
|
|
|
attributes = self.attributes
|
|
|
|
self.class.reflect_on_all_associations.map { |assoc|
|
2016-06-06 15:26:37 +00:00
|
|
|
real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
|
2016-08-19 11:02:34 +00:00
|
|
|
next if self.class.attributes_with_associations_support_config && self.class.attributes_with_associations_support_config[:ignore][real_ids.to_sym] == true
|
2016-06-06 15:26:37 +00:00
|
|
|
next if !respond_to?(real_ids)
|
|
|
|
attributes[real_ids] = send(real_ids)
|
2014-05-16 13:09:17 +00:00
|
|
|
}
|
2016-08-18 21:43:07 +00:00
|
|
|
Cache.write(key, attributes)
|
2014-05-16 13:09:17 +00:00
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-06-08 04:56:05 +00:00
|
|
|
get relation name of model based on params
|
|
|
|
|
|
|
|
model = Model.find(1)
|
|
|
|
attributes = model.attributes_with_relation_names
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
hash with attributes, association ids, association names and relation name
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def attributes_with_relation_names
|
|
|
|
|
|
|
|
# get relations
|
|
|
|
attributes = attributes_with_associations
|
|
|
|
self.class.reflect_on_all_associations.map { |assoc|
|
|
|
|
next if !respond_to?(assoc.name)
|
|
|
|
ref = send(assoc.name)
|
|
|
|
next if !ref
|
|
|
|
if ref.respond_to?(:first)
|
|
|
|
attributes[assoc.name.to_s] = []
|
2016-06-30 20:04:48 +00:00
|
|
|
ref.each { |item|
|
2016-06-08 04:56:05 +00:00
|
|
|
if item[:login]
|
|
|
|
attributes[assoc.name.to_s].push item[:login]
|
|
|
|
next
|
|
|
|
end
|
|
|
|
next if !item[:name]
|
|
|
|
attributes[assoc.name.to_s].push item[:name]
|
|
|
|
}
|
2016-10-24 21:59:18 +00:00
|
|
|
if ref.count.positive? && attributes[assoc.name.to_s].empty?
|
2016-06-08 04:56:05 +00:00
|
|
|
attributes.delete(assoc.name.to_s)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
if ref[:login]
|
|
|
|
attributes[assoc.name.to_s] = ref[:login]
|
|
|
|
next
|
|
|
|
end
|
|
|
|
next if !ref[:name]
|
|
|
|
attributes[assoc.name.to_s] = ref[:name]
|
|
|
|
}
|
|
|
|
|
|
|
|
# fill created_by/updated_by
|
|
|
|
{
|
|
|
|
'created_by_id' => 'created_by',
|
|
|
|
'updated_by_id' => 'updated_by',
|
2016-06-30 20:04:48 +00:00
|
|
|
}.each { |source, destination|
|
2016-06-08 04:56:05 +00:00
|
|
|
next if !attributes[source]
|
|
|
|
user = User.lookup(id: attributes[source])
|
|
|
|
next if !user
|
|
|
|
attributes[destination] = user.login
|
|
|
|
}
|
|
|
|
|
|
|
|
# remove forbitten attributes
|
2016-06-30 20:04:48 +00:00
|
|
|
%w(password token tokens token_ids).each { |item|
|
2016-06-08 04:56:05 +00:00
|
|
|
attributes.delete(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
attributes
|
|
|
|
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
|
2016-06-30 20:04:48 +00:00
|
|
|
[:action, :controller, :updated_at, :created_at, :updated_by_id, :created_by_id, :updated_by, :created_by].each { |key|
|
2016-06-06 15:26:37 +00:00
|
|
|
data.delete(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
data
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
do name/login/email based lookup for associations
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
params = {
|
|
|
|
login: 'some login',
|
|
|
|
firstname: 'some firstname',
|
|
|
|
lastname: 'some lastname',
|
|
|
|
email: 'some email',
|
|
|
|
organization: 'some organization',
|
|
|
|
roles: ['Agent', 'Admin'],
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
attributes = Model.param_association_lookup(params)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
attributes = params # params with possible lookups
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
attributes = {
|
|
|
|
login: 'some login',
|
|
|
|
firstname: 'some firstname',
|
|
|
|
lastname: 'some lastname',
|
|
|
|
email: 'some email',
|
|
|
|
organization_id: 123,
|
|
|
|
role_ids: [2,1],
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
=end
|
|
|
|
|
|
|
|
def self.param_association_lookup(params)
|
|
|
|
|
|
|
|
data = {}
|
2016-06-30 20:04:48 +00:00
|
|
|
params.each { |key, value|
|
2016-06-06 15:26:37 +00:00
|
|
|
data[key.to_sym] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
data.symbolize_keys!
|
|
|
|
available_attributes = attribute_names
|
|
|
|
reflect_on_all_associations.map { |assoc|
|
|
|
|
value = data[assoc.name.to_sym]
|
|
|
|
next if !value # next if we do not have a value
|
|
|
|
ref_name = "#{assoc.name}_id"
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
# handle _id values
|
|
|
|
if available_attributes.include?(ref_name) # if we do have an _id attribute
|
|
|
|
next if data[ref_name.to_sym] # next if we have already the _id filled
|
|
|
|
|
|
|
|
# get association class and do lookup
|
|
|
|
class_object = assoc.klass
|
|
|
|
lookup = nil
|
|
|
|
if class_object == User
|
|
|
|
if value.class == String
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(login: value)
|
|
|
|
end
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(email: value)
|
|
|
|
end
|
|
|
|
else
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "String is needed as ref value #{value.inspect} for '#{assoc.name}'"
|
2016-06-18 14:54:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
lookup = class_object.lookup(name: value)
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
2016-06-18 14:54:26 +00:00
|
|
|
|
|
|
|
# complain if we found no reference
|
2016-06-06 15:26:37 +00:00
|
|
|
if !lookup
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
# release data value
|
|
|
|
data.delete(assoc.name.to_sym)
|
|
|
|
|
|
|
|
# remember id reference
|
|
|
|
data[ref_name.to_sym] = lookup.id
|
|
|
|
next
|
2016-06-06 15:26:37 +00:00
|
|
|
end
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
next if value.class != Array
|
|
|
|
next if value.empty?
|
|
|
|
next if value[0].class != String
|
|
|
|
|
|
|
|
# handle _ids values
|
|
|
|
ref_names = "#{assoc.name[0, assoc.name.length - 1]}_ids"
|
|
|
|
generic_object_tmp = new
|
|
|
|
next unless generic_object_tmp.respond_to?(ref_names) # if we do have an _ids attribute
|
|
|
|
next if data[ref_names.to_sym] # next if we have already the _ids filled
|
|
|
|
|
|
|
|
# get association class and do lookup
|
|
|
|
class_object = assoc.klass
|
|
|
|
lookup_ids = []
|
2016-06-30 20:04:48 +00:00
|
|
|
value.each { |item|
|
2016-06-18 14:54:26 +00:00
|
|
|
lookup = nil
|
|
|
|
if class_object == User
|
|
|
|
if item.class == String
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(login: item)
|
|
|
|
end
|
|
|
|
if !lookup
|
|
|
|
lookup = class_object.lookup(email: item)
|
|
|
|
end
|
|
|
|
else
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "String is needed in array ref as ref value #{value.inspect} for '#{assoc.name}'"
|
2016-06-18 14:54:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
lookup = class_object.lookup(name: item)
|
|
|
|
end
|
|
|
|
|
|
|
|
# complain if we found no reference
|
|
|
|
if !lookup
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, "No lookup value found for '#{assoc.name}': #{item.inspect}"
|
2016-06-18 14:54:26 +00:00
|
|
|
end
|
|
|
|
lookup_ids.push lookup.id
|
|
|
|
}
|
|
|
|
|
2016-06-06 15:26:37 +00:00
|
|
|
# release data value
|
|
|
|
data.delete(assoc.name.to_sym)
|
|
|
|
|
|
|
|
# remember id reference
|
2016-06-18 14:54:26 +00:00
|
|
|
data[ref_names.to_sym] = lookup_ids
|
2016-06-06 15:26:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
2015-05-07 12:10:38 +00:00
|
|
|
if updated_by_id && updated_by_id != UserInfo.current_user_id
|
|
|
|
logger.info "NOTICE create - self.updated_by_id is different: #{updated_by_id}/#{UserInfo.current_user_id}"
|
2013-04-20 08:52:17 +00:00
|
|
|
end
|
|
|
|
self.updated_by_id = UserInfo.current_user_id
|
|
|
|
end
|
|
|
|
end
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
return if !self.class.column_names.include? 'created_by_id'
|
|
|
|
|
|
|
|
return if !UserInfo.current_user_id
|
|
|
|
|
2015-05-07 12:10:38 +00:00
|
|
|
if created_by_id && created_by_id != UserInfo.current_user_id
|
|
|
|
logger.info "NOTICE create - self.created_by_id is different: #{created_by_id}/#{UserInfo.current_user_id}"
|
2013-04-20 08:52:17 +00:00
|
|
|
end
|
2015-04-30 15:25:04 +00:00
|
|
|
self.created_by_id = UserInfo.current_user_id
|
2013-04-20 08:52:17 +00:00
|
|
|
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'
|
2015-04-30 15:25:04 +00:00
|
|
|
return if !UserInfo.current_user_id
|
|
|
|
|
|
|
|
self.updated_by_id = UserInfo.current_user_id
|
2013-04-20 08:52:17 +00:00
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
def cache_update(o)
|
2016-01-15 17:22:57 +00:00
|
|
|
cache_delete if respond_to?('cache_delete')
|
2015-05-04 19:11:20 +00:00
|
|
|
o.cache_delete if o.respond_to?('cache_delete')
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
def cache_delete
|
2016-09-04 21:24:19 +00:00
|
|
|
keys = []
|
2015-02-01 12:36:22 +00:00
|
|
|
|
2016-08-18 21:43:07 +00:00
|
|
|
# delete by id caches
|
2016-09-04 21:24:19 +00:00
|
|
|
keys.push "#{self.class}::#{id}"
|
2015-02-01 12:36:22 +00:00
|
|
|
|
2016-08-18 21:43:07 +00:00
|
|
|
# delete by id with attributes_with_associations caches
|
2016-09-04 21:24:19 +00:00
|
|
|
keys.push "#{self.class}::aws::#{id}"
|
|
|
|
|
|
|
|
# delete by name caches
|
|
|
|
if self[:name]
|
|
|
|
keys.push "#{self.class}::#{name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# delete by login caches
|
|
|
|
if self[:login]
|
|
|
|
keys.push "#{self.class}::#{login}"
|
|
|
|
end
|
|
|
|
|
|
|
|
keys.each { |key|
|
|
|
|
Cache.delete(key)
|
|
|
|
}
|
2016-08-18 21:43:07 +00:00
|
|
|
|
2015-02-01 12:36:22 +00:00
|
|
|
# delete old name / login caches
|
2016-01-15 17:22:57 +00:00
|
|
|
if changed?
|
2015-05-07 12:10:38 +00:00
|
|
|
if changes.key?('name')
|
2015-06-18 14:23:03 +00:00
|
|
|
name = changes['name'][0]
|
|
|
|
key = "#{self.class}::#{name}"
|
2016-09-04 21:24:19 +00:00
|
|
|
Cache.delete(key)
|
2015-02-01 12:36:22 +00:00
|
|
|
end
|
2015-05-07 12:10:38 +00:00
|
|
|
if changes.key?('login')
|
2015-06-18 14:23:03 +00:00
|
|
|
name = changes['login'][0]
|
|
|
|
key = "#{self.class}::#{name}"
|
|
|
|
Cache.delete(key)
|
2015-02-01 12:36:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2013-03-11 21:15:21 +00:00
|
|
|
|
2015-06-18 12:32:06 +00:00
|
|
|
def self.cache_set(data_id, data)
|
2015-06-18 14:23:03 +00:00
|
|
|
key = "#{self}::#{data_id}"
|
|
|
|
Cache.write(key, data)
|
2012-04-16 08:04:49 +00:00
|
|
|
end
|
2015-02-01 12:36:22 +00:00
|
|
|
|
2015-06-18 12:32:06 +00:00
|
|
|
def self.cache_get(data_id)
|
2015-06-18 14:23:03 +00:00
|
|
|
key = "#{self}::#{data_id}"
|
|
|
|
Cache.get(key)
|
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
|
|
|
|
|
2015-09-22 14:48:43 +00:00
|
|
|
generate uniq name (will check name of model and generates _1 sequenze)
|
|
|
|
|
|
|
|
Used as before_update callback, no own use needed
|
|
|
|
|
|
|
|
name = Model.genrate_uniq_name('some name')
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = 'some name_X'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.genrate_uniq_name(name)
|
|
|
|
return name if !find_by(name: name)
|
2016-06-30 20:04:48 +00:00
|
|
|
(1..100).each { |counter|
|
2015-09-22 14:48:43 +00:00
|
|
|
name = "#{name}_#{counter}"
|
|
|
|
exists = find_by(name: name)
|
|
|
|
next if exists
|
|
|
|
break
|
|
|
|
}
|
|
|
|
name
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2016-01-19 22:30:23 +00:00
|
|
|
lookup model from cache (if exists) or retrieve it from db, id, name, login or email possible
|
2013-08-17 20:12:27 +00:00
|
|
|
|
2015-10-27 07:41:55 +00:00
|
|
|
result = Model.lookup(id: 123)
|
|
|
|
result = Model.lookup(name: 'some name')
|
|
|
|
result = Model.lookup(login: 'some login')
|
2016-01-19 22:30:23 +00:00
|
|
|
result = Model.lookup(email: 'some login')
|
2013-08-17 20:12:27 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-01-01 20:29:26 +00:00
|
|
|
def self.lookup(data)
|
|
|
|
if data[:id]
|
2015-12-13 12:08:58 +00:00
|
|
|
cache = cache_get(data[:id])
|
2013-01-01 20:29:26 +00:00
|
|
|
return cache if cache
|
|
|
|
|
2015-12-13 12:08:58 +00:00
|
|
|
record = find_by(id: data[:id])
|
|
|
|
cache_set(data[:id], record)
|
2013-01-01 20:29:26 +00:00
|
|
|
return record
|
|
|
|
elsif data[:name]
|
2015-12-13 12:08:58 +00:00
|
|
|
cache = cache_get(data[:name])
|
2013-01-01 20:29:26 +00:00
|
|
|
return cache if cache
|
|
|
|
|
2015-07-28 13:19:54 +00:00
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(name) = LOWER(?)', data[:name])
|
|
|
|
else
|
|
|
|
where(name: data[:name])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-05-08 10:33:08 +00:00
|
|
|
if loop_record.name == data[:name]
|
2015-12-13 12:08:58 +00:00
|
|
|
cache_set(data[:name], loop_record)
|
2015-05-08 10:33:08 +00:00
|
|
|
return loop_record
|
2013-01-04 08:00:16 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
return
|
2013-01-04 18:42:20 +00:00
|
|
|
elsif data[:login]
|
2015-12-13 12:08:58 +00:00
|
|
|
cache = cache_get(data[:login])
|
2013-01-04 18:42:20 +00:00
|
|
|
return cache if cache
|
|
|
|
|
2015-07-28 13:19:54 +00:00
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(login) = LOWER(?)', data[:login])
|
|
|
|
else
|
|
|
|
where(login: data[:login])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-05-08 10:33:08 +00:00
|
|
|
if loop_record.login == data[:login]
|
2016-01-19 22:30:23 +00:00
|
|
|
cache_set(data[:login], loop_record)
|
|
|
|
return loop_record
|
|
|
|
end
|
|
|
|
}
|
|
|
|
return
|
|
|
|
elsif data[:email]
|
|
|
|
cache = cache_get(data[:email])
|
|
|
|
return cache if cache
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(email) = LOWER(?)', data[:email])
|
|
|
|
else
|
|
|
|
where(email: data[:email])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2016-01-19 22:30:23 +00:00
|
|
|
if loop_record.email == data[:email]
|
|
|
|
cache_set(data[:email], loop_record)
|
2015-05-08 10:33:08 +00:00
|
|
|
return loop_record
|
2013-01-04 18:42:20 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
return
|
2013-01-01 20:29:26 +00:00
|
|
|
end
|
2016-01-15 17:22:57 +00:00
|
|
|
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, 'Need name, id, login or email for lookup()'
|
2013-01-01 20:29:26 +00:00
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
2015-09-11 17:19:13 +00:00
|
|
|
create model if not exists (check exists based on id, name, login, email or locale)
|
2013-08-17 20:12:27 +00:00
|
|
|
|
2015-12-13 12:08:58 +00:00
|
|
|
result = Model.create_if_not_exists(attributes)
|
2013-08-17 20:12:27 +00:00
|
|
|
|
|
|
|
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]
|
2015-12-13 12:08:58 +00:00
|
|
|
record = find_by(id: data[:id])
|
2015-07-27 18:42:52 +00:00
|
|
|
return record if record
|
2013-01-04 18:42:20 +00:00
|
|
|
elsif data[:name]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(name) = LOWER(?)', data[:name])
|
|
|
|
else
|
|
|
|
where(name: data[:name])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-07-27 18:42:52 +00:00
|
|
|
return loop_record if loop_record.name == data[:name]
|
|
|
|
}
|
2013-01-04 18:42:20 +00:00
|
|
|
elsif data[:login]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(login) = LOWER(?)', data[:login])
|
|
|
|
else
|
|
|
|
where(login: data[:login])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-07-27 18:42:52 +00:00
|
|
|
return loop_record if loop_record.login == data[:login]
|
|
|
|
}
|
2015-09-11 17:19:13 +00:00
|
|
|
elsif data[:email]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(email) = LOWER(?)', data[:email])
|
|
|
|
else
|
|
|
|
where(email: data[:email])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-09-11 17:19:13 +00:00
|
|
|
return loop_record if loop_record.email == data[:email]
|
|
|
|
}
|
2012-12-27 20:17:33 +00:00
|
|
|
elsif data[:locale] && data[:source]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(locale) = LOWER(?) AND LOWER(source) = LOWER(?)', data[:locale], data[:source])
|
|
|
|
else
|
|
|
|
where(locale: data[:locale], source: data[:source])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-07-27 18:42:52 +00:00
|
|
|
return loop_record if loop_record.source == data[:source]
|
|
|
|
}
|
2012-12-27 20:17:33 +00:00
|
|
|
end
|
2015-05-07 12:10:38 +00:00
|
|
|
create(data)
|
2012-12-27 20:17:33 +00:00
|
|
|
end
|
2012-12-30 09:45:01 +00:00
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
Model.create_if_not_exists with ref lookups
|
|
|
|
|
|
|
|
result = Model.create_if_not_exists_with_ref(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.create_if_not_exists_with_ref(data)
|
|
|
|
data = param_association_lookup(data)
|
|
|
|
create_or_update(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-09-11 17:19:13 +00:00
|
|
|
create or update model (check exists based on id, name, login, email or locale)
|
2013-08-17 20:12:27 +00:00
|
|
|
|
2015-12-13 12:08:58 +00:00
|
|
|
result = Model.create_or_update(attributes)
|
2013-08-17 20:12:27 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2012-12-30 09:45:01 +00:00
|
|
|
def self.create_or_update(data)
|
2015-01-12 07:31:30 +00:00
|
|
|
if data[:id]
|
2015-12-13 12:08:58 +00:00
|
|
|
record = find_by(id: data[:id])
|
2015-07-28 13:19:54 +00:00
|
|
|
if record
|
2015-12-13 12:08:58 +00:00
|
|
|
record.update_attributes(data)
|
2015-07-28 13:19:54 +00:00
|
|
|
return record
|
|
|
|
end
|
2015-12-13 12:08:58 +00:00
|
|
|
record = new(data)
|
2015-01-12 07:31:30 +00:00
|
|
|
record.save
|
|
|
|
return record
|
|
|
|
elsif data[:name]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(name) = LOWER(?)', data[:name])
|
|
|
|
else
|
|
|
|
where(name: data[:name])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2015-05-08 10:33:08 +00:00
|
|
|
if loop_record.name == data[:name]
|
2015-12-13 12:08:58 +00:00
|
|
|
loop_record.update_attributes(data)
|
2015-05-08 10:33:08 +00:00
|
|
|
return loop_record
|
2013-01-04 08:00:16 +00:00
|
|
|
end
|
|
|
|
}
|
2015-12-13 12:08:58 +00:00
|
|
|
record = new(data)
|
2013-01-04 08:00:16 +00:00
|
|
|
record.save
|
2012-12-30 09:45:01 +00:00
|
|
|
return record
|
2013-07-19 14:21:44 +00:00
|
|
|
elsif data[:login]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(login) = LOWER(?)', data[:login])
|
|
|
|
else
|
|
|
|
where(login: data[:login])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2016-01-16 10:05:04 +00:00
|
|
|
if loop_record.login.casecmp(data[:login]).zero?
|
2015-12-13 12:08:58 +00:00
|
|
|
loop_record.update_attributes(data)
|
2015-05-08 10:33:08 +00:00
|
|
|
return loop_record
|
2013-07-19 14:21:44 +00:00
|
|
|
end
|
|
|
|
}
|
2015-12-13 12:08:58 +00:00
|
|
|
record = new(data)
|
2013-07-19 14:21:44 +00:00
|
|
|
record.save
|
|
|
|
return record
|
2015-09-11 17:19:13 +00:00
|
|
|
elsif data[:email]
|
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(email) = LOWER(?)', data[:email])
|
|
|
|
else
|
|
|
|
where(email: data[:email])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2016-01-16 10:05:04 +00:00
|
|
|
if loop_record.email.casecmp(data[:email]).zero?
|
2015-12-13 12:08:58 +00:00
|
|
|
loop_record.update_attributes(data)
|
2015-09-11 17:19:13 +00:00
|
|
|
return loop_record
|
|
|
|
end
|
|
|
|
}
|
2015-12-13 12:08:58 +00:00
|
|
|
record = new(data)
|
2015-09-11 17:19:13 +00:00
|
|
|
record.save
|
|
|
|
return record
|
2013-08-17 20:12:27 +00:00
|
|
|
elsif data[:locale]
|
2015-07-28 13:19:54 +00:00
|
|
|
|
|
|
|
# do lookup with == to handle case insensitive databases
|
2016-01-19 22:30:23 +00:00
|
|
|
records = if Rails.application.config.db_case_sensitive
|
|
|
|
where('LOWER(locale) = LOWER(?)', data[:locale])
|
|
|
|
else
|
|
|
|
where(locale: data[:locale])
|
|
|
|
end
|
2016-06-30 20:04:48 +00:00
|
|
|
records.each { |loop_record|
|
2016-01-16 10:05:04 +00:00
|
|
|
if loop_record.locale.casecmp(data[:locale]).zero?
|
2015-12-13 12:08:58 +00:00
|
|
|
loop_record.update_attributes(data)
|
2015-05-08 10:33:08 +00:00
|
|
|
return loop_record
|
2013-08-17 20:12:27 +00:00
|
|
|
end
|
|
|
|
}
|
2015-12-13 12:08:58 +00:00
|
|
|
record = new(data)
|
2013-08-17 20:12:27 +00:00
|
|
|
record.save
|
|
|
|
return record
|
2012-12-30 09:45:01 +00:00
|
|
|
else
|
2016-09-09 11:29:33 +00:00
|
|
|
raise ArgumentError, 'Need name, login, email 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
|
|
|
|
|
2016-06-18 14:54:26 +00:00
|
|
|
Model.create_or_update with ref lookups
|
|
|
|
|
|
|
|
result = Model.create_or_update(attributes)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = model # with all attributes
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.create_or_update_with_ref(data)
|
|
|
|
data = param_association_lookup(data)
|
|
|
|
create_or_update(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
activate latest change on create, update, touch and destroy
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
latest_change_support
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.latest_change_support
|
|
|
|
after_create :latest_change_set_from_observer
|
|
|
|
after_update :latest_change_set_from_observer
|
|
|
|
after_touch :latest_change_set_from_observer
|
|
|
|
after_destroy :latest_change_set_from_observer_destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
def latest_change_set_from_observer
|
2015-05-07 12:10:38 +00:00
|
|
|
self.class.latest_change_set(updated_at)
|
2015-02-25 20:52:14 +00:00
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
def latest_change_set_from_observer_destroy
|
|
|
|
self.class.latest_change_set(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.latest_change_set(updated_at)
|
2015-05-07 12:10:38 +00:00
|
|
|
key = "#{new.class.name}_latest_change"
|
2015-02-25 20:52:14 +00:00
|
|
|
expires_in = 31_536_000 # 1 year
|
|
|
|
|
2015-05-07 10:11:45 +00:00
|
|
|
if updated_at.nil?
|
2015-12-13 12:08:58 +00:00
|
|
|
Cache.delete(key)
|
2015-02-25 20:52:14 +00:00
|
|
|
else
|
2015-12-13 12:08:58 +00:00
|
|
|
Cache.write(key, updated_at, { expires_in: expires_in })
|
2015-02-25 20:52:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get latest updated_at object timestamp
|
|
|
|
|
|
|
|
latest_change = Ticket.latest_change
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
result = timestamp
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.latest_change
|
2015-05-07 12:10:38 +00:00
|
|
|
key = "#{new.class.name}_latest_change"
|
2016-03-08 06:32:58 +00:00
|
|
|
updated_at = Cache.get(key)
|
2015-05-04 19:11:20 +00:00
|
|
|
|
2015-02-25 20:52:14 +00:00
|
|
|
# if we do not have it cached, do lookup
|
|
|
|
if !updated_at
|
2015-05-07 12:10:38 +00:00
|
|
|
o = select(:updated_at).order(updated_at: :desc).limit(1).first
|
2015-02-25 20:52:14 +00:00
|
|
|
if o
|
|
|
|
updated_at = o.updated_at
|
2015-05-07 12:10:38 +00:00
|
|
|
latest_change_set(updated_at)
|
2015-02-25 20:52:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
updated_at
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-02-01 12:08:11 +00:00
|
|
|
activate client notify support on create, update, touch and destroy
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
notify_clients_support
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.notify_clients_support
|
2015-12-13 12:08:58 +00:00
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_touch :notify_clients_after_touch
|
|
|
|
after_destroy :notify_clients_after_destroy
|
2015-02-01 12:08:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
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
|
2015-04-27 13:29:32 +00:00
|
|
|
after_touch :notify_clients_after_touch
|
2013-08-17 20:12:27 +00:00
|
|
|
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')
|
2015-07-03 15:18:01 +00:00
|
|
|
logger.debug "#{self.class.name}.find(#{id}) notify created " + created_at.to_s
|
2013-06-29 00:13:28 +00:00
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2016-09-04 21:24:19 +00:00
|
|
|
PushMessages.send(
|
|
|
|
message: {
|
|
|
|
event: class_name + ':create',
|
|
|
|
data: { id: id, updated_at: updated_at }
|
|
|
|
},
|
|
|
|
type: 'authenticated',
|
2013-06-29 00:13:28 +00:00
|
|
|
)
|
|
|
|
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
|
2015-04-27 13:29:32 +00:00
|
|
|
after_touch :notify_clients_after_touch
|
2013-08-17 20:12:27 +00:00
|
|
|
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')
|
2015-07-03 15:18:01 +00:00
|
|
|
logger.debug "#{self.class.name}.find(#{id}) notify UPDATED " + updated_at.to_s
|
2013-06-29 00:13:28 +00:00
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2016-09-04 21:24:19 +00:00
|
|
|
PushMessages.send(
|
|
|
|
message: {
|
|
|
|
event: class_name + ':update',
|
|
|
|
data: { id: id, updated_at: updated_at }
|
|
|
|
},
|
|
|
|
type: 'authenticated',
|
2013-06-29 00:13:28 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
=begin
|
|
|
|
|
2015-04-27 13:29:32 +00:00
|
|
|
notify_clients_after_touch after model got touched
|
|
|
|
|
|
|
|
used as callback in model file
|
|
|
|
|
|
|
|
class OwnModel < ApplicationModel
|
|
|
|
after_create :notify_clients_after_create
|
|
|
|
after_update :notify_clients_after_update
|
|
|
|
after_touch :notify_clients_after_touch
|
|
|
|
after_destroy :notify_clients_after_destroy
|
|
|
|
|
|
|
|
[...]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def notify_clients_after_touch
|
|
|
|
|
|
|
|
# return if we run import mode
|
|
|
|
return if Setting.get('import_mode')
|
2015-07-03 15:18:01 +00:00
|
|
|
logger.debug "#{self.class.name}.find(#{id}) notify TOUCH " + updated_at.to_s
|
2015-04-27 13:29:32 +00:00
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2016-09-04 21:24:19 +00:00
|
|
|
PushMessages.send(
|
|
|
|
message: {
|
|
|
|
event: class_name + ':touch',
|
|
|
|
data: { id: id, updated_at: updated_at }
|
|
|
|
},
|
|
|
|
type: 'authenticated',
|
2015-04-27 13:29:32 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2013-08-17 20:12:27 +00:00
|
|
|
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
|
2015-04-27 13:29:32 +00:00
|
|
|
after_touch :notify_clients_after_touch
|
2013-08-17 20:12:27 +00:00
|
|
|
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')
|
2015-07-03 15:18:01 +00:00
|
|
|
logger.debug "#{self.class.name}.find(#{id}) notify DESTOY " + updated_at.to_s
|
2013-06-29 00:13:28 +00:00
|
|
|
class_name = self.class.name
|
|
|
|
class_name.gsub!(/::/, '')
|
2016-09-04 21:24:19 +00:00
|
|
|
PushMessages.send(
|
|
|
|
message: {
|
|
|
|
event: class_name + ':destroy',
|
|
|
|
data: { id: id, updated_at: updated_at }
|
|
|
|
},
|
|
|
|
type: 'authenticated',
|
2013-06-29 00:13:28 +00:00
|
|
|
)
|
|
|
|
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
|
2016-03-08 06:32:58 +00:00
|
|
|
search_index_support
|
|
|
|
ignore_attributes: {
|
|
|
|
create_article_type_id: true,
|
|
|
|
create_article_sender_id: true,
|
|
|
|
article_count: true,
|
|
|
|
},
|
|
|
|
ignore_ids: [1,2,4]
|
2014-01-27 22:59:41 +00:00
|
|
|
|
|
|
|
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
|
2016-03-08 06:32:58 +00:00
|
|
|
config = self.class.search_index_support_config
|
|
|
|
return if !config
|
|
|
|
return if config[:ignore_ids] && config[:ignore_ids].include?(id)
|
2014-02-03 18:27:49 +00:00
|
|
|
|
|
|
|
# start background job to transfer data to search index
|
|
|
|
return if !SearchIndexBackend.enabled?
|
2016-03-08 06:32:58 +00:00
|
|
|
Delayed::Job.enqueue(ApplicationModel::BackgroundJobSearchIndex.new(self.class.to_s, id))
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
delete search index object, will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.search_index_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def search_index_destroy
|
2016-03-08 06:32:58 +00:00
|
|
|
config = self.class.search_index_support_config
|
|
|
|
return if !config
|
|
|
|
return if config[:ignore_ids] && config[:ignore_ids].include?(id)
|
|
|
|
|
2015-12-13 12:08:58 +00:00
|
|
|
SearchIndexBackend.remove(self.class.to_s, id)
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
reload search index with full data
|
|
|
|
|
|
|
|
Model.search_index_reload
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.search_index_reload
|
2016-03-08 06:43:36 +00:00
|
|
|
config = @search_index_support_config
|
|
|
|
return if !config
|
2016-07-07 23:20:25 +00:00
|
|
|
tolerance = 5
|
|
|
|
tolerance_count = 0
|
2015-05-07 12:10:38 +00:00
|
|
|
all_ids = select('id').all.order('created_at DESC')
|
2014-12-09 19:11:25 +00:00
|
|
|
all_ids.each { |item_with_id|
|
2016-03-08 06:43:36 +00:00
|
|
|
next if config[:ignore_ids] && config[:ignore_ids].include?(item_with_id.id)
|
2016-03-08 06:32:58 +00:00
|
|
|
item = find(item_with_id.id)
|
2016-07-07 23:20:25 +00:00
|
|
|
begin
|
|
|
|
item.search_index_update_backend
|
|
|
|
rescue => e
|
|
|
|
logger.error "Unable to send #{item.class}.find(#{item.id}) backend: #{e.inspect}"
|
|
|
|
tolerance_count += 1
|
|
|
|
raise "Unable to send #{item.class}.find(#{item.id}) backend: #{e.inspect}" if tolerance_count == tolerance
|
|
|
|
end
|
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
|
2016-08-12 16:39:09 +00:00
|
|
|
activity_stream_support permission: 'admin.user'
|
2013-09-28 00:07:11 +00:00
|
|
|
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
|
2016-02-22 19:58:23 +00:00
|
|
|
activity_stream_log('create', self['created_by_id'])
|
2013-09-28 00:07:11 +00:00
|
|
|
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
|
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
return if !changed?
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
# default ignored attributes
|
|
|
|
ignore_attributes = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_at: true,
|
|
|
|
updated_at: true,
|
|
|
|
created_by_id: true,
|
|
|
|
updated_by_id: true,
|
2013-10-05 12:56:03 +00:00
|
|
|
}
|
|
|
|
if self.class.activity_stream_support_config[:ignore_attributes]
|
2016-06-30 20:04:48 +00:00
|
|
|
self.class.activity_stream_support_config[:ignore_attributes].each { |key, value|
|
2013-10-05 12:56:03 +00:00
|
|
|
ignore_attributes[key] = value
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
log = false
|
2016-06-30 20:04:48 +00:00
|
|
|
changes.each { |key, _value|
|
2013-10-05 12:56:03 +00:00
|
|
|
|
|
|
|
# do not log created_at and updated_at attributes
|
|
|
|
next if ignore_attributes[key.to_sym] == true
|
|
|
|
|
|
|
|
log = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return if !log
|
|
|
|
|
2016-02-22 19:58:23 +00:00
|
|
|
activity_stream_log('update', self['updated_by_id'])
|
2013-09-28 00:07:11 +00:00
|
|
|
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
|
2015-12-13 12:08:58 +00:00
|
|
|
ActivityStream.remove(self.class.to_s, id)
|
2013-09-28 00:07:11 +00:00
|
|
|
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
|
2015-10-27 07:41:55 +00:00
|
|
|
history_support ignore_attributes: { article_count: true }
|
2013-09-29 16:40:42 +00:00
|
|
|
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
|
2015-05-04 18:58:28 +00:00
|
|
|
#logger.debug 'create ' + self.changes.inspect
|
2015-12-13 12:08:58 +00:00
|
|
|
history_log('created', created_by_id)
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2016-01-15 17:22:57 +00:00
|
|
|
return if !changed?
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
# return if it's no update
|
2016-01-15 17:22:57 +00:00
|
|
|
return if new_record?
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
# new record also triggers update, so ignore new records
|
|
|
|
changes = self.changes
|
2015-05-07 12:10:38 +00:00
|
|
|
if history_changes_last_done
|
2016-06-30 20:04:48 +00:00
|
|
|
history_changes_last_done.each { |key, value|
|
2015-04-30 17:34:33 +00:00
|
|
|
if changes.key?(key) && changes[key] == value
|
2013-09-29 22:45:58 +00:00
|
|
|
changes.delete(key)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
self.history_changes_last_done = changes
|
2015-05-04 18:58:28 +00:00
|
|
|
#logger.info '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 = {
|
2015-04-27 13:42:53 +00:00
|
|
|
created_at: true,
|
|
|
|
updated_at: true,
|
|
|
|
created_by_id: true,
|
|
|
|
updated_by_id: true,
|
2013-09-29 16:40:42 +00:00
|
|
|
}
|
2013-09-29 22:45:58 +00:00
|
|
|
if self.class.history_support_config[:ignore_attributes]
|
2016-06-30 20:04:48 +00:00
|
|
|
self.class.history_support_config[:ignore_attributes].each { |key, value|
|
2013-09-29 22:45:58 +00:00
|
|
|
ignore_attributes[key] = value
|
|
|
|
}
|
|
|
|
end
|
2013-09-29 16:40:42 +00:00
|
|
|
|
2016-06-30 20:04:48 +00:00
|
|
|
changes.each { |key, value|
|
2013-09-29 16:40:42 +00:00
|
|
|
|
|
|
|
# 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
|
2015-04-27 14:53:29 +00:00
|
|
|
if attribute_name[-3, 3] == '_id'
|
2015-04-27 14:41:03 +00:00
|
|
|
attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
|
2013-09-29 16:40:42 +00:00
|
|
|
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] ]
|
2015-04-27 14:53:29 +00:00
|
|
|
if key.to_s[-3, 3] == '_id'
|
2013-09-29 16:40:42 +00:00
|
|
|
value_id[0] = value[0]
|
|
|
|
value_id[1] = value[1]
|
|
|
|
|
2016-03-08 06:32:58 +00:00
|
|
|
if respond_to?(attribute_name) && send(attribute_name)
|
2015-05-07 12:10:38 +00:00
|
|
|
relation_class = send(attribute_name).class
|
2013-09-29 21:37:49 +00:00
|
|
|
if relation_class && value_id[0]
|
2016-03-08 06:32:58 +00:00
|
|
|
relation_model = relation_class.lookup(id: value_id[0])
|
2013-09-29 16:40:42 +00:00
|
|
|
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]
|
2016-03-08 06:32:58 +00:00
|
|
|
relation_model = relation_class.lookup(id: value_id[1])
|
2013-09-29 16:40:42 +00:00
|
|
|
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 = {
|
2015-04-27 13:42:53 +00:00
|
|
|
history_attribute: attribute_name,
|
|
|
|
value_from: value_str[0].to_s,
|
|
|
|
value_to: value_str[1].to_s,
|
|
|
|
id_from: value_id[0],
|
|
|
|
id_to: value_id[1],
|
2013-09-29 16:40:42 +00:00
|
|
|
}
|
2015-05-04 18:58:28 +00:00
|
|
|
#logger.info "HIST NEW #{self.class.to_s}.find(#{self.id}) #{data.inspect}"
|
2015-12-13 12:08:58 +00:00
|
|
|
history_log('updated', updated_by_id, data)
|
2013-09-29 16:40:42 +00:00
|
|
|
}
|
|
|
|
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
|
2015-12-13 12:08:58 +00:00
|
|
|
History.remove(self.class.to_s, id)
|
2013-09-29 16:40:42 +00:00
|
|
|
end
|
2013-09-28 00:07:11 +00:00
|
|
|
|
2014-02-05 12:22:14 +00:00
|
|
|
=begin
|
|
|
|
|
2016-08-19 11:02:34 +00:00
|
|
|
serve methode to configure and attributes_with_associations support for this model
|
|
|
|
|
|
|
|
class Model < ApplicationModel
|
|
|
|
attributes_with_associations(ignore: { user_ids: => true })
|
|
|
|
end
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.attributes_with_associations_support(data = {})
|
|
|
|
@attributes_with_associations_support_config = data
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-02-05 12:22:14 +00:00
|
|
|
get list of attachments of this object
|
|
|
|
|
|
|
|
item = Model.find(123)
|
|
|
|
list = item.attachments
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
# array with Store model objects
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def attachments
|
2015-12-13 12:08:58 +00:00
|
|
|
Store.list(object: self.class.to_s, o_id: id)
|
2014-02-05 12:22:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
store attachments for this object
|
|
|
|
|
|
|
|
item = Model.find(123)
|
|
|
|
item.attachments = [ Store-Object1, Store-Object2 ]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def attachments=(attachments)
|
|
|
|
self.attachments_buffer = attachments
|
|
|
|
|
|
|
|
# update if object already exists
|
2016-07-26 22:02:28 +00:00
|
|
|
return if !(id && id.nonzero?)
|
2015-04-30 15:25:04 +00:00
|
|
|
|
|
|
|
attachments_buffer_check
|
2014-02-05 12:22:14 +00:00
|
|
|
end
|
|
|
|
|
2014-08-23 22:29:04 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
return object and assets
|
|
|
|
|
|
|
|
data = Model.full(123)
|
|
|
|
data = {
|
2015-10-27 07:41:55 +00:00
|
|
|
id: 123,
|
|
|
|
assets: assets,
|
2014-08-23 22:29:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.full(id)
|
2015-05-07 12:10:38 +00:00
|
|
|
object = find(id)
|
2014-08-23 22:29:04 +00:00
|
|
|
assets = object.assets({})
|
|
|
|
{
|
2015-04-27 13:42:53 +00:00
|
|
|
id: id,
|
|
|
|
assets: assets,
|
2014-08-23 22:29:04 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-08-26 07:40:25 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get assets of object list
|
|
|
|
|
|
|
|
list = [
|
|
|
|
{
|
|
|
|
object => 'Ticket',
|
|
|
|
o_id => 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
object => 'User',
|
|
|
|
o_id => 121,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
assets = Model.assets_of_object_list(list, assets)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.assets_of_object_list(list, assets = {})
|
2016-06-30 20:04:48 +00:00
|
|
|
list.each { |item|
|
2014-08-26 07:40:25 +00:00
|
|
|
require item['object'].to_filename
|
2015-12-13 12:08:58 +00:00
|
|
|
record = Kernel.const_get(item['object']).find(item['o_id'])
|
2014-08-26 07:40:25 +00:00
|
|
|
assets = record.assets(assets)
|
|
|
|
if item['created_by_id']
|
2015-12-13 12:08:58 +00:00
|
|
|
user = User.find(item['created_by_id'])
|
2014-08-26 07:40:25 +00:00
|
|
|
assets = user.assets(assets)
|
|
|
|
end
|
|
|
|
if item['updated_by_id']
|
2015-12-13 12:08:58 +00:00
|
|
|
user = User.find(item['updated_by_id'])
|
2014-08-26 07:40:25 +00:00
|
|
|
assets = user.assets(assets)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assets
|
|
|
|
end
|
|
|
|
|
2016-03-20 19:09:52 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get assets and record_ids of selector
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
|
|
|
|
assets = model.assets_of_selector('attribute_name_of_selector', assets)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def assets_of_selector(selector, assets = {})
|
|
|
|
|
|
|
|
# get assets of condition
|
|
|
|
models = Models.all
|
2016-06-30 20:04:48 +00:00
|
|
|
send(selector).each { |item, content|
|
2016-03-20 19:09:52 +00:00
|
|
|
attribute = item.split(/\./)
|
|
|
|
next if !attribute[1]
|
2016-05-20 08:44:00 +00:00
|
|
|
begin
|
|
|
|
attribute_class = attribute[0].to_classname.constantize
|
|
|
|
rescue => e
|
|
|
|
logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
|
|
|
|
next
|
|
|
|
end
|
2016-03-20 19:09:52 +00:00
|
|
|
reflection = attribute[1].sub(/_id$/, '')
|
|
|
|
#reflection = reflection.to_sym
|
|
|
|
next if !models[attribute_class]
|
|
|
|
next if !models[attribute_class][:reflections]
|
|
|
|
next if !models[attribute_class][:reflections][reflection]
|
|
|
|
next if !models[attribute_class][:reflections][reflection].klass
|
|
|
|
attribute_ref_class = models[attribute_class][:reflections][reflection].klass
|
|
|
|
if content['value'].class == Array
|
2016-06-30 20:04:48 +00:00
|
|
|
content['value'].each { |item_id|
|
2016-03-20 19:09:52 +00:00
|
|
|
attribute_object = attribute_ref_class.find_by(id: item_id)
|
|
|
|
if attribute_object
|
|
|
|
assets = attribute_object.assets(assets)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
else
|
|
|
|
attribute_object = attribute_ref_class.find_by(id: content['value'])
|
|
|
|
if attribute_object
|
|
|
|
assets = attribute_object.assets(assets)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
assets
|
|
|
|
end
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
touch references by params
|
|
|
|
|
|
|
|
Model.touch_reference_by_params(
|
|
|
|
object: 'Ticket',
|
|
|
|
o_id: 123,
|
|
|
|
)
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.touch_reference_by_params(data)
|
2016-06-07 20:06:08 +00:00
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
object_class = Kernel.const_get(data[:object])
|
|
|
|
object = object_class.lookup(id: data[:o_id])
|
|
|
|
return if !object
|
|
|
|
object.touch
|
2016-06-07 20:06:08 +00:00
|
|
|
rescue => e
|
|
|
|
logger.error e.message
|
|
|
|
logger.error e.backtrace.inspect
|
|
|
|
|
2016-06-07 19:22:08 +00:00
|
|
|
end
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
private
|
|
|
|
|
2014-02-05 12:22:14 +00:00
|
|
|
def attachments_buffer
|
|
|
|
@attachments_buffer_data
|
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2014-02-05 12:22:14 +00:00
|
|
|
def attachments_buffer=(attachments)
|
|
|
|
@attachments_buffer_data = attachments
|
|
|
|
end
|
|
|
|
|
|
|
|
def attachments_buffer_check
|
|
|
|
|
|
|
|
# do nothing if no attachment exists
|
2015-05-07 10:11:45 +00:00
|
|
|
return 1 if attachments_buffer.nil?
|
2014-02-05 12:22:14 +00:00
|
|
|
|
|
|
|
# store attachments
|
|
|
|
article_store = []
|
|
|
|
attachments_buffer.each do |attachment|
|
|
|
|
article_store.push Store.add(
|
2015-04-27 13:42:53 +00:00
|
|
|
object: self.class.to_s,
|
2015-05-07 12:10:38 +00:00
|
|
|
o_id: id,
|
2015-04-27 13:42:53 +00:00
|
|
|
data: attachment.content,
|
|
|
|
filename: attachment.filename,
|
|
|
|
preferences: attachment.preferences,
|
2015-05-07 12:10:38 +00:00
|
|
|
created_by_id: created_by_id,
|
2014-02-05 12:22:14 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
attachments_buffer = nil
|
|
|
|
end
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
=begin
|
|
|
|
|
2014-08-23 22:29:04 +00:00
|
|
|
delete object recent viewed list, will be executed automatically
|
|
|
|
|
|
|
|
model = Model.find(123)
|
|
|
|
model.recent_view_destroy
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def recent_view_destroy
|
2015-12-13 12:08:58 +00:00
|
|
|
RecentView.log_destroy(self.class.to_s, id)
|
2014-08-23 22:29:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2014-01-27 22:59:41 +00:00
|
|
|
check string/varchar size and cut them if needed
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def check_limits
|
2016-06-30 20:04:48 +00:00
|
|
|
attributes.each { |attribute|
|
2014-01-27 22:59:41 +00:00
|
|
|
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] ]
|
2015-07-11 22:37:31 +00:00
|
|
|
next if !column
|
2014-01-27 22:59:41 +00:00
|
|
|
limit = column.limit
|
|
|
|
if column && limit
|
|
|
|
current_length = attribute[1].to_s.length
|
|
|
|
if limit < current_length
|
2015-05-06 09:30:39 +00:00
|
|
|
logger.warn "WARNING: cut string because of database length #{self.class}.#{attribute[0]}(#{limit} but is #{current_length}:#{attribute[1]})"
|
2014-06-01 08:29:58 +00:00
|
|
|
self[ attribute[0] ] = attribute[1][ 0, limit ]
|
2014-01-27 22:59:41 +00:00
|
|
|
end
|
|
|
|
end
|
2014-06-01 08:29:58 +00:00
|
|
|
|
|
|
|
# strip 4 bytes utf8 chars if needed
|
|
|
|
if column && self[ attribute[0] ]
|
|
|
|
self[attribute[0]] = self[ attribute[0] ].utf8_to_3bytesutf8
|
|
|
|
end
|
2014-01-27 22:59:41 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|