2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
class Models
|
|
|
|
include ApplicationLib
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
get list of models
|
|
|
|
|
|
|
|
result = Models.all
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
2015-08-16 09:14:24 +00:00
|
|
|
Some::Classname1 => {
|
2015-09-27 21:01:45 +00:00
|
|
|
attributes: ['id', 'name', '...'],
|
|
|
|
reflections: ...model.reflections...,
|
|
|
|
table: 'some_classname1s',
|
2015-06-30 15:10:22 +00:00
|
|
|
},
|
2015-08-16 09:14:24 +00:00
|
|
|
Some::Classname2 => {
|
2015-06-30 15:10:22 +00:00
|
|
|
attributes: ['id', 'name', '...']
|
|
|
|
reflections: ...model.reflections...
|
2015-09-27 21:01:45 +00:00
|
|
|
table: 'some_classname2s',
|
2015-06-30 15:10:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.all
|
2018-01-24 13:30:04 +00:00
|
|
|
@all ||= begin
|
|
|
|
all = {}
|
2020-02-18 19:51:31 +00:00
|
|
|
dir = Rails.root.join('app/models').to_s
|
2018-01-24 13:30:04 +00:00
|
|
|
tables = ActiveRecord::Base.connection.tables
|
|
|
|
Dir.glob("#{dir}/**/*.rb") do |entry|
|
2021-05-12 11:37:44 +00:00
|
|
|
next if entry.match?(%r{application_model}i)
|
2018-01-24 13:30:04 +00:00
|
|
|
next if entry.match?(%r{channel/}i)
|
|
|
|
next if entry.match?(%r{observer/}i)
|
|
|
|
next if entry.match?(%r{store/provider/}i)
|
|
|
|
next if entry.match?(%r{models/concerns/}i)
|
2019-03-14 13:22:03 +00:00
|
|
|
next if entry.match?(%r{models/object_manager/attribute/validation/}i)
|
2018-01-24 13:30:04 +00:00
|
|
|
|
|
|
|
entry.gsub!(dir, '')
|
|
|
|
entry = entry.to_classname
|
2019-01-06 18:41:29 +00:00
|
|
|
model_class = entry.constantize
|
2018-01-24 13:30:04 +00:00
|
|
|
next if !model_class.respond_to? :new
|
|
|
|
next if !model_class.respond_to? :table_name
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-24 13:30:04 +00:00
|
|
|
table_name = model_class.table_name # handle models where not table exists, pending migrations
|
2020-09-30 09:07:01 +00:00
|
|
|
next if tables.exclude?(table_name)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-24 13:30:04 +00:00
|
|
|
model_object = model_class.new
|
|
|
|
next if !model_object.respond_to? :attributes
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2018-01-24 13:30:04 +00:00
|
|
|
all[model_class] = {}
|
|
|
|
all[model_class][:attributes] = model_class.attribute_names
|
|
|
|
all[model_class][:reflections] = model_class.reflections
|
|
|
|
all[model_class][:table] = model_class.table_name
|
2021-07-16 13:44:10 +00:00
|
|
|
# puts model_class
|
|
|
|
# puts "rrrr #{all[model_class][:attributes]}"
|
|
|
|
# puts " #{model_class.attribute_names.inspect}"
|
2018-01-24 13:30:04 +00:00
|
|
|
end
|
|
|
|
all
|
2015-06-30 15:10:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2018-10-03 06:23:22 +00:00
|
|
|
get list of searchable models for UI
|
2015-08-16 09:14:24 +00:00
|
|
|
|
|
|
|
result = Models.searchable
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
[Model1, Model2, Model3]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.searchable
|
2018-10-04 07:29:06 +00:00
|
|
|
@searchable ||= Models.all.keys.select { |model| model.respond_to?(:search_preferences) }
|
2015-08-16 09:14:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2018-10-03 06:23:22 +00:00
|
|
|
get list of indexable models
|
|
|
|
|
|
|
|
result = Models.indexable
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
[Model1, Model2, Model3]
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.indexable
|
2018-10-04 07:29:06 +00:00
|
|
|
@indexable ||= Models.all.keys.select { |model| model.method_defined?(:search_index_update_backend) }
|
2018-10-03 06:23:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
get reference list of a models
|
|
|
|
|
|
|
|
result = Models.references('User', 2)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
{
|
|
|
|
'Some::Classname1' => {
|
2015-07-06 00:33:43 +00:00
|
|
|
attribute1: 12,
|
|
|
|
attribute2: 6,
|
2015-06-30 15:10:22 +00:00
|
|
|
},
|
|
|
|
'Some::Classname2' => {
|
2015-07-06 00:33:43 +00:00
|
|
|
updated_by_id: 12,
|
|
|
|
created_by_id: 6,
|
2015-06-30 15:10:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2020-09-08 15:06:23 +00:00
|
|
|
def self.references(object_name, object_id, include_zero = false)
|
2016-06-06 06:34:15 +00:00
|
|
|
object_name = object_name.to_s
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
# check if model exists
|
2019-01-06 18:41:29 +00:00
|
|
|
object_name.constantize.find(object_id)
|
2015-07-06 00:33:43 +00:00
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
list = all
|
2015-07-06 00:33:43 +00:00
|
|
|
references = {}
|
2015-06-30 15:10:22 +00:00
|
|
|
|
|
|
|
# find relations via attributes
|
2015-07-06 00:33:43 +00:00
|
|
|
ref_attributes = ["#{object_name.downcase}_id"]
|
|
|
|
|
|
|
|
# for users we do not define relations for created_by_id &
|
|
|
|
# updated_by_id - add it here directly
|
|
|
|
if object_name == 'User'
|
|
|
|
ref_attributes.push 'created_by_id'
|
|
|
|
ref_attributes.push 'updated_by_id'
|
2021-06-02 10:25:12 +00:00
|
|
|
ref_attributes.push 'out_of_office_replacement_id'
|
2015-07-06 00:33:43 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
list.each do |model_class, model_attributes|
|
2015-07-06 00:33:43 +00:00
|
|
|
if !references[model_class.to_s]
|
|
|
|
references[model_class.to_s] = {}
|
|
|
|
end
|
2015-07-03 16:51:22 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
next if !model_attributes[:attributes]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
ref_attributes.each do |item|
|
2020-09-30 09:07:01 +00:00
|
|
|
next if model_attributes[:attributes].exclude?(item)
|
2015-07-03 16:51:22 +00:00
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
count = model_class.where(item => object_id).count
|
2020-09-08 15:06:23 +00:00
|
|
|
next if count.zero? && !include_zero
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
if !references[model_class.to_s][item]
|
|
|
|
references[model_class.to_s][item] = 0
|
|
|
|
end
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "FOUND (by id) #{model_class}->#{item} #{count}!" }
|
2015-07-06 00:33:43 +00:00
|
|
|
references[model_class.to_s][item] += count
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-30 15:10:22 +00:00
|
|
|
|
|
|
|
# find relations via reflections
|
2020-09-30 09:07:01 +00:00
|
|
|
list.each do |model_class, model_attributes| # rubocop:disable Style/CombinableLoops
|
2015-06-30 15:10:22 +00:00
|
|
|
next if !model_attributes[:reflections]
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
model_attributes[:reflections].each_value do |reflection_value|
|
2015-07-03 16:51:22 +00:00
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
next if reflection_value.macro != :belongs_to
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
col_name = "#{reflection_value.name}_id"
|
|
|
|
next if ref_attributes.include?(col_name)
|
2015-07-03 16:51:22 +00:00
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
if reflection_value.options[:class_name] == object_name
|
2021-07-20 13:31:46 +00:00
|
|
|
count = model_class.where(col_name => object_id).count
|
2020-09-08 15:06:23 +00:00
|
|
|
next if count.zero? && !include_zero
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
if !references[model_class.to_s][col_name]
|
|
|
|
references[model_class.to_s][col_name] = 0
|
|
|
|
end
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "FOUND (by ref without class) #{model_class}->#{col_name} #{count}!" }
|
2015-07-06 00:33:43 +00:00
|
|
|
references[model_class.to_s][col_name] += count
|
2015-06-30 15:10:22 +00:00
|
|
|
end
|
2015-07-03 16:51:22 +00:00
|
|
|
|
|
|
|
next if reflection_value.options[:class_name]
|
|
|
|
next if reflection_value.name != object_name.downcase.to_sym
|
|
|
|
|
2021-07-20 13:31:46 +00:00
|
|
|
count = model_class.where(col_name => object_id).count
|
2020-09-08 15:06:23 +00:00
|
|
|
next if count.zero? && !include_zero
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
if !references[model_class.to_s][col_name]
|
|
|
|
references[model_class.to_s][col_name] = 0
|
|
|
|
end
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "FOUND (by ref with class) #{model_class}->#{col_name} #{count}!" }
|
2015-07-06 00:33:43 +00:00
|
|
|
references[model_class.to_s][col_name] += count
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-30 15:10:22 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
# cleanup, remove models with empty references
|
2017-10-01 12:25:52 +00:00
|
|
|
references.each do |k, v|
|
2017-11-23 08:09:44 +00:00
|
|
|
next if v.present?
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
references.delete(k)
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-07-06 00:33:43 +00:00
|
|
|
|
2015-06-30 15:10:22 +00:00
|
|
|
references
|
|
|
|
end
|
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
get reference total of a models
|
|
|
|
|
|
|
|
count = Models.references_total('User', 2)
|
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
count # 1234
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.references_total(object_name, object_id)
|
2015-07-06 01:12:36 +00:00
|
|
|
references = references(object_name, object_id)
|
2015-07-06 00:33:43 +00:00
|
|
|
total = 0
|
2017-11-23 08:09:44 +00:00
|
|
|
references.each_value do |model_references|
|
|
|
|
model_references.each_value do |count|
|
2015-07-06 00:33:43 +00:00
|
|
|
total += count
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-07-06 00:33:43 +00:00
|
|
|
total
|
|
|
|
end
|
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
merge model references to other model
|
|
|
|
|
2015-07-06 06:22:17 +00:00
|
|
|
result = Models.merge('User', 2, 4711) # Object, object_id_of_primary, object_id_which_should_be_merged
|
2015-07-06 00:33:43 +00:00
|
|
|
|
|
|
|
returns
|
|
|
|
|
|
|
|
true # false
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def self.merge(object_name, object_id_primary, object_id_to_merge, force = false)
|
|
|
|
|
|
|
|
# if lower x references to update, do it right now
|
|
|
|
if force
|
2015-07-06 01:12:36 +00:00
|
|
|
total = references_total(object_name, object_id_to_merge)
|
2015-07-06 00:33:43 +00:00
|
|
|
if total > 1000
|
2016-03-01 14:26:46 +00:00
|
|
|
raise "Can't merge object because object has more then 1000 (#{total}) references, please contact your system administrator."
|
2015-07-06 00:33:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-06 01:12:36 +00:00
|
|
|
# update references
|
|
|
|
references = references(object_name, object_id_to_merge)
|
2017-10-01 12:25:52 +00:00
|
|
|
references.each do |model, attributes|
|
2019-01-06 18:41:29 +00:00
|
|
|
model_object = model.constantize
|
2015-07-06 01:12:36 +00:00
|
|
|
|
|
|
|
# collect items and attributes to update
|
2015-07-06 00:33:43 +00:00
|
|
|
items_to_update = {}
|
2017-11-23 08:09:44 +00:00
|
|
|
attributes.each_key do |attribute|
|
2018-03-20 17:47:49 +00:00
|
|
|
Rails.logger.debug { "#{object_name}: #{model}.#{attribute}->#{object_id_to_merge}->#{object_id_primary}" }
|
2021-07-20 13:31:46 +00:00
|
|
|
model_object.where(attribute => object_id_to_merge).each do |item|
|
2015-07-06 00:33:43 +00:00
|
|
|
if !items_to_update[item.id]
|
|
|
|
items_to_update[item.id] = item
|
|
|
|
end
|
|
|
|
items_to_update[item.id][attribute.to_sym] = object_id_primary
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2015-07-06 01:12:36 +00:00
|
|
|
|
|
|
|
# update items
|
2015-07-06 06:22:17 +00:00
|
|
|
ActiveRecord::Base.transaction do
|
2017-11-23 08:09:44 +00:00
|
|
|
items_to_update.each_value(&:save!)
|
2015-07-06 06:22:17 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2020-07-20 06:19:51 +00:00
|
|
|
|
|
|
|
ExternalSync.migrate(object_name, object_id_primary, object_id_to_merge)
|
|
|
|
|
2015-07-06 00:33:43 +00:00
|
|
|
true
|
|
|
|
end
|
2015-07-03 15:38:50 +00:00
|
|
|
end
|