Simon COURTOIS
2010-Sep-03 08:43 UTC
[Ovirt-devel] [PATCH] Adding some control over usages in the network creation/edition form
Signed-off-by: Simon COURTOIS <scourtois at linagora.com> --- src/app/controllers/usages_controller.rb | 13 +++++++ src/app/models/usage.rb | 2 +- src/app/services/usage_service.rb | 23 ++++++++++++ src/app/views/network/_form.rhtml | 56 +++++++++++++++++++++++++++++- src/config/routes.rb | 1 + src/public/stylesheets/components.css | 18 +++++++++ 6 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/app/controllers/usages_controller.rb create mode 100644 src/app/services/usage_service.rb diff --git a/src/app/controllers/usages_controller.rb b/src/app/controllers/usages_controller.rb new file mode 100644 index 0000000..0da324e --- /dev/null +++ b/src/app/controllers/usages_controller.rb @@ -0,0 +1,13 @@ +class UsagesController < ApplicationController + include UsageService + + def create + usage = svc_create(params[:usage]) + render :json => { :success => true, :data => usage } + end + + def remove + removed_ids = svc_destroy_all(params[:ids]) + render :json => { :success => true, :removed => removed_ids } + end +end diff --git a/src/app/models/usage.rb b/src/app/models/usage.rb index 59b0e48..883a888 100644 --- a/src/app/models/usage.rb +++ b/src/app/models/usage.rb @@ -22,6 +22,6 @@ class Usage < ActiveRecord::Base validates_presence_of :label validates_presence_of :usage - validates_uniqueness_of :usage + # validates_uniqueness_of :usage end diff --git a/src/app/services/usage_service.rb b/src/app/services/usage_service.rb new file mode 100644 index 0000000..d044631 --- /dev/null +++ b/src/app/services/usage_service.rb @@ -0,0 +1,23 @@ +module UsageService + include ApplicationService + + def authorize + authorized!(Privilege::MODIFY,HardwarePool.get_default_pool) + end + + def svc_create(usage_hash) + authorize + usage_hash[:id] = Usage.maximum(:id) + 1 + @usage = Usage.new(usage_hash) + @usage.save! + return @usage + end + + def svc_destroy_all(ids) + authorize + # prevent destruction of original usages (1, 2 and 3) + ids -= ['1','2','3'] + Usage.destroy(ids) + return ids + end +end diff --git a/src/app/views/network/_form.rhtml b/src/app/views/network/_form.rhtml index 6c67a0e..d04e450 100644 --- a/src/app/views/network/_form.rhtml +++ b/src/app/views/network/_form.rhtml @@ -18,11 +18,20 @@ <div class="field_title">Usage:</div> -<div class="form_field"> +<div id="network_usages_infos" class="form_field"> <select id="network_usages_ids" name="network[usage_ids][]" multiple="true"> <%= options_from_collection_for_select @usage_types, "id", "label", @network ? @network.usages.collect{ |x| x.id.to_i } : [] %> </select> + <div class="usage_buttons"> + <p> + <%= text_field_tag 'network_usage_label', nil, :class => 'textfield_effect' %> + <button id="add_usage">Add usage</button> + </p> + <p> + <button id="delete_usages">Remove selected usages</button> + </p> + </div> </div> <div id="vlan_options" @@ -43,4 +52,49 @@ $("#network_type").change(function () { $("#vlan_options").hide(); } }).trigger('change'); + +$('#add_usage').click(function() { + var label = $('#network_usage_label').val(); + if (label.length > 0) { + $.post( + "<%= usages_path :format => 'json' %>", + { + "usage[label]": label, + "usage[usage]": 'management' + }, + function(data, status) { + if (data.success) { + var usage = data.data.usage; + $('#network_usages_ids') + .append('<option value="'+usage.id+'">'+usage.label+'</option>'); + } + }, + 'json' + ); + } + return false; +}); + +$('#delete_usages').click(function() { + var selected_ids = $('#network_usages_ids').val(); + + $.post( + "<%= remove_usages_path :format => 'json' %>", + { + '_method': 'delete', + 'ids[]': selected_ids + }, + function(data, status) { + if (data.success) { + for (id in data.removed) { + $('#network_usages_ids') + .find('option:selected[value='+data.removed[id]+']') + .remove(); + } + } + }, + 'json' + ); + return false; +}); </script> diff --git a/src/config/routes.rb b/src/config/routes.rb index 795f022..70fe614 100644 --- a/src/config/routes.rb +++ b/src/config/routes.rb @@ -58,4 +58,5 @@ ActionController::Routing::Routes.draw do |map| hardware_pools.resources :storage_pools, :controller => 'storage' end map.resources :vms, :controller => 'vm' + map.resources :usages, :only => [ :create ], :collection => { :remove => :delete } end diff --git a/src/public/stylesheets/components.css b/src/public/stylesheets/components.css index 70cda97..3cdbf49 100644 --- a/src/public/stylesheets/components.css +++ b/src/public/stylesheets/components.css @@ -394,3 +394,21 @@ #vm_network_config_add:hover { cursor: pointer; } + +#network_usages_infos { + overflow: hidden; + width: 100%; +} + +#network_usages_infos select { + float: left; +} + +#network_usages_infos .usage_buttons { + float: left; + margin-left: 1em; +} + +#network_usages_infos .usage_buttons p { + margin-top: 0; +} -- 1.7.2.1
Arthur Clément
2010-Sep-08 13:14 UTC
[Ovirt-devel] [PATCH] Adding some control over usages in the network creation/edition form
Pushed On vendredi 03 septembre 2010 10:43:11 Simon COURTOIS wrote:> Signed-off-by: Simon COURTOIS <scourtois at linagora.com> > --- > src/app/controllers/usages_controller.rb | 13 +++++++ > src/app/models/usage.rb | 2 +- > src/app/services/usage_service.rb | 23 ++++++++++++ > src/app/views/network/_form.rhtml | 56 > +++++++++++++++++++++++++++++- src/config/routes.rb | > 1 + > src/public/stylesheets/components.css | 18 +++++++++ > 6 files changed, 111 insertions(+), 2 deletions(-) > create mode 100644 src/app/controllers/usages_controller.rb > create mode 100644 src/app/services/usage_service.rb > > diff --git a/src/app/controllers/usages_controller.rb > b/src/app/controllers/usages_controller.rb new file mode 100644 > index 0000000..0da324e > --- /dev/null > +++ b/src/app/controllers/usages_controller.rb > @@ -0,0 +1,13 @@ > +class UsagesController < ApplicationController > + include UsageService > + > + def create > + usage = svc_create(params[:usage]) > + render :json => { :success => true, :data => usage } > + end > + > + def remove > + removed_ids = svc_destroy_all(params[:ids]) > + render :json => { :success => true, :removed => removed_ids } > + end > +end > diff --git a/src/app/models/usage.rb b/src/app/models/usage.rb > index 59b0e48..883a888 100644 > --- a/src/app/models/usage.rb > +++ b/src/app/models/usage.rb > @@ -22,6 +22,6 @@ class Usage < ActiveRecord::Base > validates_presence_of :label > validates_presence_of :usage > > - validates_uniqueness_of :usage > + # validates_uniqueness_of :usage > > end > diff --git a/src/app/services/usage_service.rb > b/src/app/services/usage_service.rb new file mode 100644 > index 0000000..d044631 > --- /dev/null > +++ b/src/app/services/usage_service.rb > @@ -0,0 +1,23 @@ > +module UsageService > + include ApplicationService > + > + def authorize > + authorized!(Privilege::MODIFY,HardwarePool.get_default_pool) > + end > + > + def svc_create(usage_hash) > + authorize > + usage_hash[:id] = Usage.maximum(:id) + 1 > + @usage = Usage.new(usage_hash) > + @usage.save! > + return @usage > + end > + > + def svc_destroy_all(ids) > + authorize > + # prevent destruction of original usages (1, 2 and 3) > + ids -= ['1','2','3'] > + Usage.destroy(ids) > + return ids > + end > +end > diff --git a/src/app/views/network/_form.rhtml > b/src/app/views/network/_form.rhtml index 6c67a0e..d04e450 100644 > --- a/src/app/views/network/_form.rhtml > +++ b/src/app/views/network/_form.rhtml > @@ -18,11 +18,20 @@ > > > <div class="field_title">Usage:</div> > -<div class="form_field"> > +<div id="network_usages_infos" class="form_field"> > <select id="network_usages_ids" name="network[usage_ids][]" > multiple="true"> <%= options_from_collection_for_select @usage_types, > "id", "label", @network ? @network.usages.collect{ |x| x.id.to_i } : [] %> > </select> > + <div class="usage_buttons"> > + <p> > + <%= text_field_tag 'network_usage_label', nil, :class => > 'textfield_effect' %> + <button id="add_usage">Add usage</button> > + </p> > + <p> > + <button id="delete_usages">Remove selected usages</button> > + </p> > + </div> > </div> > > <div id="vlan_options" > @@ -43,4 +52,49 @@ $("#network_type").change(function () { > $("#vlan_options").hide(); > } > }).trigger('change'); > + > +$('#add_usage').click(function() { > + var label = $('#network_usage_label').val(); > + if (label.length > 0) { > + $.post( > + "<%= usages_path :format => 'json' %>", > + { > + "usage[label]": label, > + "usage[usage]": 'management' > + }, > + function(data, status) { > + if (data.success) { > + var usage = data.data.usage; > + $('#network_usages_ids') > + .append('<option > value="'+usage.id+'">'+usage.label+'</option>'); + } > + }, > + 'json' > + ); > + } > + return false; > +}); > + > +$('#delete_usages').click(function() { > + var selected_ids = $('#network_usages_ids').val(); > + > + $.post( > + "<%= remove_usages_path :format => 'json' %>", > + { > + '_method': 'delete', > + 'ids[]': selected_ids > + }, > + function(data, status) { > + if (data.success) { > + for (id in data.removed) { > + $('#network_usages_ids') > + .find('option:selected[value='+data.removed[id]+']') > + .remove(); > + } > + } > + }, > + 'json' > + ); > + return false; > +}); > </script> > diff --git a/src/config/routes.rb b/src/config/routes.rb > index 795f022..70fe614 100644 > --- a/src/config/routes.rb > +++ b/src/config/routes.rb > @@ -58,4 +58,5 @@ ActionController::Routing::Routes.draw do |map| > hardware_pools.resources :storage_pools, :controller => 'storage' > end > map.resources :vms, :controller => 'vm' > + map.resources :usages, :only => [ :create ], :collection => { :remove => > :delete } end > diff --git a/src/public/stylesheets/components.css > b/src/public/stylesheets/components.css index 70cda97..3cdbf49 100644 > --- a/src/public/stylesheets/components.css > +++ b/src/public/stylesheets/components.css > @@ -394,3 +394,21 @@ > #vm_network_config_add:hover { > cursor: pointer; > } > + > +#network_usages_infos { > + overflow: hidden; > + width: 100%; > +} > + > +#network_usages_infos select { > + float: left; > +} > + > +#network_usages_infos .usage_buttons { > + float: left; > + margin-left: 1em; > +} > + > +#network_usages_infos .usage_buttons p { > + margin-top: 0; > +}-- Arthur CLEMENT Linagora Paris