12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601 |
- # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
- class ApplicationModel < ActiveRecord::Base
- include ApplicationModel::Assets
- include ApplicationModel::HistoryLogBase
- include ApplicationModel::ActivityStreamBase
- include ApplicationModel::SearchIndexBase
- self.abstract_class = true
- before_create :check_attributes_protected, :check_limits, :cache_delete, :fill_up_user_create
- before_update :check_limits, :fill_up_user_update
- before_destroy :destroy_dependencies
- after_create :cache_delete
- after_update :cache_delete
- after_touch :cache_delete
- after_destroy :cache_delete
- after_create :attachments_buffer_check
- after_update :attachments_buffer_check
- after_create :activity_stream_create
- after_update :activity_stream_update
- before_destroy :activity_stream_destroy
- after_create :history_create
- after_update :history_update
- after_destroy :history_destroy
- after_create :search_index_update
- after_update :search_index_update
- after_touch :search_index_update
- after_destroy :search_index_destroy
- before_destroy :recent_view_destroy
- # create instance accessor
- class << self
- attr_accessor :activity_stream_support_config, :history_support_config, :search_index_support_config, :attributes_with_associations_support_config
- end
- attr_accessor :history_changes_last_done
- def check_attributes_protected
- import_class_list = ['Ticket', 'Ticket::Article', 'History', 'Ticket::State', 'Ticket::StateType', 'Ticket::Priority', 'Group', 'User', 'Role' ]
- # do noting, use id as it is
- return if !Setting.get('system_init_done')
- return if Setting.get('import_mode') && import_class_list.include?(self.class.to_s)
- self[:id] = nil
- end
- =begin
- remove all not used model attributes of params
- result = Model.param_cleanup(params)
- for object creation, ignore id's
- result = Model.param_cleanup(params, true)
- returns
- result = params # params with valid attributes of model
- =end
- def self.param_cleanup(params, new_object = false)
- if params.respond_to?('permit!')
- params.permit!
- end
- if params.nil?
- raise ArgumentError, "No params for #{self}!"
- end
- data = {}
- params.each { |key, value|
- data[key.to_sym] = value
- }
- # ignore id for new objects
- if new_object && params[:id]
- data.delete(:id)
- end
- # only use object attributes
- clean_params = {}
- new.attributes.each { |attribute, _value|
- next if !data.key?(attribute.to_sym)
- clean_params[attribute.to_sym] = data[attribute.to_sym]
- }
- # we do want to set this via database
- param_validation(clean_params)
- end
- =begin
- set relations of model based on params
- model = Model.find(1)
- result = model.param_set_associations(params)
- returns
- result = true|false
- =end
- def param_set_associations(params)
- # set relations by id/verify if ref exists
- self.class.reflect_on_all_associations.map { |assoc|
- real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
- real_ids = real_ids.to_sym
- next if !params.key?(real_ids)
- list_of_items = params[real_ids]
- if params[real_ids].class != Array
- list_of_items = [ params[real_ids] ]
- end
- list = []
- list_of_items.each { |item_id|
- next if !item_id
- lookup = assoc.klass.lookup(id: item_id)
- # complain if we found no reference
- if !lookup
- raise ArgumentError, "No value found for '#{assoc.name}' with id #{item_id.inspect}"
- end
- list.push item_id
- }
- #p "SEND #{real_ids} = #{list.inspect}"
- send("#{real_ids}=", list)
- }
- # set relations by name/lookup
- self.class.reflect_on_all_associations.map { |assoc|
- real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
- next if !respond_to?(real_ids)
- real_values = assoc.name.to_s[0, assoc.name.to_s.length - 1] + 's'
- real_values = real_values.to_sym
- next if !respond_to?(real_values)
- next if !params[real_values]
- next if params[real_values].class != Array
- list = []
- class_object = assoc.klass
- params[real_values].each { |value|
- lookup = nil
- if class_object == User
- if !lookup
- lookup = class_object.lookup(login: value)
- end
- if !lookup
- lookup = class_object.lookup(email: value)
- end
- else
- lookup = class_object.lookup(name: value)
- end
- # complain if we found no reference
- if !lookup
- raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
- end
- list.push lookup.id
- }
- #p "SEND #{real_ids} = #{list.inspect}"
- send("#{real_ids}=", list)
- }
- end
- =begin
- get relations of model based on params
- model = Model.find(1)
- attributes = model.attributes_with_associations
- returns
- hash with attributes and association ids
- =end
- def attributes_with_associations
- key = "#{self.class}::aws::#{id}"
- cache = Cache.get(key)
- return cache if cache
- # get relations
- attributes = self.attributes
- self.class.reflect_on_all_associations.map { |assoc|
- real_ids = assoc.name.to_s[0, assoc.name.to_s.length - 1] + '_ids'
- next if self.class.attributes_with_associations_support_config && self.class.attributes_with_associations_support_config[:ignore][real_ids.to_sym] == true
- next if !respond_to?(real_ids)
- attributes[real_ids] = send(real_ids)
- }
- Cache.write(key, attributes)
- attributes
- end
- =begin
- get relation name of model based on params
- model = Model.find(1)
- attributes = model.attributes_with_relation_names
- returns
- hash with attributes, association ids, association names and relation name
- =end
- def attributes_with_relation_names
- # get relations
- attributes = attributes_with_associations
- self.class.reflect_on_all_associations.map { |assoc|
- next if !respond_to?(assoc.name)
- ref = send(assoc.name)
- next if !ref
- if ref.respond_to?(:first)
- attributes[assoc.name.to_s] = []
- ref.each { |item|
- if item[:login]
- attributes[assoc.name.to_s].push item[:login]
- next
- end
- next if !item[:name]
- attributes[assoc.name.to_s].push item[:name]
- }
- if ref.count.positive? && attributes[assoc.name.to_s].empty?
- attributes.delete(assoc.name.to_s)
- end
- next
- end
- if ref[:login]
- attributes[assoc.name.to_s] = ref[:login]
- next
- end
- next if !ref[:name]
- attributes[assoc.name.to_s] = ref[:name]
- }
- # fill created_by/updated_by
- {
- 'created_by_id' => 'created_by',
- 'updated_by_id' => 'updated_by',
- }.each { |source, destination|
- next if !attributes[source]
- user = User.lookup(id: attributes[source])
- next if !user
- attributes[destination] = user.login
- }
- # remove forbitten attributes
- %w(password token tokens token_ids).each { |item|
- attributes.delete(item)
- }
- attributes
- end
- =begin
- remove all not used params of object (per default :updated_at, :created_at, :updated_by_id and :created_by_id)
- result = Model.param_validation(params)
- returns
- result = params # params without listed attributes
- =end
- def self.param_validation(data)
- # we do want to set this via database
- [:action, :controller, :updated_at, :created_at, :updated_by_id, :created_by_id, :updated_by, :created_by].each { |key|
- data.delete(key)
- }
- data
- end
- =begin
- do name/login/email based lookup for associations
- params = {
- login: 'some login',
- firstname: 'some firstname',
- lastname: 'some lastname',
- email: 'some email',
- organization: 'some organization',
- roles: ['Agent', 'Admin'],
- }
- attributes = Model.param_association_lookup(params)
- returns
- attributes = params # params with possible lookups
- attributes = {
- login: 'some login',
- firstname: 'some firstname',
- lastname: 'some lastname',
- email: 'some email',
- organization_id: 123,
- role_ids: [2,1],
- }
- =end
- def self.param_association_lookup(params)
- data = {}
- params.each { |key, value|
- data[key.to_sym] = value
- }
- data.symbolize_keys!
- available_attributes = attribute_names
- reflect_on_all_associations.map { |assoc|
- value = data[assoc.name.to_sym]
- next if !value # next if we do not have a value
- ref_name = "#{assoc.name}_id"
- # handle _id values
- if available_attributes.include?(ref_name) # if we do have an _id attribute
- next if data[ref_name.to_sym] # next if we have already the _id filled
- # get association class and do lookup
- class_object = assoc.klass
- lookup = nil
- if class_object == User
- if value.class == String
- if !lookup
- lookup = class_object.lookup(login: value)
- end
- if !lookup
- lookup = class_object.lookup(email: value)
- end
- else
- raise ArgumentError, "String is needed as ref value #{value.inspect} for '#{assoc.name}'"
- end
- else
- lookup = class_object.lookup(name: value)
- end
- # complain if we found no reference
- if !lookup
- raise ArgumentError, "No lookup value found for '#{assoc.name}': #{value.inspect}"
- end
- # release data value
- data.delete(assoc.name.to_sym)
- # remember id reference
- data[ref_name.to_sym] = lookup.id
- next
- end
- next if value.class != Array
- next if value.empty?
- next if value[0].class != String
- # handle _ids values
- ref_names = "#{assoc.name[0, assoc.name.length - 1]}_ids"
- generic_object_tmp = new
- next unless generic_object_tmp.respond_to?(ref_names) # if we do have an _ids attribute
- next if data[ref_names.to_sym] # next if we have already the _ids filled
- # get association class and do lookup
- class_object = assoc.klass
- lookup_ids = []
- value.each { |item|
- lookup = nil
- if class_object == User
- if item.class == String
- if !lookup
- lookup = class_object.lookup(login: item)
- end
- if !lookup
- lookup = class_object.lookup(email: item)
- end
- else
- raise ArgumentError, "String is needed in array ref as ref value #{value.inspect} for '#{assoc.name}'"
- end
- else
- lookup = class_object.lookup(name: item)
- end
- # complain if we found no reference
- if !lookup
- raise ArgumentError, "No lookup value found for '#{assoc.name}': #{item.inspect}"
- end
- lookup_ids.push lookup.id
- }
- # release data value
- data.delete(assoc.name.to_sym)
- # remember id reference
- data[ref_names.to_sym] = lookup_ids
- }
- data
- end
- =begin
- set created_by_id & updated_by_id if not given based on UserInfo (current session)
- Used as before_create callback, no own use needed
- result = Model.fill_up_user_create(params)
- returns
- result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
- =end
- def fill_up_user_create
- if self.class.column_names.include? 'updated_by_id'
- if UserInfo.current_user_id
- if updated_by_id && updated_by_id != UserInfo.current_user_id
- logger.info "NOTICE create - self.updated_by_id is different: #{updated_by_id}/#{UserInfo.current_user_id}"
- end
- self.updated_by_id = UserInfo.current_user_id
- end
- end
- return if !self.class.column_names.include? 'created_by_id'
- return if !UserInfo.current_user_id
- if created_by_id && created_by_id != UserInfo.current_user_id
- logger.info "NOTICE create - self.created_by_id is different: #{created_by_id}/#{UserInfo.current_user_id}"
- end
- self.created_by_id = UserInfo.current_user_id
- end
- =begin
- set updated_by_id if not given based on UserInfo (current session)
- Used as before_update callback, no own use needed
- result = Model.fill_up_user_update(params)
- returns
- result = params # params with updated_by_id & created_by_id if not given based on UserInfo (current session)
- =end
- def fill_up_user_update
- return if !self.class.column_names.include? 'updated_by_id'
- return if !UserInfo.current_user_id
- self.updated_by_id = UserInfo.current_user_id
- end
- def cache_update(o)
- cache_delete if respond_to?('cache_delete')
- o.cache_delete if o.respond_to?('cache_delete')
- end
- def cache_delete
- keys = []
- # delete by id caches
- keys.push "#{self.class}::#{id}"
- # delete by id with attributes_with_associations caches
- keys.push "#{self.class}::aws::#{id}"
- # delete by name caches
- if self[:name]
- keys.push "#{self.class}::#{name}"
- end
- # delete by login caches
- if self[:login]
- keys.push "#{self.class}::#{login}"
- end
- keys.each { |key|
- Cache.delete(key)
- }
- # delete old name / login caches
- if changed?
- if changes.key?('name')
- name = changes['name'][0]
- key = "#{self.class}::#{name}"
- Cache.delete(key)
- end
- if changes.key?('login')
- name = changes['login'][0]
- key = "#{self.class}::#{name}"
- Cache.delete(key)
- end
- end
- end
- def self.cache_set(data_id, data)
- key = "#{self}::#{data_id}"
- Cache.write(key, data)
- end
- def self.cache_get(data_id)
- key = "#{self}::#{data_id}"
- Cache.get(key)
- end
- =begin
- generate uniq name (will check name of model and generates _1 sequenze)
- Used as before_update callback, no own use needed
- name = Model.genrate_uniq_name('some name')
- returns
- result = 'some name_X'
- =end
- def self.genrate_uniq_name(name)
- return name if !find_by(name: name)
- (1..100).each { |counter|
- name = "#{name}_#{counter}"
- exists = find_by(name: name)
- next if exists
- break
- }
- name
- end
- =begin
- lookup model from cache (if exists) or retrieve it from db, id, name, login or email possible
- result = Model.lookup(id: 123)
- result = Model.lookup(name: 'some name')
- result = Model.lookup(login: 'some login')
- result = Model.lookup(email: 'some login')
- returns
- result = model # with all attributes
- =end
- def self.lookup(data)
- if data[:id]
- cache = cache_get(data[:id])
- return cache if cache
- record = find_by(id: data[:id])
- cache_set(data[:id], record)
- return record
- elsif data[:name]
- cache = cache_get(data[:name])
- return cache if cache
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(name) = LOWER(?)', data[:name])
- else
- where(name: data[:name])
- end
- records.each { |loop_record|
- if loop_record.name == data[:name]
- cache_set(data[:name], loop_record)
- return loop_record
- end
- }
- return
- elsif data[:login]
- cache = cache_get(data[:login])
- return cache if cache
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(login) = LOWER(?)', data[:login])
- else
- where(login: data[:login])
- end
- records.each { |loop_record|
- if loop_record.login == data[:login]
- cache_set(data[:login], loop_record)
- return loop_record
- end
- }
- return
- elsif data[:email]
- cache = cache_get(data[:email])
- return cache if cache
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(email) = LOWER(?)', data[:email])
- else
- where(email: data[:email])
- end
- records.each { |loop_record|
- if loop_record.email == data[:email]
- cache_set(data[:email], loop_record)
- return loop_record
- end
- }
- return
- end
- raise ArgumentError, 'Need name, id, login or email for lookup()'
- end
- =begin
- create model if not exists (check exists based on id, name, login, email or locale)
- result = Model.create_if_not_exists(attributes)
- returns
- result = model # with all attributes
- =end
- def self.create_if_not_exists(data)
- if data[:id]
- record = find_by(id: data[:id])
- return record if record
- elsif data[:name]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(name) = LOWER(?)', data[:name])
- else
- where(name: data[:name])
- end
- records.each { |loop_record|
- return loop_record if loop_record.name == data[:name]
- }
- elsif data[:login]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(login) = LOWER(?)', data[:login])
- else
- where(login: data[:login])
- end
- records.each { |loop_record|
- return loop_record if loop_record.login == data[:login]
- }
- elsif data[:email]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(email) = LOWER(?)', data[:email])
- else
- where(email: data[:email])
- end
- records.each { |loop_record|
- return loop_record if loop_record.email == data[:email]
- }
- elsif data[:locale] && data[:source]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(locale) = LOWER(?) AND LOWER(source) = LOWER(?)', data[:locale], data[:source])
- else
- where(locale: data[:locale], source: data[:source])
- end
- records.each { |loop_record|
- return loop_record if loop_record.source == data[:source]
- }
- end
- create(data)
- end
- =begin
- Model.create_if_not_exists with ref lookups
- result = Model.create_if_not_exists_with_ref(attributes)
- returns
- result = model # with all attributes
- =end
- def self.create_if_not_exists_with_ref(data)
- data = param_association_lookup(data)
- create_or_update(data)
- end
- =begin
- create or update model (check exists based on id, name, login, email or locale)
- result = Model.create_or_update(attributes)
- returns
- result = model # with all attributes
- =end
- def self.create_or_update(data)
- if data[:id]
- record = find_by(id: data[:id])
- if record
- record.update_attributes(data)
- return record
- end
- record = new(data)
- record.save
- return record
- elsif data[:name]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(name) = LOWER(?)', data[:name])
- else
- where(name: data[:name])
- end
- records.each { |loop_record|
- if loop_record.name == data[:name]
- loop_record.update_attributes(data)
- return loop_record
- end
- }
- record = new(data)
- record.save
- return record
- elsif data[:login]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(login) = LOWER(?)', data[:login])
- else
- where(login: data[:login])
- end
- records.each { |loop_record|
- if loop_record.login.casecmp(data[:login]).zero?
- loop_record.update_attributes(data)
- return loop_record
- end
- }
- record = new(data)
- record.save
- return record
- elsif data[:email]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(email) = LOWER(?)', data[:email])
- else
- where(email: data[:email])
- end
- records.each { |loop_record|
- if loop_record.email.casecmp(data[:email]).zero?
- loop_record.update_attributes(data)
- return loop_record
- end
- }
- record = new(data)
- record.save
- return record
- elsif data[:locale]
- # do lookup with == to handle case insensitive databases
- records = if Rails.application.config.db_case_sensitive
- where('LOWER(locale) = LOWER(?)', data[:locale])
- else
- where(locale: data[:locale])
- end
- records.each { |loop_record|
- if loop_record.locale.casecmp(data[:locale]).zero?
- loop_record.update_attributes(data)
- return loop_record
- end
- }
- record = new(data)
- record.save
- return record
- else
- raise ArgumentError, 'Need name, login, email or locale for create_or_update()'
- end
- end
- =begin
- Model.create_or_update with ref lookups
- result = Model.create_or_update(attributes)
- returns
- result = model # with all attributes
- =end
- def self.create_or_update_with_ref(data)
- data = param_association_lookup(data)
- create_or_update(data)
- end
- =begin
- activate latest change on create, update, touch and destroy
- class Model < ApplicationModel
- latest_change_support
- end
- =end
- def self.latest_change_support
- after_create :latest_change_set_from_observer
- after_update :latest_change_set_from_observer
- after_touch :latest_change_set_from_observer
- after_destroy :latest_change_set_from_observer_destroy
- end
- def latest_change_set_from_observer
- self.class.latest_change_set(updated_at)
- end
- def latest_change_set_from_observer_destroy
- self.class.latest_change_set(nil)
- end
- def self.latest_change_set(updated_at)
- key = "#{new.class.name}_latest_change"
- expires_in = 31_536_000 # 1 year
- if updated_at.nil?
- Cache.delete(key)
- else
- Cache.write(key, updated_at, { expires_in: expires_in })
- end
- end
- =begin
- get latest updated_at object timestamp
- latest_change = Ticket.latest_change
- returns
- result = timestamp
- =end
- def self.latest_change
- key = "#{new.class.name}_latest_change"
- updated_at = Cache.get(key)
- # if we do not have it cached, do lookup
- if !updated_at
- o = select(:updated_at).order(updated_at: :desc).limit(1).first
- if o
- updated_at = o.updated_at
- latest_change_set(updated_at)
- end
- end
- updated_at
- end
- =begin
- activate client notify support on create, update, touch and destroy
- class Model < ApplicationModel
- notify_clients_support
- end
- =end
- def self.notify_clients_support
- after_create :notify_clients_after_create
- after_update :notify_clients_after_update
- after_touch :notify_clients_after_touch
- after_destroy :notify_clients_after_destroy
- end
- =begin
- notify_clients_after_create after model got created
- used as callback in model file
- class OwnModel < ApplicationModel
- after_create :notify_clients_after_create
- after_update :notify_clients_after_update
- after_touch :notify_clients_after_touch
- after_destroy :notify_clients_after_destroy
- [...]
- =end
- def notify_clients_after_create
- # return if we run import mode
- return if Setting.get('import_mode')
- logger.debug "#{self.class.name}.find(#{id}) notify created " + created_at.to_s
- class_name = self.class.name
- class_name.gsub!(/::/, '')
- PushMessages.send(
- message: {
- event: class_name + ':create',
- data: { id: id, updated_at: updated_at }
- },
- type: 'authenticated',
- )
- end
- =begin
- notify_clients_after_update after model got updated
- used as callback in model file
- class OwnModel < ApplicationModel
- after_create :notify_clients_after_create
- after_update :notify_clients_after_update
- after_touch :notify_clients_after_touch
- after_destroy :notify_clients_after_destroy
- [...]
- =end
- def notify_clients_after_update
- # return if we run import mode
- return if Setting.get('import_mode')
- logger.debug "#{self.class.name}.find(#{id}) notify UPDATED " + updated_at.to_s
- class_name = self.class.name
- class_name.gsub!(/::/, '')
- PushMessages.send(
- message: {
- event: class_name + ':update',
- data: { id: id, updated_at: updated_at }
- },
- type: 'authenticated',
- )
- end
- =begin
- notify_clients_after_touch after model got touched
- used as callback in model file
- class OwnModel < ApplicationModel
- after_create :notify_clients_after_create
- after_update :notify_clients_after_update
- after_touch :notify_clients_after_touch
- after_destroy :notify_clients_after_destroy
- [...]
- =end
- def notify_clients_after_touch
- # return if we run import mode
- return if Setting.get('import_mode')
- logger.debug "#{self.class.name}.find(#{id}) notify TOUCH " + updated_at.to_s
- class_name = self.class.name
- class_name.gsub!(/::/, '')
- PushMessages.send(
- message: {
- event: class_name + ':touch',
- data: { id: id, updated_at: updated_at }
- },
- type: 'authenticated',
- )
- end
- =begin
- notify_clients_after_destroy after model got destroyed
- used as callback in model file
- class OwnModel < ApplicationModel
- after_create :notify_clients_after_create
- after_update :notify_clients_after_update
- after_touch :notify_clients_after_touch
- after_destroy :notify_clients_after_destroy
- [...]
- =end
- def notify_clients_after_destroy
- # return if we run import mode
- return if Setting.get('import_mode')
- logger.debug "#{self.class.name}.find(#{id}) notify DESTOY " + updated_at.to_s
- class_name = self.class.name
- class_name.gsub!(/::/, '')
- PushMessages.send(
- message: {
- event: class_name + ':destroy',
- data: { id: id, updated_at: updated_at }
- },
- type: 'authenticated',
- )
- end
- =begin
- serve methode to configure and enable search index support for this model
- class Model < ApplicationModel
- search_index_support
- ignore_attributes: {
- create_article_type_id: true,
- create_article_sender_id: true,
- article_count: true,
- },
- ignore_ids: [1,2,4]
- end
- =end
- def self.search_index_support(data = {})
- @search_index_support_config = data
- end
- =begin
- update search index, if configured - will be executed automatically
- model = Model.find(123)
- model.search_index_update
- =end
- def search_index_update
- config = self.class.search_index_support_config
- return if !config
- return if config[:ignore_ids] && config[:ignore_ids].include?(id)
- # start background job to transfer data to search index
- return if !SearchIndexBackend.enabled?
- Delayed::Job.enqueue(ApplicationModel::BackgroundJobSearchIndex.new(self.class.to_s, id))
- end
- =begin
- delete search index object, will be executed automatically
- model = Model.find(123)
- model.search_index_destroy
- =end
- def search_index_destroy
- config = self.class.search_index_support_config
- return if !config
- return if config[:ignore_ids] && config[:ignore_ids].include?(id)
- SearchIndexBackend.remove(self.class.to_s, id)
- end
- =begin
- reload search index with full data
- Model.search_index_reload
- =end
- def self.search_index_reload
- config = @search_index_support_config
- return if !config
- tolerance = 5
- tolerance_count = 0
- all_ids = select('id').all.order('created_at DESC')
- all_ids.each { |item_with_id|
- next if config[:ignore_ids] && config[:ignore_ids].include?(item_with_id.id)
- item = find(item_with_id.id)
- begin
- item.search_index_update_backend
- rescue => e
- logger.error "Unable to send #{item.class}.find(#{item.id}) backend: #{e.inspect}"
- tolerance_count += 1
- raise "Unable to send #{item.class}.find(#{item.id}) backend: #{e.inspect}" if tolerance_count == tolerance
- end
- }
- end
- =begin
- serve methode to configure and enable activity stream support for this model
- class Model < ApplicationModel
- activity_stream_support permission: 'admin.user'
- end
- =end
- def self.activity_stream_support(data = {})
- @activity_stream_support_config = data
- end
- =begin
- log object create activity stream, if configured - will be executed automatically
- model = Model.find(123)
- model.activity_stream_create
- =end
- def activity_stream_create
- return if !self.class.activity_stream_support_config
- activity_stream_log('create', self['created_by_id'])
- end
- =begin
- log object update activity stream, if configured - will be executed automatically
- model = Model.find(123)
- model.activity_stream_update
- =end
- def activity_stream_update
- return if !self.class.activity_stream_support_config
- return if !changed?
- # default ignored attributes
- ignore_attributes = {
- created_at: true,
- updated_at: true,
- created_by_id: true,
- updated_by_id: true,
- }
- if self.class.activity_stream_support_config[:ignore_attributes]
- self.class.activity_stream_support_config[:ignore_attributes].each { |key, value|
- ignore_attributes[key] = value
- }
- end
- log = false
- changes.each { |key, _value|
- # do not log created_at and updated_at attributes
- next if ignore_attributes[key.to_sym] == true
- log = true
- }
- return if !log
- activity_stream_log('update', self['updated_by_id'])
- end
- =begin
- delete object activity stream, will be executed automatically
- model = Model.find(123)
- model.activity_stream_destroy
- =end
- def activity_stream_destroy
- return if !self.class.activity_stream_support_config
- ActivityStream.remove(self.class.to_s, id)
- end
- =begin
- serve methode to configure and enable history support for this model
- class Model < ApplicationModel
- history_support
- end
- class Model < ApplicationModel
- history_support ignore_attributes: { article_count: true }
- end
- =end
- def self.history_support(data = {})
- @history_support_config = data
- end
- =begin
- log object create history, if configured - will be executed automatically
- model = Model.find(123)
- model.history_create
- =end
- def history_create
- return if !self.class.history_support_config
- #logger.debug 'create ' + self.changes.inspect
- history_log('created', created_by_id)
- end
- =begin
- log object update history with all updated attributes, if configured - will be executed automatically
- model = Model.find(123)
- model.history_update
- =end
- def history_update
- return if !self.class.history_support_config
- return if !changed?
- # return if it's no update
- return if new_record?
- # new record also triggers update, so ignore new records
- changes = self.changes
- if history_changes_last_done
- history_changes_last_done.each { |key, value|
- if changes.key?(key) && changes[key] == value
- changes.delete(key)
- end
- }
- end
- self.history_changes_last_done = changes
- #logger.info 'updated ' + self.changes.inspect
- return if changes['id'] && !changes['id'][0]
- # default ignored attributes
- ignore_attributes = {
- created_at: true,
- updated_at: true,
- created_by_id: true,
- updated_by_id: true,
- }
- if self.class.history_support_config[:ignore_attributes]
- self.class.history_support_config[:ignore_attributes].each { |key, value|
- ignore_attributes[key] = value
- }
- end
- changes.each { |key, value|
- # do not log created_at and updated_at attributes
- next if ignore_attributes[key.to_sym] == true
- # get attribute name
- attribute_name = key.to_s
- if attribute_name[-3, 3] == '_id'
- attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
- end
- value_id = []
- value_str = [ value[0], value[1] ]
- if key.to_s[-3, 3] == '_id'
- value_id[0] = value[0]
- value_id[1] = value[1]
- if respond_to?(attribute_name) && send(attribute_name)
- relation_class = send(attribute_name).class
- if relation_class && value_id[0]
- relation_model = relation_class.lookup(id: value_id[0])
- if relation_model
- if relation_model['name']
- value_str[0] = relation_model['name']
- elsif relation_model.respond_to?('fullname')
- value_str[0] = relation_model.send('fullname')
- end
- end
- end
- if relation_class && value_id[1]
- relation_model = relation_class.lookup(id: value_id[1])
- if relation_model
- if relation_model['name']
- value_str[1] = relation_model['name']
- elsif relation_model.respond_to?('fullname')
- value_str[1] = relation_model.send('fullname')
- end
- end
- end
- end
- end
- data = {
- history_attribute: attribute_name,
- value_from: value_str[0].to_s,
- value_to: value_str[1].to_s,
- id_from: value_id[0],
- id_to: value_id[1],
- }
- #logger.info "HIST NEW #{self.class.to_s}.find(#{self.id}) #{data.inspect}"
- history_log('updated', updated_by_id, data)
- }
- end
- =begin
- delete object history, will be executed automatically
- model = Model.find(123)
- model.history_destroy
- =end
- def history_destroy
- return if !self.class.history_support_config
- History.remove(self.class.to_s, id)
- end
- =begin
- serve methode to configure and attributes_with_associations support for this model
- class Model < ApplicationModel
- attributes_with_associations(ignore: { user_ids: => true })
- end
- =end
- def self.attributes_with_associations_support(data = {})
- @attributes_with_associations_support_config = data
- end
- =begin
- get list of attachments of this object
- item = Model.find(123)
- list = item.attachments
- returns
- # array with Store model objects
- =end
- def attachments
- Store.list(object: self.class.to_s, o_id: id)
- end
- =begin
- store attachments for this object
- item = Model.find(123)
- item.attachments = [ Store-Object1, Store-Object2 ]
- =end
- def attachments=(attachments)
- self.attachments_buffer = attachments
- # update if object already exists
- return if !(id && id.nonzero?)
- attachments_buffer_check
- end
- =begin
- return object and assets
- data = Model.full(123)
- data = {
- id: 123,
- assets: assets,
- }
- =end
- def self.full(id)
- object = find(id)
- assets = object.assets({})
- {
- id: id,
- assets: assets,
- }
- end
- =begin
- get assets of object list
- list = [
- {
- object => 'Ticket',
- o_id => 1,
- },
- {
- object => 'User',
- o_id => 121,
- },
- ]
- assets = Model.assets_of_object_list(list, assets)
- =end
- def self.assets_of_object_list(list, assets = {})
- list.each { |item|
- require item['object'].to_filename
- record = Kernel.const_get(item['object']).find(item['o_id'])
- assets = record.assets(assets)
- if item['created_by_id']
- user = User.find(item['created_by_id'])
- assets = user.assets(assets)
- end
- if item['updated_by_id']
- user = User.find(item['updated_by_id'])
- assets = user.assets(assets)
- end
- }
- assets
- end
- =begin
- get assets and record_ids of selector
- model = Model.find(123)
- assets = model.assets_of_selector('attribute_name_of_selector', assets)
- =end
- def assets_of_selector(selector, assets = {})
- # get assets of condition
- models = Models.all
- send(selector).each { |item, content|
- attribute = item.split(/\./)
- next if !attribute[1]
- begin
- attribute_class = attribute[0].to_classname.constantize
- rescue => e
- logger.error "Unable to get asset for '#{attribute[0]}': #{e.inspect}"
- next
- end
- reflection = attribute[1].sub(/_id$/, '')
- #reflection = reflection.to_sym
- 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
- if content['value'].class == Array
- content['value'].each { |item_id|
- attribute_object = attribute_ref_class.find_by(id: item_id)
- if attribute_object
- assets = attribute_object.assets(assets)
- end
- }
- else
- attribute_object = attribute_ref_class.find_by(id: content['value'])
- if attribute_object
- assets = attribute_object.assets(assets)
- end
- end
- }
- assets
- end
- =begin
- touch references by params
- Model.touch_reference_by_params(
- object: 'Ticket',
- o_id: 123,
- )
- =end
- def self.touch_reference_by_params(data)
- object_class = Kernel.const_get(data[:object])
- object = object_class.lookup(id: data[:o_id])
- return if !object
- object.touch
- rescue => e
- logger.error e.message
- logger.error e.backtrace.inspect
- end
- private
- def attachments_buffer
- @attachments_buffer_data
- end
- def attachments_buffer=(attachments)
- @attachments_buffer_data = attachments
- end
- def attachments_buffer_check
- # do nothing if no attachment exists
- return 1 if attachments_buffer.nil?
- # store attachments
- article_store = []
- attachments_buffer.each do |attachment|
- article_store.push Store.add(
- object: self.class.to_s,
- o_id: id,
- data: attachment.content,
- filename: attachment.filename,
- preferences: attachment.preferences,
- created_by_id: created_by_id,
- )
- end
- attachments_buffer = nil
- end
- =begin
- delete object recent viewed list, will be executed automatically
- model = Model.find(123)
- model.recent_view_destroy
- =end
- def recent_view_destroy
- RecentView.log_destroy(self.class.to_s, id)
- end
- =begin
- check string/varchar size and cut them if needed
- =end
- def check_limits
- attributes.each { |attribute|
- next if !self[ attribute[0] ]
- next if self[ attribute[0] ].class != String
- next if self[ attribute[0] ].empty?
- column = self.class.columns_hash[ attribute[0] ]
- next if !column
- limit = column.limit
- if column && limit
- current_length = attribute[1].to_s.length
- if limit < current_length
- logger.warn "WARNING: cut string because of database length #{self.class}.#{attribute[0]}(#{limit} but is #{current_length}:#{attribute[1]})"
- self[ attribute[0] ] = attribute[1][ 0, limit ]
- end
- end
- # strip 4 bytes utf8 chars if needed
- if column && self[ attribute[0] ]
- self[attribute[0]] = self[ attribute[0] ].utf8_to_3bytesutf8
- end
- }
- end
- =begin
- destroy object dependencies, will be executed automatically
- =end
- def destroy_dependencies
- end
- end
|