Reworked file structure of concerns.

- can (new methods)
    - checks (events, pre/post function checks)
    - has (model related functions and events)
This commit is contained in:
Rolf Schmidt 2017-05-02 17:21:13 +02:00
parent 998a74b7f0
commit fa454abf3a
44 changed files with 194 additions and 199 deletions

View file

@ -1,23 +1,22 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class ApplicationModel < ActiveRecord::Base
include ApplicationModel::ActivityStreamLoggable
include ApplicationModel::Cacheable
include ApplicationModel::CanActivityStreamLog
include ApplicationModel::HasCache
include ApplicationModel::CanLookup
include ApplicationModel::CanLookupSearchIndexAttributes
include ApplicationModel::ChecksAttributeLength
include ApplicationModel::CleansParam
include ApplicationModel::CleansRecentView
include ApplicationModel::FillsByUserColumns
include ApplicationModel::HandlesCreatesAndUpdates
include ApplicationModel::HasAssets
include ApplicationModel::HasAssociations
include ApplicationModel::CanCleanupParam
include ApplicationModel::HasRecentViews
include ApplicationModel::ChecksUserColumnsFillup
include ApplicationModel::CanCreatesAndUpdates
include ApplicationModel::CanAssets
include ApplicationModel::CanAssociations
include ApplicationModel::HasAttachments
include ApplicationModel::HasLatestChangeTimestamp
include ApplicationModel::CanLatestChange
include ApplicationModel::HasExternalSync
include ApplicationModel::Importable
include ApplicationModel::HistoryLoggable
include ApplicationModel::TouchesReferences
include ApplicationModel::ChecksImport
include ApplicationModel::CanTouchReferences
self.abstract_class = true
end

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::ActivityStreamLoggable
module ApplicationModel::CanActivityStreamLog
extend ActiveSupport::Concern
=begin

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HasAssets
module ApplicationModel::CanAssets
extend ActiveSupport::Concern
=begin

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HasAssociations
module ApplicationModel::CanAssociations
extend ActiveSupport::Concern
=begin

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::CleansParam
module ApplicationModel::CanCleanupParam
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HandlesCreatesAndUpdates
module ApplicationModel::CanCreatesAndUpdates
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HasLatestChangeTimestamp
module ApplicationModel::CanLatestChange
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::TouchesReferences
module ApplicationModel::CanTouchReferences
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::Importable
module ApplicationModel::ChecksImport
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::FillsByUserColumns
module ApplicationModel::ChecksUserColumnsFillup
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::Cacheable
module ApplicationModel::HasCache
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::CleansRecentView
module ApplicationModel::HasRecentViews
extend ActiveSupport::Concern
included do

View file

@ -1,112 +0,0 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ApplicationModel::HistoryLoggable
extend ActiveSupport::Concern
=begin
create history entry for this object
organization = Organization.find(123)
result = organization.history_log('created', user_id)
returns
result = true # false
=end
def history_log(type, user_id, attributes = {})
attributes.merge!(
o_id: self['id'],
history_type: type,
history_object: self.class.name,
related_o_id: nil,
related_history_object: nil,
created_by_id: user_id,
updated_at: updated_at,
created_at: updated_at,
).merge!(history_log_attributes)
History.add(attributes)
end
# callback function to overwrite
# default history log attributes
# gets called from history_log
def history_log_attributes
{}
end
=begin
get history log for this object
organization = Organization.find(123)
result = organization.history_get()
returns
result = [
{
:type => 'created',
:object => 'Organization',
:created_by_id => 3,
:created_at => "2013-08-19 20:41:33",
},
{
:type => 'updated',
:object => 'Organization',
:attribute => 'note',
:o_id => 1,
:id_to => nil,
:id_from => nil,
:value_from => "some note",
:value_to => "some other note",
:created_by_id => 3,
:created_at => "2013-08-19 20:41:33",
},
]
to get history log for this object with all assets
organization = Organization.find(123)
result = organization.history_get(true)
returns
result = {
:history => [
{ ... },
{ ... },
],
:assets => {
...
}
}
=end
def history_get(fulldata = false)
if !fulldata
return History.list(self.class.name, self['id'])
end
# get related objects
history = History.list(self.class.name, self['id'], nil, true)
history[:list].each { |item|
record = Kernel.const_get(item['object']).find(item['o_id'])
history[:assets] = record.assets(history[:assets])
if item['related_object']
record = Kernel.const_get(item['related_object']).find(item['related_o_id'])
history[:assets] = record.assets(history[:assets])
end
}
{
history: history[:list],
assets: history[:assets],
}
end
end

View file

@ -1,8 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Calendar < ApplicationModel
include NotifiesClients
include UniqNamed
include ChecksClientNotification
include CanUniqName
store :business_hours
store :public_holidays

View file

@ -1,5 +1,5 @@
class Chat::Message < ApplicationModel
include HtmlSanitized
include ChecksHtmlSanitized
sanitized_html :content
end

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module Seedable
module CanSeed
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module UniqNamed
module CanUniqName
extend ActiveSupport::Concern
# methods defined here are going to extend the class, not the instance of it

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module NotifiesClients
module ChecksClientNotification
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module ValidatesCondition
module ChecksConditionValidation
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module HtmlSanitized
module ChecksHtmlSanitized
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module LatestChangeObserved
module ChecksLatestChangeObserved
extend ActiveSupport::Concern
included do

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module LogsActivityStream
module HasActivityStreamLog
extend ActiveSupport::Concern
included do
@ -69,7 +69,7 @@ delete object activity stream, will be executed automatically
serve methode to ignore model attributes in activity stream and/or limit activity stream permission
class Model < ApplicationModel
include LogsActivityStream
include HasActivityStreamLog
activity_stream_permission 'admin.user'
activity_stream_attributes_ignored :create_article_type_id, :preferences
end

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module Historisable
module HasHistory
extend ActiveSupport::Concern
included do
@ -120,6 +120,114 @@ delete object history, will be executed automatically
History.remove(self.class.to_s, id)
end
=begin
create history entry for this object
organization = Organization.find(123)
result = organization.history_log('created', user_id)
returns
result = true # false
=end
def history_log(type, user_id, attributes = {})
attributes.merge!(
o_id: self['id'],
history_type: type,
history_object: self.class.name,
related_o_id: nil,
related_history_object: nil,
created_by_id: user_id,
updated_at: updated_at,
created_at: updated_at,
).merge!(history_log_attributes)
History.add(attributes)
end
# callback function to overwrite
# default history log attributes
# gets called from history_log
def history_log_attributes
{}
end
=begin
get history log for this object
organization = Organization.find(123)
result = organization.history_get()
returns
result = [
{
:type => 'created',
:object => 'Organization',
:created_by_id => 3,
:created_at => "2013-08-19 20:41:33",
},
{
:type => 'updated',
:object => 'Organization',
:attribute => 'note',
:o_id => 1,
:id_to => nil,
:id_from => nil,
:value_from => "some note",
:value_to => "some other note",
:created_by_id => 3,
:created_at => "2013-08-19 20:41:33",
},
]
to get history log for this object with all assets
organization = Organization.find(123)
result = organization.history_get(true)
returns
result = {
:history => [
{ ... },
{ ... },
],
:assets => {
...
}
}
=end
def history_get(fulldata = false)
if !fulldata
return History.list(self.class.name, self['id'])
end
# get related objects
history = History.list(self.class.name, self['id'], nil, true)
history[:list].each { |item|
record = Kernel.const_get(item['object']).find(item['o_id'])
history[:assets] = record.assets(history[:assets])
if item['related_object']
record = Kernel.const_get(item['related_object']).find(item['related_o_id'])
history[:assets] = record.assets(history[:assets])
end
}
{
history: history[:list],
assets: history[:assets],
}
end
# methods defined here are going to extend the class, not the instance of it
class_methods do
=begin
@ -127,7 +235,7 @@ delete object history, will be executed automatically
serve methode to ignore model attributes in historization
class Model < ApplicationModel
include Historisable
include HasHistory
history_attributes_ignored :create_article_type_id, :preferences
end

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module SearchIndexed
module HasSearchIndexBackend
extend ActiveSupport::Concern
included do
@ -102,7 +102,7 @@ returns
serve methode to ignore model attributes in search index
class Model < ApplicationModel
include SearchIndexed
include HasSearchIndexBackend
search_index_attributes_ignored :password, :image
end

View file

@ -1,5 +1,5 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
module Taggable
module HasTags
extend ActiveSupport::Concern
included do

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class EmailAddress < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
has_many :groups, after_add: :cache_update, after_remove: :cache_update
belongs_to :channel

View file

@ -1,10 +1,10 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Group < ApplicationModel
include LogsActivityStream
include NotifiesClients
include LatestChangeObserved
include Historisable
include HasActivityStreamLog
include ChecksClientNotification
include ChecksLatestChangeObserved
include HasHistory
has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update
belongs_to :email_address

View file

@ -1,8 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Job < ApplicationModel
include NotifiesClients
include ValidatesCondition
include ChecksClientNotification
include ChecksConditionValidation
load 'job/assets.rb'
include Job::Assets

View file

@ -1,8 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Macro < ApplicationModel
include NotifiesClients
include LatestChangeObserved
include ChecksClientNotification
include ChecksLatestChangeObserved
store :perform
validates :name, presence: true

View file

@ -1,5 +1,5 @@
class ObjectManager::Attribute < ApplicationModel
include NotifiesClients
include ChecksClientNotification
self.table_name = 'object_manager_attributes'

View file

@ -1,11 +1,11 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Organization < ApplicationModel
include LogsActivityStream
include NotifiesClients
include LatestChangeObserved
include Historisable
include SearchIndexed
include HasActivityStreamLog
include ChecksClientNotification
include ChecksLatestChangeObserved
include HasHistory
include HasSearchIndexBackend
load 'organization/permission.rb'
include Organization::Permission

View file

@ -1,10 +1,10 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Overview < ApplicationModel
include NotifiesClients
include LatestChangeObserved
include ValidatesCondition
include Seedable
include ChecksClientNotification
include ChecksLatestChangeObserved
include ChecksConditionValidation
include CanSeed
load 'overview/assets.rb'
include Overview::Assets

View file

@ -1,8 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Permission < ApplicationModel
include NotifiesClients
include LatestChangeObserved
include ChecksClientNotification
include ChecksLatestChangeObserved
has_and_belongs_to_many :roles
validates :name, presence: true

View file

@ -1,9 +1,9 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Role < ApplicationModel
include LogsActivityStream
include NotifiesClients
include LatestChangeObserved
include HasActivityStreamLog
include ChecksClientNotification
include ChecksLatestChangeObserved
has_and_belongs_to_many :users, after_add: :cache_update, after_remove: :cache_update
has_and_belongs_to_many :permissions, after_add: :cache_update, after_remove: :cache_update, before_add: :validate_agent_limit

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Signature < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
has_many :groups, after_add: :cache_update, after_remove: :cache_update
validates :name, presence: true

View file

@ -1,8 +1,8 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Sla < ApplicationModel
include NotifiesClients
include ValidatesCondition
include ChecksClientNotification
include ChecksConditionValidation
load 'sla/assets.rb'
include Sla::Assets

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Template < ApplicationModel
include NotifiesClients
include ChecksClientNotification
store :options
validates :name, presence: true

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class TextModule < ApplicationModel
include NotifiesClients
include ChecksClientNotification
validates :name, presence: true
validates :content, presence: true

View file

@ -1,12 +1,12 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Ticket < ApplicationModel
include LogsActivityStream
include NotifiesClients
include LatestChangeObserved
include Historisable
include Taggable
include SearchIndexed
include HasActivityStreamLog
include ChecksClientNotification
include ChecksLatestChangeObserved
include HasHistory
include HasTags
include HasSearchIndexBackend
include Ticket::Escalation
include Ticket::Subject

View file

@ -1,9 +1,9 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Ticket::Article < ApplicationModel
include LogsActivityStream
include NotifiesClients
include Historisable
include HtmlSanitized
include HasActivityStreamLog
include ChecksClientNotification
include HasHistory
include ChecksHtmlSanitized
load 'ticket/article/assets.rb'
include Ticket::Article::Assets
@ -305,12 +305,12 @@ returns
end
class Sender < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
validates :name, presence: true
end
class Type < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
validates :name, presence: true
end
end

View file

@ -1,6 +1,6 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Ticket::State < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
after_create :ensure_defaults
after_update :ensure_defaults

View file

@ -1,6 +1,6 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Ticket::StateType < ApplicationModel
include LatestChangeObserved
include ChecksLatestChangeObserved
has_many :states, class_name: 'Ticket::State'
validates :name, presence: true

View file

@ -1,7 +1,7 @@
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
class Trigger < ApplicationModel
include ValidatesCondition
include ChecksConditionValidation
store :condition
store :perform

View file

@ -24,10 +24,10 @@ require 'digest/md5'
# @property active [Boolean] The flag that shows the active state of the User.
# @property note [String] The note or comment stored to the User.
class User < ApplicationModel
include LogsActivityStream
include NotifiesClients
include Historisable
include SearchIndexed
include HasActivityStreamLog
include ChecksClientNotification
include HasHistory
include HasSearchIndexBackend
load 'user/permission.rb'
include User::Permission