Revision 7cf99090
Added by Joseph Magen almost 10 years ago
app/models/concerns/hostext/search.rb | ||
---|---|---|
scoped_search :in => :hostgroup, :on => :title, :complete_value => true, :rename => :hostgroup_fullname
|
||
scoped_search :in => :hostgroup, :on => :title, :complete_value => true, :rename => :hostgroup_title
|
||
scoped_search :in => :hostgroup, :on => :id, :complete_value => false, :rename => :hostgroup_id, :only_explicit => true
|
||
scoped_search :in => :hostgroup, :on => :title, :complete_value => true, :rename => :parent_hostgroup, :only_explicit => true, :ext_method => :search_by_hostgroup_and_descendants
|
||
scoped_search :in => :domain, :on => :name, :complete_value => true, :rename => :domain
|
||
scoped_search :in => :domain, :on => :id, :complete_value => true, :rename => :domain_id
|
||
scoped_search :in => :realm, :on => :name, :complete_value => true, :rename => :realm
|
||
... | ... | |
return {:conditions => opts, :include => :hostgroup}
|
||
end
|
||
|
||
def search_by_hostgroup_and_descendants(key, operator, value)
|
||
conditions = sanitize_sql_for_conditions(["hostgroups.title #{operator} ?", value_to_sql(operator, value)])
|
||
# Only one hostgroup (first) is used to determined descendants. Future TODO - alert if result results more than one hostgroup
|
||
hostgroup = Hostgroup.unscoped.with_taxonomy_scope.where(conditions).first
|
||
hostgroup_ids = hostgroup.subtree_ids
|
||
if hostgroup_ids.any?
|
||
opts = "hosts.hostgroup_id IN (#{hostgroup_ids.join(',')})"
|
||
else
|
||
opts = "hosts.id < 0"
|
||
end
|
||
return {:conditions => opts}
|
||
end
|
||
|
||
def search_by_params(key, operator, value)
|
||
key_name = key.sub(/^.*\./,'')
|
||
condition = sanitize_sql_for_conditions(["name = ? and value #{operator} ?", key_name, value_to_sql(operator, value)])
|
test/unit/host_test.rb | ||
---|---|---|
assert_equal hosts.first.hostgroup_id, hostgroup.id
|
||
end
|
||
|
||
test "can search hosts by parent hostgroup and its descendants" do
|
||
#setup - add parent to hostgroup :common (not in fixtures, since no field parent_id)
|
||
hostgroup = hostgroups(:db)
|
||
parent_hostgroup = hostgroups(:common)
|
||
hostgroup.parent_id = parent_hostgroup.id
|
||
assert hostgroup.save!
|
||
|
||
# search hosts by parent hostgroup label
|
||
hosts = Host::Managed.search_for("parent_hostgroup = Common")
|
||
assert_equal hosts.count, 2
|
||
assert_equal ["Common", "Common/db"].sort, hosts.map { |h| h.hostgroup.title }.sort
|
||
end
|
||
|
||
test "non-admin user with edit_hosts permission can update interface" do
|
||
@one = users(:one)
|
||
# add permission for user :one
|
Also available in: Unified diff
fixes #6116 - search hosts by parent host group and its decendants