Skip to content
Permalink
Browse files

Refactoring: Move Ruby class definitions to dedicated files to improv…

…e orientation and expressiveness.
  • Loading branch information...
thorsteneckel committed Sep 27, 2019
1 parent f0fdf7e commit d354dee74c03795059ab398d38666071fd99ef47
@@ -225,8 +225,4 @@ def self.attribute_lookup(name)
# create
History::Attribute.create!(name: name)
end

class Object < ApplicationModel; end
class Type < ApplicationModel; end
class Attribute < ApplicationModel; end
end
@@ -0,0 +1,4 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class History::Attribute < ApplicationModel
end
@@ -0,0 +1,4 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class History::Object < ApplicationModel
end
@@ -0,0 +1,4 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class History::Type < ApplicationModel
end
@@ -245,11 +245,3 @@ def self.lookup_linked_object(elem)
end

end

class Link::Type < ApplicationModel
validates :name, presence: true
end

class Link::Object < ApplicationModel
validates :name, presence: true
end
@@ -0,0 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class Link::Object < ApplicationModel
validates :name, presence: true
end
@@ -0,0 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/

class Link::Type < ApplicationModel
validates :name, presence: true
end
@@ -485,83 +485,4 @@ def self._delete_file(file, _permission, _data)

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
@@ -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

0 comments on commit d354dee

Please sign in to comment.
You can’t perform that action at this time.