diff --git a/app/models/history.rb b/app/models/history.rb index 8ee791922..662a555e6 100644 --- a/app/models/history.rb +++ b/app/models/history.rb @@ -225,8 +225,4 @@ returns # create History::Attribute.create!(name: name) end - - class Object < ApplicationModel; end - class Type < ApplicationModel; end - class Attribute < ApplicationModel; end end diff --git a/app/models/history/attribute.rb b/app/models/history/attribute.rb new file mode 100644 index 000000000..af4f19daa --- /dev/null +++ b/app/models/history/attribute.rb @@ -0,0 +1,4 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class History::Attribute < ApplicationModel +end diff --git a/app/models/history/object.rb b/app/models/history/object.rb new file mode 100644 index 000000000..663f63f16 --- /dev/null +++ b/app/models/history/object.rb @@ -0,0 +1,4 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class History::Object < ApplicationModel +end diff --git a/app/models/history/type.rb b/app/models/history/type.rb new file mode 100644 index 000000000..3e0cbf060 --- /dev/null +++ b/app/models/history/type.rb @@ -0,0 +1,4 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class History::Type < ApplicationModel +end diff --git a/app/models/link.rb b/app/models/link.rb index e7ddf3350..29577623e 100644 --- a/app/models/link.rb +++ b/app/models/link.rb @@ -245,11 +245,3 @@ class Link < ApplicationModel end end - -class Link::Type < ApplicationModel - validates :name, presence: true -end - -class Link::Object < ApplicationModel - validates :name, presence: true -end diff --git a/app/models/link/object.rb b/app/models/link/object.rb new file mode 100644 index 000000000..8e70137e1 --- /dev/null +++ b/app/models/link/object.rb @@ -0,0 +1,5 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Link::Object < ApplicationModel + validates :name, presence: true +end diff --git a/app/models/link/type.rb b/app/models/link/type.rb new file mode 100644 index 000000000..808fa877f --- /dev/null +++ b/app/models/link/type.rb @@ -0,0 +1,5 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Link::Type < ApplicationModel + validates :name, presence: true +end diff --git a/app/models/package.rb b/app/models/package.rb index 2d146a7be..48c3066c8 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -485,83 +485,4 @@ execute all pending package migrations at once true end - - class Migration < ApplicationModel - - def self.linked - szpm_files = [] - Dir.chdir(root) do - szpm_files = Dir['*.szpm'] - end - - szpm_files.each do |szpm_file| - package = szpm_file.sub('.szpm', '') - migrate(package) - end - end - - def self.migrate(package, direction = 'normal') - location = "#{root}/db/addon/#{package.underscore}" - - return true if !File.exist?(location) - - # get existing migrations - migrations_existing = [] - Dir.foreach(location) do |entry| - next if entry == '.' - next if entry == '..' - - migrations_existing.push entry - end - - # up - migrations_existing = migrations_existing.sort - - # down - if direction == 'reverse' - migrations_existing = migrations_existing.reverse - end - - migrations_existing.each do |migration| - next if !migration.match?(/\.rb$/) - - version = nil - name = nil - if migration =~ /^(.+?)_(.*)\.rb$/ - version = $1 - name = $2 - end - if !version || !name - raise "Invalid package migration '#{migration}'" - end - - # down - done = Package::Migration.find_by(name: package.underscore, version: version) - if direction == 'reverse' - next if !done - - logger.info "NOTICE: down package migration '#{migration}'" - load "#{location}/#{migration}" - classname = name.camelcase - classname.constantize.down - record = Package::Migration.find_by(name: package.underscore, version: version) - record&.destroy - - # up - else - next if done - - logger.info "NOTICE: up package migration '#{migration}'" - load "#{location}/#{migration}" - classname = name.camelcase - classname.constantize.up - Package::Migration.create(name: package.underscore, version: version) - end - end - end - - def self.root - Rails.root - end - end end diff --git a/app/models/package/migration.rb b/app/models/package/migration.rb new file mode 100644 index 000000000..e683a3057 --- /dev/null +++ b/app/models/package/migration.rb @@ -0,0 +1,80 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Package::Migration < ApplicationModel + + def self.linked + szpm_files = [] + Dir.chdir(root) do + szpm_files = Dir['*.szpm'] + end + + szpm_files.each do |szpm_file| + package = szpm_file.sub('.szpm', '') + migrate(package) + end + end + + def self.migrate(package, direction = 'normal') + location = "#{root}/db/addon/#{package.underscore}" + + return true if !File.exist?(location) + + # get existing migrations + migrations_existing = [] + Dir.foreach(location) do |entry| + next if entry == '.' + next if entry == '..' + + migrations_existing.push entry + end + + # up + migrations_existing = migrations_existing.sort + + # down + if direction == 'reverse' + migrations_existing = migrations_existing.reverse + end + + migrations_existing.each do |migration| + next if !migration.match?(/\.rb$/) + + version = nil + name = nil + if migration =~ /^(.+?)_(.*)\.rb$/ + version = $1 + name = $2 + end + if !version || !name + raise "Invalid package migration '#{migration}'" + end + + # down + done = Package::Migration.find_by(name: package.underscore, version: version) + if direction == 'reverse' + next if !done + + logger.info "NOTICE: down package migration '#{migration}'" + load "#{location}/#{migration}" + classname = name.camelcase + classname.constantize.down + record = Package::Migration.find_by(name: package.underscore, version: version) + record&.destroy + + # up + else + next if done + + logger.info "NOTICE: up package migration '#{migration}'" + load "#{location}/#{migration}" + classname = name.camelcase + classname.constantize.up + Package::Migration.create(name: package.underscore, version: version) + end + end + end + + def self.root + Rails.root + end +end diff --git a/app/models/tag.rb b/app/models/tag.rb index e5911279a..43f77950a 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -150,198 +150,4 @@ returns .order(:id) .pluck('tag_items.name') end - - class Object < ApplicationModel - validates :name, presence: true - -=begin - -lookup by name and create tag item - - tag_object = Tag::Object.lookup_by_name_and_create('some tag') - -=end - - def self.lookup_by_name_and_create(name) - name.strip! - - tag_object = Tag::Object.lookup(name: name) - return tag_object if tag_object - - Tag::Object.create(name: name) - end - - end - - class Item < ApplicationModel - validates :name, presence: true - before_save :fill_namedowncase - -=begin - -lookup by name and create tag item - - tag_item = Tag::Item.lookup_by_name_and_create('some tag') - -=end - - def self.lookup_by_name_and_create(name) - name.strip! - - tag_item = Tag::Item.lookup(name: name) - return tag_item if tag_item - - Tag::Item.create(name: name) - end - -=begin - -rename tag items - - Tag::Item.rename( - id: existing_tag_item_to_rename, - name: 'new tag item name', - updated_by_id: current_user.id, - ) - -=end - - def self.rename(data) - - new_tag_name = data[:name].strip - old_tag_item = Tag::Item.find(data[:id]) - already_existing_tag = Tag::Item.lookup(name: new_tag_name) - - # check if no rename is needed - return true if new_tag_name == old_tag_item.name - - # merge old with new tag if already existing - if already_existing_tag - - # re-assign old tag to already existing tag - Tag.where(tag_item_id: old_tag_item.id).each do |tag| - - # check if tag already exists on object - if Tag.find_by(tag_object_id: tag.tag_object_id, o_id: tag.o_id, tag_item_id: already_existing_tag.id) - Tag.tag_remove( - tag_object_id: tag.tag_object_id, - o_id: tag.o_id, - tag_item_id: old_tag_item.id, - ) - next - end - - # re-assign - tag_object = Tag::Object.lookup(id: tag.tag_object_id) - tag.tag_item_id = already_existing_tag.id - tag.save - - # touch reference objects - Tag.touch_reference_by_params( - object: tag_object.name, - o_id: tag.o_id, - ) - end - - # delete not longer used tag - old_tag_item.destroy - return true - end - - update_referenced_objects(old_tag_item.name, new_tag_name) - - # update new tag name - old_tag_item.name = new_tag_name - old_tag_item.save - - # touch reference objects - Tag.where(tag_item_id: old_tag_item.id).each do |tag| - tag_object = Tag::Object.lookup(id: tag.tag_object_id) - Tag.touch_reference_by_params( - object: tag_object.name, - o_id: tag.o_id, - ) - end - - true - end - -=begin - -remove tag item (destroy with reverences) - - Tag::Item.remove(id) - -=end - - def self.remove(id) - - # search for references, destroy and touch - Tag.where(tag_item_id: id).each do |tag| - tag_object = Tag::Object.lookup(id: tag.tag_object_id) - tag.destroy - Tag.touch_reference_by_params( - object: tag_object.name, - o_id: tag.o_id, - ) - end - Tag::Item.find(id).destroy - true - end - - def fill_namedowncase - self.name_downcase = name.downcase - true - end - -=begin - - Update referenced objects such as triggers, overviews, schedulers, and postmaster filters - - Specifically, the following fields are updated: - - Overview.condition - Trigger.condition Trigger.perform - Job.condition Job.perform - PostmasterFilter.perform - -=end - - def self.update_referenced_objects(old_name, new_name) - objects = Overview.all + Trigger.all + Job.all + PostmasterFilter.all - - objects.each do |object| - changed = false - if object.has_attribute?(:condition) - changed |= update_condition_hash object.condition, old_name, new_name - end - if object.has_attribute?(:perform) - changed |= update_condition_hash object.perform, old_name, new_name - end - object.save if changed - end - end - - def self.update_condition_hash(hash, old_name, new_name) - changed = false - hash.each do |key, condition| - next if %w[ticket.tags x-zammad-ticket-tags].exclude? key - next if condition[:value].split(', ').exclude? old_name - - condition[:value] = update_name(condition[:value], old_name, new_name) - changed = true - end - changed - end - - def self.update_name(condition, old_name, new_name) - tags = condition.split(', ') - return new_name if tags.size == 1 - - tags = tags.map { |t| t == old_name ? new_name : t } - tags.join(', ') - end - - end - end diff --git a/app/models/tag/item.rb b/app/models/tag/item.rb new file mode 100644 index 000000000..94b1c25d8 --- /dev/null +++ b/app/models/tag/item.rb @@ -0,0 +1,171 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Tag::Item < ApplicationModel + validates :name, presence: true + before_save :fill_namedowncase + +=begin + +lookup by name and create tag item + +tag_item = Tag::Item.lookup_by_name_and_create('some tag') + +=end + + def self.lookup_by_name_and_create(name) + name.strip! + + tag_item = Tag::Item.lookup(name: name) + return tag_item if tag_item + + Tag::Item.create(name: name) + end + +=begin + +rename tag items + +Tag::Item.rename( + id: existing_tag_item_to_rename, + name: 'new tag item name', + updated_by_id: current_user.id, +) + +=end + + def self.rename(data) + + new_tag_name = data[:name].strip + old_tag_item = Tag::Item.find(data[:id]) + already_existing_tag = Tag::Item.lookup(name: new_tag_name) + + # check if no rename is needed + return true if new_tag_name == old_tag_item.name + + # merge old with new tag if already existing + if already_existing_tag + + # re-assign old tag to already existing tag + Tag.where(tag_item_id: old_tag_item.id).each do |tag| + + # check if tag already exists on object + if Tag.find_by(tag_object_id: tag.tag_object_id, o_id: tag.o_id, tag_item_id: already_existing_tag.id) + Tag.tag_remove( + tag_object_id: tag.tag_object_id, + o_id: tag.o_id, + tag_item_id: old_tag_item.id, + ) + next + end + + # re-assign + tag_object = Tag::Object.lookup(id: tag.tag_object_id) + tag.tag_item_id = already_existing_tag.id + tag.save + + # touch reference objects + Tag.touch_reference_by_params( + object: tag_object.name, + o_id: tag.o_id, + ) + end + + # delete not longer used tag + old_tag_item.destroy + return true + end + + update_referenced_objects(old_tag_item.name, new_tag_name) + + # update new tag name + old_tag_item.name = new_tag_name + old_tag_item.save + + # touch reference objects + Tag.where(tag_item_id: old_tag_item.id).each do |tag| + tag_object = Tag::Object.lookup(id: tag.tag_object_id) + Tag.touch_reference_by_params( + object: tag_object.name, + o_id: tag.o_id, + ) + end + + true + end + +=begin + +remove tag item (destroy with reverences) + +Tag::Item.remove(id) + +=end + + def self.remove(id) + + # search for references, destroy and touch + Tag.where(tag_item_id: id).each do |tag| + tag_object = Tag::Object.lookup(id: tag.tag_object_id) + tag.destroy + Tag.touch_reference_by_params( + object: tag_object.name, + o_id: tag.o_id, + ) + end + Tag::Item.find(id).destroy + true + end + + def fill_namedowncase + self.name_downcase = name.downcase + true + end + +=begin + +Update referenced objects such as triggers, overviews, schedulers, and postmaster filters + +Specifically, the following fields are updated: + +Overview.condition +Trigger.condition Trigger.perform +Job.condition Job.perform + PostmasterFilter.perform + +=end + + def self.update_referenced_objects(old_name, new_name) + objects = Overview.all + Trigger.all + Job.all + PostmasterFilter.all + + objects.each do |object| + changed = false + if object.has_attribute?(:condition) + changed |= update_condition_hash object.condition, old_name, new_name + end + if object.has_attribute?(:perform) + changed |= update_condition_hash object.perform, old_name, new_name + end + object.save if changed + end + end + + def self.update_condition_hash(hash, old_name, new_name) + changed = false + hash.each do |key, condition| + next if %w[ticket.tags x-zammad-ticket-tags].exclude? key + next if condition[:value].split(', ').exclude? old_name + + condition[:value] = update_name(condition[:value], old_name, new_name) + changed = true + end + changed + end + + def self.update_name(condition, old_name, new_name) + tags = condition.split(', ') + return new_name if tags.size == 1 + + tags = tags.map { |t| t == old_name ? new_name : t } + tags.join(', ') + end +end diff --git a/app/models/tag/object.rb b/app/models/tag/object.rb new file mode 100644 index 000000000..7fa372ea0 --- /dev/null +++ b/app/models/tag/object.rb @@ -0,0 +1,22 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Tag::Object < ApplicationModel + validates :name, presence: true + +=begin + +lookup by name and create tag item + + tag_object = Tag::Object.lookup_by_name_and_create('some tag') + +=end + + def self.lookup_by_name_and_create(name) + name.strip! + + tag_object = Tag::Object.lookup(name: name) + return tag_object if tag_object + + Tag::Object.create(name: name) + end +end diff --git a/app/models/ticket/article.rb b/app/models/ticket/article.rb index 1e2ed02f5..bbd5e39d7 100644 --- a/app/models/ticket/article.rb +++ b/app/models/ticket/article.rb @@ -412,17 +412,4 @@ returns o_id: id, ) end - - class Flag < ApplicationModel - end - - class Sender < ApplicationModel - include ChecksLatestChangeObserved - validates :name, presence: true - end - - class Type < ApplicationModel - include ChecksLatestChangeObserved - validates :name, presence: true - end end diff --git a/app/models/ticket/article/flag.rb b/app/models/ticket/article/flag.rb new file mode 100644 index 000000000..62af9f9f2 --- /dev/null +++ b/app/models/ticket/article/flag.rb @@ -0,0 +1,4 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Ticket::Article::Flag < ApplicationModel +end diff --git a/app/models/ticket/article/sender.rb b/app/models/ticket/article/sender.rb new file mode 100644 index 000000000..261ab8b82 --- /dev/null +++ b/app/models/ticket/article/sender.rb @@ -0,0 +1,6 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Ticket::Article::Sender < ApplicationModel + include ChecksLatestChangeObserved + validates :name, presence: true +end diff --git a/app/models/ticket/article/type.rb b/app/models/ticket/article/type.rb new file mode 100644 index 000000000..2c2f26c86 --- /dev/null +++ b/app/models/ticket/article/type.rb @@ -0,0 +1,6 @@ +# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/ + +class Ticket::Article::Type < ApplicationModel + include ChecksLatestChangeObserved + validates :name, presence: true +end