Project

General

Profile

« Previous | Next » 

Revision b597a8a5

Added by Shimon Shtein about 8 years ago

Fixes #14369 - Removed default scope from host counter in taxonomies

View differences:

app/controllers/concerns/foreman/controller/taxonomies_controller.rb
respond_to do |format|
format.html do
@taxonomies = values.paginate(:page => params[:page])
@counter = Host.group(taxonomy_id).where(taxonomy_id => values).count
@counter = hosts_scope.group(taxonomy_id).where(taxonomy_id => values).count
render 'taxonomies/index'
end
format.json
......
def assign_hosts
@taxonomy_type = taxonomy_single.classify
@hosts = Host.authorized(:view_hosts, Host).send("no_#{taxonomy_single}").includes(included_associations).search_for(params[:search],:order => params[:order]).paginate(:page => params[:page])
@hosts = hosts_scope_without_taxonomy.includes(included_associations).search_for(params[:search],:order => params[:order]).paginate(:page => params[:page])
render "hosts/assign_hosts"
end
def assign_all_hosts
Host.send("no_#{taxonomy_single}").update_all(taxonomy_id => @taxonomy.id)
hosts_scope_without_taxonomy.update_all(taxonomy_id => @taxonomy.id)
@taxonomy.import_missing_ids
redirect_to send("#{taxonomies_plural}_path"), :notice => _("All hosts previously with no %{single} are now assigned to %{name}") % { :single => taxonomy_single, :name => @taxonomy.name }
end
def assign_selected_hosts
host_ids = params[taxonomy_single.to_sym][:host_ids] - ["0"]
@hosts = Host.where(:id => host_ids).update_all(taxonomy_id => @taxonomy.id)
@hosts = hosts_scope_without_taxonomy.where(:id => host_ids).update_all(taxonomy_id => @taxonomy.id)
@taxonomy.import_missing_ids
redirect_to send("#{taxonomies_plural}_path"), :notice => _("Selected hosts are now assigned to %s") % @taxonomy.name
end
......
def count_nil_hosts
return @count_nil_hosts if @count_nil_hosts
@count_nil_hosts = Host.where(taxonomy_id => nil).count
@count_nil_hosts = hosts_scope_without_taxonomy.count
end
private
def hosts_scope
Host.authorized(:view_hosts, Host)
end
def hosts_scope_without_taxonomy
hosts_scope.send("no_#{taxonomy_single}")
end
end
app/models/host/base.rb
conditions
end
scope :no_location, -> { where(:location_id => nil) }
scope :no_organization, -> { where(:organization_id => nil) }
scope :no_location, -> { rewhere(:location_id => nil) }
scope :no_organization, -> { rewhere(:organization_id => nil) }
# primary interface is mandatory because of delegated methods so we build it if it's missing
# similar for provision interface
test/functional/locations_controller_test.rb
User.any_instance.expects(:expire_topbar_cache).times(2+User.only_admin.count) #2 users, all admins
put :update, { :id => location.id, :location => {:name => "Topbar Loc" }}, set_session_user
end
context 'wizard' do
test 'redirects to step 2 if unassigned hosts exist' do
host = FactoryGirl.create(:host)
host.update_attributes(:location => nil)
location = FactoryGirl.create(:location)
Location.stubs(:current).returns(location)
post :create, {:location => {:name => "test_loc"} }, set_session_user
assert_redirected_to /step2/
Location.unstub(:current)
end
test 'redirects to step 3 if no unassigned hosts exist' do
post :create, {:location => {:name => "test_loc"} }, set_session_user
assert_redirected_to /edit/
end
test 'redirects to step 3 if no permissins for hosts' do
host = FactoryGirl.create(:host)
host.update_attributes(:location => nil)
Host.stubs(:authorized).returns(Host.where('1=0'))
post :create, {:location => {:name => "test_loc"} }, set_session_user
assert_redirected_to /edit/
Host.unstub(:authorized)
end
end
end
test/functional/organizations_controller_test.rb
User.any_instance.expects(:expire_topbar_cache).times(2+User.only_admin.count) #2 users, all admins
put :update, { :id => organization.id, :organization => {:name => "Topbar Org" }}, set_session_user
end
context 'wizard' do
test 'redirects to step 2 if unassigned hosts exist' do
host = FactoryGirl.create(:host)
host.update_attributes(:organization => nil)
organization = FactoryGirl.create(:organization)
Organization.stubs(:current).returns(organization)
post :create, {:organization => {:name => "test_org"} }, set_session_user
assert_redirected_to /step2/
Organization.unstub(:current)
end
test 'redirects to step 3 if no unassigned hosts exist' do
post :create, {:organization => {:name => "test_org"} }, set_session_user
assert_redirected_to /edit/
end
test 'redirects to step 3 if no permissins for hosts' do
host = FactoryGirl.create(:host)
host.update_attributes(:organization => nil)
Host.stubs(:authorized).returns(Host.where('1=0'))
post :create, {:organization => {:name => "test_org"} }, set_session_user
assert_redirected_to /edit/
Host.unstub(:authorized)
end
end
end
test/unit/host_test.rb
end
end
describe 'taxonomy scopes' do
test 'no_location overrides default scope' do
location = FactoryGirl.create(:location)
host = FactoryGirl.create(:host, :location => nil)
Location.stubs(:current).returns(location)
assert_nil Host.where(:id => host.id).first
assert_not_nil Host.no_location.where(:id => host.id).first
Location.unstub(:current)
end
test 'no_organization overrides default scope' do
organization = FactoryGirl.create(:organization)
host = FactoryGirl.create(:host, :organization => nil)
Organization.stubs(:current).returns(organization)
assert_nil Host.where(:id => host.id).first
assert_not_nil Host.no_organization.where(:id => host.id).first
Organization.unstub(:current)
end
end
private
def parse_json_fixture(relative_path)

Also available in: Unified diff