From 3b8f6259b62861c64e0f572cc28ebea8fd178aaf Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 26 Dec 2013 22:51:25 +0100 Subject: [PATCH] Moved to application model assets (for all models per default). --- app/models/application_model.rb | 1 + app/models/application_model/assets.rb | 49 ++++++++++++++++++++++++++ app/models/group.rb | 4 +-- app/models/group/assets.rb | 34 ------------------ app/models/role.rb | 1 + 5 files changed, 52 insertions(+), 37 deletions(-) create mode 100644 app/models/application_model/assets.rb delete mode 100644 app/models/group/assets.rb diff --git a/app/models/application_model.rb b/app/models/application_model.rb index 75f984adc..7814c10a6 100644 --- a/app/models/application_model.rb +++ b/app/models/application_model.rb @@ -1,6 +1,7 @@ # Copyright (C) 2013-2013 Zammad Foundation, http://zammad-foundation.org/ class ApplicationModel < ActiveRecord::Base + include ApplicationModel::Assets include ApplicationModel::HistoryLogBase include ApplicationModel::ActivityStreamBase diff --git a/app/models/application_model/assets.rb b/app/models/application_model/assets.rb new file mode 100644 index 000000000..7c8289584 --- /dev/null +++ b/app/models/application_model/assets.rb @@ -0,0 +1,49 @@ +# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ + +module ApplicationModel::Assets + +=begin + +get all assets / related models for this user + + user = User.find(123) + result = user.assets( assets_if_exists ) + +returns + + result = { + :users => { + 123 => user_model_123, + 1234 => user_model_1234, + } + } + +=end + + def assets (data) + + if !data[ self.class.to_app_model ] + data[ self.class.to_app_model ] = {} + end + if !data[ self.class.to_app_model ][ self.id ] + data[ self.class.to_app_model ][ self.id ] = self.attributes + end + + return data if !self['created_by_id'] && !self['updated_by_id'] + if !data[ User.to_app_model ] + data[ User.to_app_model ] = {} + end + if self['created_by_id'] + if !data[ User.to_app_model ][ self['created_by_id'] ] + data[ User.to_app_model ][ self['created_by_id'] ] = User.user_data_full( self['created_by_id'] ) + end + end + if self['updated_by_id'] + if !data[ User.to_app_model ][ self['updated_by_id'] ] + data[ User.to_app_model ][ self['updated_by_id'] ] = User.user_data_full( self['updated_by_id'] ) + end + end + data + end + +end diff --git a/app/models/group.rb b/app/models/group.rb index e3a730bc2..b1813e6c9 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,8 +1,6 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ class Group < ApplicationModel - include Group::Assets - has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update belongs_to :email_address belongs_to :signature @@ -10,4 +8,4 @@ class Group < ApplicationModel activity_stream_support :role => 'Admin' history_support -end \ No newline at end of file +end diff --git a/app/models/group/assets.rb b/app/models/group/assets.rb deleted file mode 100644 index 87eacc87a..000000000 --- a/app/models/group/assets.rb +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ - -module Group::Assets - -=begin - -get all assets / related models for this group - - group = Group.find(123) - result = group.assets( assets_if_exists ) - -returns - - result = { - :groups => { - 123 => group_model_123, - 1234 => group_model_1234, - } - } - -=end - - def assets (data) - - if !data[ Group.to_app_model ] - data[ Group.to_app_model ] = {} - end - if !data[ Group.to_app_model ][ self.id ] - data[ Group.to_app_model ][ self.id ] = self.attributes - end - data - end - -end \ No newline at end of file diff --git a/app/models/role.rb b/app/models/role.rb index cfaa3e030..d0a22bfed 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/ class Role < ApplicationModel + has_and_belongs_to_many :users, :after_add => :cache_update, :after_remove => :cache_update validates :name, :presence => true activity_stream_support :role => 'Admin'