Skip to content

Commit

Permalink
Maintenance: remove unused notifications table
Browse files Browse the repository at this point in the history
  • Loading branch information
mantas committed Jun 23, 2022
1 parent c8454fb commit bc1b789
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 68 deletions.
112 changes: 61 additions & 51 deletions app/models/application_model/can_assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,62 +58,69 @@ def assets(data = {})
=end

def assets_of_selector(selector, assets = {})
send(selector)
.each_with_object(assets) do |(item, content), memo|
assets_of_single_selector(item, content, memo)
end
end

# get assets of condition
models = Models.all
send(selector).each do |item, content|
attribute = item.split('.')
next if !attribute[1]
def assets_added_to?(data)
data.dig(self.class.to_app_model, id).present?
end

if attribute[0] == 'customer' || attribute[0] == 'session'
attribute[0] = 'user'
end
private

begin
attribute_class = attribute[0].to_classname.constantize
rescue => e
next if attribute[0] == 'article'
next if attribute[0] == 'execution_time'
def assets_of_single_selector(item, content, assets = {})
area, key = item.split('.')
return if !key

logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
next
end
area = 'user' if %w[customer session].include? area

if attribute_class == ::Notification
next if content['recipient'].blank?
attribute_ref_class, item_ids = if area == 'notification'
notifications_assets_data(content)
else
non_notifications_assets_data(area, key, content)
end

attribute_ref_class = ::User
item_ids = []
Array(content['recipient']).each do |identifier|
next if identifier !~ %r{\Auserid_(\d+)\z}
return if !attribute_ref_class

item_ids.push($1)
end
else
reflection = attribute[1].sub(%r{_id$}, '')
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
item_ids = Array(content['value'])
end
items = item_ids
.compact_blank
.filter_map { |elem| attribute_ref_class.lookup(id: elem) }

item_ids.each do |item_id|
next if item_id.blank?
ApplicationModel::CanAssets.reduce items, assets
end

def notifications_assets_data(content)
return if content['recipient'].blank?

attribute_object = attribute_ref_class.lookup(id: item_id)
next if !attribute_object
item_ids = Array(content['recipient'])
.filter_map do |elem|
match = elem.match %r{\Auserid_(?<id>\d+)\z}

assets = attribute_object.assets(assets)
match[:id] if match
end
end
assets

[::User, item_ids]
end

def assets_added_to?(data)
data.dig(self.class.to_app_model, id).present?
def non_notifications_assets_data(area, key, content)
return if %w[article execution_time].include? area

begin
attribute_class = area.to_classname.constantize
rescue => e
logger.error "Unable to get asset for '#{area}': #{e.inspect}"
return
end

reflection = key.delete_suffix '_id'

klass = Models.all.dig(attribute_class, :reflections, reflection)&.klass

return if !klass

[klass, Array(content['value'])]
end

# methods defined here are going to extend the class, not the instance of it
Expand Down Expand Up @@ -178,6 +185,8 @@ def assets_of_object_list(list, assets = {})
end
end

class << self

=begin
Compiles an assets hash for given items
Expand All @@ -193,15 +202,16 @@ def assets_of_object_list(list, assets = {})
=end

def self.reduce(items, data = {}, suffix = nil)
items.reduce(data) do |memo, elem|
method_name = if suffix.present? && elem.respond_to?("assets_#{suffix}")
"assets_#{suffix}"
else
:assets
end
def reduce(items, data = {}, suffix = nil)
items.reduce(data) do |memo, elem|
method_name = if suffix.present? && elem.respond_to?("assets_#{suffix}")
"assets_#{suffix}"
else
:assets
end

elem.send method_name, memo
elem.send method_name, memo
end
end
end
end
7 changes: 0 additions & 7 deletions app/models/notification.rb

This file was deleted.

10 changes: 0 additions & 10 deletions db/migrate/20120101000010_create_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,6 @@ def up
add_foreign_key :jobs, :users, column: :created_by_id
add_foreign_key :jobs, :users, column: :updated_by_id

create_table :notifications do |t|
t.column :subject, :string, limit: 250, null: false
t.column :body, :string, limit: 8000, null: false
t.column :content_type, :string, limit: 250, null: false
t.column :active, :boolean, null: false, default: true
t.column :note, :string, limit: 250, null: true
t.timestamps limit: 3, null: false
end

create_table :link_types do |t|
t.column :name, :string, limit: 250, null: false
t.column :note, :string, limit: 250, null: true
Expand Down Expand Up @@ -625,7 +616,6 @@ def self.down
drop_table :text_modules_groups
drop_table :text_modules
drop_table :postmaster_filters
drop_table :notifications
drop_table :triggers
drop_table :links
drop_table :link_types
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20220607113229_drop_notifications_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/

class DropNotificationsTable < ActiveRecord::Migration[6.1]
def up
# return if it's a new setup
return if !Setting.exists?(name: 'system_init_done')

drop_table :notifications
end
end

0 comments on commit bc1b789

Please sign in to comment.