From: Imre Farkas <[email protected]>

---
 .../provider_priority_groups_controller.rb         | 112 +++++++++++++++++++++
 .../views/provider_priority_groups/_form.html.haml |  71 +++++++++++++
 .../views/provider_priority_groups/edit.html.haml  |  15 +++
 .../views/provider_priority_groups/index.html.haml |  50 +++++++++
 .../views/provider_priority_groups/new.html.haml   |  15 +++
 5 files changed, 263 insertions(+)
 create mode 100644 src/app/controllers/provider_priority_groups_controller.rb
 create mode 100644 src/app/views/provider_priority_groups/_form.html.haml
 create mode 100644 src/app/views/provider_priority_groups/edit.html.haml
 create mode 100644 src/app/views/provider_priority_groups/index.html.haml
 create mode 100644 src/app/views/provider_priority_groups/new.html.haml

diff --git a/src/app/controllers/provider_priority_groups_controller.rb 
b/src/app/controllers/provider_priority_groups_controller.rb
new file mode 100644
index 0000000..76785b0
--- /dev/null
+++ b/src/app/controllers/provider_priority_groups_controller.rb
@@ -0,0 +1,112 @@
+#
+#   Copyright 2012 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+class ProviderPriorityGroupsController < ApplicationController
+
+  before_filter :require_user
+  before_filter :load_pool
+  before_filter :load_providers, :only => [:new, :create, :edit, :update]
+  before_filter :require_privileged_user_for_modify, :except => :index
+
+  def index
+    require_privilege(Privilege::VIEW, @pool)
+
+    @strategy = ProviderSelection::Base.find_strategy_by_name(params[:name])
+    @priority_groups = @pool.provider_priority_groups
+  end
+
+  def new
+    @priority_group = ProviderPriorityGroup.new
+  end
+
+  def create
+    @priority_group = 
ProviderPriorityGroup.new(params[:provider_priority_group])
+    @priority_group.pool = @pool
+
+    unless @priority_group.save
+      render :new
+      return
+    end
+
+    if params[:provider_ids].present?
+      selected_providers =
+        Provider.list_for_user(current_session, current_user, Privilege::USE).
+          find(params[:provider_ids])
+      @priority_group.providers = selected_providers
+    end
+
+    if params[:provider_account_ids].present?
+      selected_provider_accounts =
+        ProviderAccount.list_for_user(current_session, current_user, 
Privilege::USE).
+          find(params[:provider_account_ids])
+      @priority_group.add_provider_accounts(selected_provider_accounts)
+    end
+
+    redirect_to 
pool_provider_selection_provider_priority_groups_path(@priority_group.pool),
+                :notice => t('provider_priority_groups.flash.created')
+  end
+
+  def edit
+    @priority_group = ProviderPriorityGroup.find(params[:id])
+  end
+
+  def update
+    @priority_group = ProviderPriorityGroup.find(params[:id])
+
+    unless @priority_group.update_attributes!(params[:provider_priority_group])
+      render :edit
+      return
+    end
+
+    if params[:provider_ids].present?
+      selected_providers =
+        Provider.list_for_user(current_session, current_user, Privilege::USE).
+          find(params[:provider_ids])
+      @priority_group.providers = selected_providers
+    end
+
+    @priority_group.provider_accounts.clear
+    if params[:provider_account_ids].present?
+      selected_provider_accounts =
+        ProviderAccount.list_for_user(current_session, current_user, 
Privilege::USE).
+          find(params[:provider_account_ids])
+      @priority_group.add_provider_accounts(selected_provider_accounts)
+    end
+
+    redirect_to 
pool_provider_selection_provider_priority_groups_path(@priority_group.pool),
+                :notice => t('provider_priority_groups.flash.updated')
+  end
+
+  def destroy
+    @pool.provider_priority_groups.find(params[:id]).destroy
+    redirect_to :back, :notice => t('provider_priority_groups.flash.deleted')
+  end
+
+  private
+
+  def load_pool
+    @pool = Pool.find(params[:pool_id])
+  end
+
+  def load_providers
+    @providers = Provider.list_for_user(current_session, current_user, 
Privilege::USE)
+  end
+
+  def require_privileged_user_for_modify
+    require_privilege(Privilege::MODIFY, @pool)
+  end
+
+end
diff --git a/src/app/views/provider_priority_groups/_form.html.haml 
b/src/app/views/provider_priority_groups/_form.html.haml
new file mode 100644
index 0000000..e1b2f56
--- /dev/null
+++ b/src/app/views/provider_priority_groups/_form.html.haml
@@ -0,0 +1,71 @@
+.priority-group
+  - if @priority_group.errors.any?
+    = render 'layouts/error_messages', :object => @priority_group
+
+  %fieldset
+    %legend= t('provider_priority_groups.form.basic_data')
+
+    .field
+      = form.label :name
+      .input
+        = form.text_field :name
+    .field
+      = form.label :score
+      .input
+        = form.text_field :score
+
+  %fieldset
+    %legend= t('provider_priority_groups.form.providers_and_provider_accounts')
+
+    - if @providers.any?
+      %ul.priority-groups
+        - @providers.each do |provider|
+          %li
+            .summary
+              .info
+                %h3.name
+                  = check_box_tag("provider_ids[]", provider.id, 
@priority_group.include?(provider), :class => 'provider-checkbox')
+                  = provider.name
+            .details
+              %table
+                %tr
+                  %th
+                  %th
+                    %strong= 
t('provider_priority_groups.index.provider_account_name')
+                  %th= 
t('provider_priority_groups.index.provider_account_username')
+                  %th= ProviderAccount.human_attribute_name(:priority)
+                  %th= 
t('provider_priority_groups.index.quota_percentage_used')
+                  %th= 
t('provider_priority_groups.index.quota_maximum_running_instances')
+
+                - provider.provider_accounts.each do |account|
+                  %tr
+                    %td= check_box_tag("provider_account_ids[]", account.id, 
@priority_group.include?(account), :class => 'provider-account-checkbox')
+                    %td= link_to account.name, 
provider_provider_account_path(account.provider, account)
+                    %td= account.credentials_hash['username']
+                    %td= account.priority
+                    %td{:class => 'center'}= number_to_percentage 
account.quota.percentage_used, :precision => 0
+                    %td{:class => 'center'}= 
account.quota.maximum_running_instances or 
t('provider_accounts.properties.unlimited')
+    - else
+      #no-provider-account.align-center
+        %strong= t("provider_priority_groups.index.no_priority_group")
+
+:javascript
+  $(document).ready(function() {
+    $(".priority-groups li h3.name").live("click", function(event) {
+      var $providerCheckbox = $(this).find('input.provider-checkbox');
+      var providerAccountCheckboxes = 
$(this).parents('li').find('input.provider-account-checkbox')
+
+      $.each(providerAccountCheckboxes, function(index, 
providerAccountCheckbox) {
+        $(providerAccountCheckbox).prop('checked', 
$providerCheckbox.prop('checked'));
+      });
+    });
+
+    $(".priority-groups li .provider-account-checkbox").live("click", 
function(event) {
+      var $providerAccountCheckbox = $(this);
+
+      if ($providerAccountCheckbox.prop('checked') == false) {
+        var $providerCheckbox = 
$(this).parents('li').find('input.provider-checkbox')
+        $providerCheckbox.prop('checked', false);
+      }
+    });
+  });
diff --git a/src/app/views/provider_priority_groups/edit.html.haml 
b/src/app/views/provider_priority_groups/edit.html.haml
new file mode 100644
index 0000000..30c1bb2
--- /dev/null
+++ b/src/app/views/provider_priority_groups/edit.html.haml
@@ -0,0 +1,15 @@
+= render :partial => 'layouts/admin_nav'
+
+%header.page-header
+  .obj_actions
+    .return_to
+      = t(:return_to)
+      = link_to t('provider_priority_groups.edit.back', :pool => @pool.name), 
pool_provider_selection_provider_priority_groups_path(@pool)
+  %h1.no-icon= t('provider_priority_groups.edit.title')
+
+%section.content-section
+  .content#priority-group-form
+    = form_for @priority_group, :url => 
pool_provider_selection_provider_priority_group_path(@pool) do |form|
+      = render :partial => 'form', :locals => { :form => form }
+      %fieldset.options
+        = form.submit(t('provider_priority_groups.edit.save'), :class => 
"submit button pill")
\ No newline at end of file
diff --git a/src/app/views/provider_priority_groups/index.html.haml 
b/src/app/views/provider_priority_groups/index.html.haml
new file mode 100644
index 0000000..e742820
--- /dev/null
+++ b/src/app/views/provider_priority_groups/index.html.haml
@@ -0,0 +1,50 @@
+= render :partial => 'layouts/admin_nav'
+
+%header.page-header
+  .obj_actions
+    .return_to
+      = t(:return_to)
+      = link_to t('provider_selection.edit_stategy.strategies', :pool => 
@pool.name), pool_provider_selection_path(@pool)
+  %h1.no-icon= t('provider_selection.show.provider_selection')
+
+%section.content-section
+  %header
+    .section-controls
+      = link_to(t('provider_priority_groups.index.add_new'), 
new_pool_provider_selection_provider_priority_group_path(@pool), :class => 
'button pill')
+    %h2= t('provider_selection.show.edit_strategy', :name => 'Stict Order')
+  .content
+    - if @priority_groups.any?
+      %ul.priority-groups
+        - @priority_groups.each do |priority_group|
+          %li
+            .summary
+              .info
+                %h2= priority_group.name
+                %p!= "#{ProviderPriorityGroup.human_attribute_name(:score)}: 
#{priority_group.score}"
+              .controls
+                = link_to(t('edit'), 
edit_pool_provider_selection_provider_priority_group_path(priority_group.pool, 
priority_group), :class => "button pill")
+                = link_to(t('delete'), 
pool_provider_selection_provider_priority_group_path(priority_group.pool, 
priority_group), :method => :delete, :confirm => 
t('provider_priority_groups.index.confirm_delete'), :class => "button pill 
danger")
+            .details
+              %table
+                %tr
+                  %th
+                    %strong= 
t('provider_priority_groups.index.provider_account_name')
+                  %th= 
t('provider_priority_groups.index.provider_account_username')
+                  %th= t('provider_priority_groups.index.provider_name')
+                  %th= t('provider_priority_groups.index.provider_type')
+                  %th= ProviderAccount.human_attribute_name(:priority)
+                  %th= 
t('provider_priority_groups.index.quota_percentage_used')
+                  %th= 
t('provider_priority_groups.index.quota_maximum_running_instances')
+
+                - priority_group.all_provider_accounts.each do |account|
+                  %tr
+                    %td= link_to account.name, 
provider_provider_account_path(account.provider, account)
+                    %td= account.credentials_hash['username']
+                    %td= account.provider.name
+                    %td= account.provider.provider_type.name
+                    %td= account.priority
+                    %td{:class => 'center'}= number_to_percentage 
account.quota.percentage_used, :precision => 0
+                    %td{:class => 'center'}= 
account.quota.maximum_running_instances or 
t('provider_accounts.properties.unlimited')
+    - else
+      #no-provider-account.align-center
+        %strong= t("provider_priority_groups.index.no_priority_group")
\ No newline at end of file
diff --git a/src/app/views/provider_priority_groups/new.html.haml 
b/src/app/views/provider_priority_groups/new.html.haml
new file mode 100644
index 0000000..0b5e26c
--- /dev/null
+++ b/src/app/views/provider_priority_groups/new.html.haml
@@ -0,0 +1,15 @@
+= render :partial => 'layouts/admin_nav'
+
+%header.page-header
+  .obj_actions
+    .return_to
+      = t(:return_to)
+      = link_to t('provider_priority_groups.new.back', :pool => @pool.name), 
pool_provider_selection_provider_priority_groups_path(@pool)
+  %h1.no-icon= t('provider_priority_groups.new.title')
+
+%section.content-section
+  .content#priority-group-form
+    = form_for @priority_group, :url => 
pool_provider_selection_provider_priority_groups_path(@pool) do |form|
+      = render :partial => 'form', :locals => { :form => form }
+      %fieldset.options
+        = form.submit(t('provider_priority_groups.new.create'), :class => 
'submit button pill')
\ No newline at end of file
-- 
1.7.11.2

Reply via email to