From: Jiri Tomasek jtomasek@redhat.com
--- src/app/controllers/resources/pools_controller.rb | 18 +++++++++--- src/app/models/pool.rb | 2 +- src/app/views/resources/pools/edit.haml | 29 +++++++++++++++++--- src/app/views/resources/pools/new.haml | 27 ++++++++++++++----- src/features/pool.feature | 6 ++++ src/features/step_definitions/pool_steps.rb | 2 +- src/spec/controllers/pools_controller_spec.rb | 6 +++- 7 files changed, 69 insertions(+), 21 deletions(-)
diff --git a/src/app/controllers/resources/pools_controller.rb b/src/app/controllers/resources/pools_controller.rb index 337a1b8..cd4840a 100644 --- a/src/app/controllers/resources/pools_controller.rb +++ b/src/app/controllers/resources/pools_controller.rb @@ -36,38 +36,46 @@ class Resources::PoolsController < ApplicationController def new require_privilege(Privilege::CREATE, Pool) @pool = Pool.new + @quota = Quota.new end
def create require_privilege(Privilege::CREATE, Pool)
@pool = Pool.new(params[:pool]) - quota = Quota.new - quota.save! + @pool.quota = @quota = Quota.new + + limit = params[:quota][:maximum_running_instances] if params[:quota] + @pool.quota.set_maximum_running_instances(limit)
- @pool.quota_id = quota.id - @pool.pool_family = PoolFamily.default if @pool.save @pool.assign_owner_roles(current_user) flash[:notice] = "Pool added." redirect_to :action => 'show', :id => @pool.id else - render :action => :new + flash.now[:warning] = "Pool creation failed." + render :new and return end end
def edit @pool = Pool.find(params[:id]) require_privilege(Privilege::MODIFY, @pool) + @quota = @pool.quota end
def update @pool = Pool.find(params[:id]) require_privilege(Privilege::MODIFY, @pool) + @quota = @pool.quota + + limit = params[:quota][:maximum_running_instances] if params[:quota] + @pool.quota.set_maximum_running_instances(limit) if @pool.update_attributes(params[:pool]) flash[:notice] = "Pool updated." redirect_to :action => 'show', :id => @pool.id else + flash[:error] = "Pool wasn't updated!" render :action => :edit end end diff --git a/src/app/models/pool.rb b/src/app/models/pool.rb index ebee420..c158403 100644 --- a/src/app/models/pool.rb +++ b/src/app/models/pool.rb @@ -41,7 +41,7 @@ class Pool < ActiveRecord::Base text :name, :as => :code_substring end has_many :instances, :dependent => :destroy - belongs_to :quota + belongs_to :quota, :autosave => true, :dependent => :destroy belongs_to :pool_family
# NOTE: Commented out because images table doesn't have pool_id foreign key?! diff --git a/src/app/views/resources/pools/edit.haml b/src/app/views/resources/pools/edit.haml index 43bb6d0..cc00233 100644 --- a/src/app/views/resources/pools/edit.haml +++ b/src/app/views/resources/pools/edit.haml @@ -1,8 +1,27 @@ -%h3 - Editing pool += error_messages_for :pool +%h1 + Edit pool = @pool.name
- form_for [:resources, @pool] do |form| - = form.text_field :name - = form.error_message_on :name, 'Name ' - = form.submit 'Save' + %fieldset + = form.label :name, "Name:", :class => "grid_2 alpha" + = form.text_field :name + %fieldset + = form.label :pool_family_id, "Pool Family:", :class => "grid_2 alpha" + = form.collection_select :pool_family_id, PoolFamily.all, :id, :name + %fieldset + = label_tag "Quota" + = text_field :quota, :maximum_running_instances, :title => t('provider_accounts.form.quota_instances'), :value => @quota.maximum_running_instances || "unlimited", :id => "quota_instances", :class => "grid_3 omega" + ( + %button.linkbutton.nospace{ :type => 'button', :onclick => "set_unlimited_quota("quota_instances");" }<> + = t('provider_accounts.form.unlimited_quota') + ) + %fieldset.clear + + = form.submit "Save" + +:javascript + function set_unlimited_quota(elem_id) { + $("#" + elem_id)[0].value = "unlimited"; + } diff --git a/src/app/views/resources/pools/new.haml b/src/app/views/resources/pools/new.haml index 032f0ac..dde52c9 100644 --- a/src/app/views/resources/pools/new.haml +++ b/src/app/views/resources/pools/new.haml @@ -1,11 +1,24 @@ -= error_messages_for 'account' += error_messages_for :pool %h1 Create a new Pool - form_for @pool, :url => resources_pools_path do |form| - %h2 Pool %fieldset - %label.grid_2.alpha Name: - = form.text_field :name, :class => "grid_5" - = form.error_message_on :name, 'Name ' - .clear.grid_14.prefix_2.alpha Provide a descriptive name for this pool. + = form.label :name, "Name:", :class => "grid_2 alpha" + = form.text_field :name + %fieldset + = form.label :pool_family_id, "Pool Family:", :class => "grid_2 alpha" + = form.collection_select :pool_family_id, PoolFamily.all, :id, :name + %fieldset + = label_tag "Quota" + = text_field :quota, :maximum_running_instances, :title => t('provider_accounts.form.quota_instances'), :value => @quota.maximum_running_instances || "unlimited", :id => "quota_instances", :class => "grid_3 omega" + ( + %button.linkbutton.nospace{ :type => 'button', :onclick => "set_unlimited_quota("quota_instances");" }<> + = t('provider_accounts.form.unlimited_quota') + ) + %fieldset.clear + + = form.submit "Save"
- = submit_tag "Save", :class => "submit formbutton" +:javascript + function set_unlimited_quota(elem_id) { + $("#" + elem_id)[0].value = "unlimited"; + } diff --git a/src/features/pool.feature b/src/features/pool.feature index 7f1a00b..04652a6 100644 --- a/src/features/pool.feature +++ b/src/features/pool.feature @@ -14,10 +14,14 @@ Feature: Manage Pools Then I should be on the new resources pool page And I should see "Create a new Pool" When I fill in "pool_name" with "mockpool" + And I select "default" from "pool_pool_family_id" + And I fill in "quota_instances" with "unlimited" And I press "Save" Then I should be on the resources pool page And I should see "Pool added" And I should see "mockpool" + And I should see "default" + And I should see "unlimited" And I should have a pool named "mockpool"
@tag @@ -62,6 +66,7 @@ Feature: Manage Pools Then I should be on the new resources pool page And I should see "Create a new Pool" When I fill in "pool_name" with "mockpool" + And I select "default" from "pool_pool_family_id" And I press "Save" Then I should be on the resources pool page And I should see "Pool added" @@ -71,6 +76,7 @@ Feature: Manage Pools Then I should be on the new resources pool page And I should see "Create a new Pool" When I fill in "pool_name" with "foopool" + And I select "default" from "pool_pool_family_id" And I press "Save" Then I should be on the resources pool page And I should see "Pool added" diff --git a/src/features/step_definitions/pool_steps.rb b/src/features/step_definitions/pool_steps.rb index 5fd48c1..f2a813a 100644 --- a/src/features/step_definitions/pool_steps.rb +++ b/src/features/step_definitions/pool_steps.rb @@ -18,7 +18,7 @@ end
Given /^a pool "([^"]*)" exists$/ do |pool_name| pool_family = PoolFamily.find_by_name('default') || Factory(:pool_family) - quota = Quota.first || Factory(:quota) + quota = Factory(:quota) Pool.create!(:name => pool_name, :pool_family => pool_family, :quota => quota) end
diff --git a/src/spec/controllers/pools_controller_spec.rb b/src/spec/controllers/pools_controller_spec.rb index bc3488d..ae84666 100644 --- a/src/spec/controllers/pools_controller_spec.rb +++ b/src/spec/controllers/pools_controller_spec.rb @@ -23,11 +23,13 @@ describe Resources::PoolsController do it "should provider means to create new pool" do UserSession.create(@admin) lambda do - post :create, :pool => { :name => 'foopool' } + post :create, :pool => { + :name => 'foopool', + :pool_family_id => PoolFamily.find_by_name('default').id + } end.should change(Pool, :count).by(1) id = Pool.find(:first, :conditions => ['name = ?', 'foopool']).id response.should redirect_to(resources_pool_path(id)) end
- end