Added own module to add member users to search index data.
This commit is contained in:
parent
e1b6523f5f
commit
900fbf7521
2 changed files with 62 additions and 1 deletions
|
@ -3,9 +3,11 @@
|
|||
class Organization < ApplicationModel
|
||||
include Organization::Assets
|
||||
extend Organization::Search
|
||||
include Organization::SearchIndex
|
||||
|
||||
has_and_belongs_to_many :users
|
||||
validates :name, :presence => true
|
||||
has_many :members, :class_name => 'User'
|
||||
validates :name, :presence => true
|
||||
|
||||
activity_stream_support :role => 'Admin'
|
||||
history_support
|
||||
|
|
59
app/models/organization/search_index.rb
Normal file
59
app/models/organization/search_index.rb
Normal file
|
@ -0,0 +1,59 @@
|
|||
# Copyright (C) 2012-2013 Zammad Foundation, http://zammad-foundation.org/
|
||||
|
||||
module Organization::SearchIndex
|
||||
|
||||
=begin
|
||||
|
||||
lookup name of ref. objects
|
||||
|
||||
attributes = search_index_attribute_lookup(attributes, Ticket)
|
||||
|
||||
returns
|
||||
|
||||
attributes # object with lookup data
|
||||
|
||||
=end
|
||||
|
||||
def search_index_attribute_lookup(attributes, ref_object)
|
||||
attributes_new = {}
|
||||
attributes.each {|key, value|
|
||||
next if !value
|
||||
|
||||
# get attribute name
|
||||
attribute_name = key.to_s
|
||||
next if attribute_name[-3,3] != '_id'
|
||||
attribute_name = attribute_name[ 0, attribute_name.length-3 ]
|
||||
|
||||
# check if attribute method exists
|
||||
next if !ref_object.respond_to?( attribute_name )
|
||||
|
||||
# check if method has own class
|
||||
relation_class = ref_object.send(attribute_name).class
|
||||
next if !relation_class
|
||||
|
||||
# lookup ref object
|
||||
relation_model = relation_class.lookup( :id => value )
|
||||
next if !relation_model
|
||||
|
||||
# get name of ref object
|
||||
value = nil
|
||||
if relation_model.respond_to?('search_index_data')
|
||||
value = relation_model.send('search_index_data')
|
||||
end
|
||||
next if !value
|
||||
|
||||
# save name of ref object
|
||||
attributes_new[ attribute_name ] = value
|
||||
attributes.delete(key)
|
||||
}
|
||||
|
||||
attributes['member'] = []
|
||||
users = User.where( :organization_id => self.id )
|
||||
users.each { |user|
|
||||
attributes['member'].push user.search_index_data
|
||||
}
|
||||
|
||||
attributes_new.merge(attributes)
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in a new issue