Project

General

Profile

Feature #179 ยป feat179.patch

Bash Shell, 05/02/2010 07:06 AM

View differences:

app/controllers/hosts_controller.rb
end
end
def edit_action
if session[:selected].nil?
flash[:foreman_error] = 'No Hosts selected'
redirect_to(hosts_path)
else
@hosts = Host.find(session[:selected].keys)
end
end
def edit
@host = Host.find(params[:id])
@environment = @host.environment
......
end
end
def edit_multiple
if session[:selected].nil?
flash[:foreman_error] = 'No Hosts selected'
redirect_to(hosts_path)
else
@hosts = Host.find(session[:selected].keys)
end
end
def update_multiple
if params[:reset] == "true"
reset_session
@search = Host.search(params[:search])
@hosts = @search.all.paginate(:page => params[:page])
flash[:foreman_notice] = 'Session cleared.'
redirect_to(hosts_path) and return
end
@hosts_without_params ||= {}
@hosts_to_edit = Host.find(session[:selected].keys)
@hosts_to_edit.each do |host_to_edit|
myparams = []
host_to_edit.host_parameters.each do |hp|
myparams << hp.name.to_str
unless params[:name][hp.name].chomp.empty?
hp.value = params[:name][hp.name]
host_to_edit.save(perform_validation = false)
end
end
params[:name].each do |pname,pvalue|
if !myparams.include?(pname) && !pvalue.chomp.empty?
@hosts_without_params[host_to_edit.name] ||= []
@hosts_without_params[host_to_edit.name] << pname
end
end
end
if @hosts_without_params.length !=0
reset_session
render :text => "\#These hosts did not have the selected parameters and were not updated: <br></br>" + @hosts_without_params.to_yaml.gsub("\n","<br>")
else
reset_session
flash[:foreman_notice] = 'Updated hosts!'
redirect_to(hosts_path)
end
end
def add_parameter
if session[:selected].nil?
flash[:foreman_error] = 'No Hosts selected'
redirect_to(hosts_path)
else
@hosts = Host.find(session[:selected].keys)
end
end
def select_hostgroup
if session[:selected].nil?
flash[:foreman_error] = 'No Hosts selected'
redirect_to(hosts_path)
else
@hosts = Host.find(session[:selected].keys, :order => "hostgroup_id ASC")
end
end
def update_hostgroup
if params["hostgroup"]["hostgroup_id_equals"].empty?
flash[:foreman_error] = 'No Hostgroup selected!'
redirect_to(select_hostgroup_hosts_path) and return
end
@hosts_to_edit = Host.find(:all, :conditions => {:id => session[:selected].keys})
@hosts_to_edit.each do |host_to_edit|
hg = Hostgroup.find(params["hostgroup"]["hostgroup_id_equals"])
!hg.nil? && host_to_edit.hostgroup=hg
host_to_edit.save(perform_validation = false)
end
reset_session
flash[:foreman_notice] = 'Updated hosts: Changed Hostgroup'
redirect_to(hosts_path)
end
def destroy
@host = Host.find(params[:id])
if @host.destroy
......
assign_parameter "operatingsystem"
end
def save_checkbox
session[:selected] ||= {}
params[:is_checked] == "true" && session[:selected][params[:box]] = params[:box]
params[:is_checked] == "false" && session[:selected][params[:box]] = nil
render :nothing => true
end
# list AJAX methods
def fact_selected
@fact_name_id = params[:search_fact_values_fact_name_id_eq].to_i
......
return head(:not_found)
end
end
end
app/views/hosts/_minilist.html.erb
<%= javascript_include_tag "host_checkbox" %>
<div id="hostlist">
<%= link_to_function "Search", toggle_div(:search) %>
......
</div>
<% if hosts.size > 0 -%>
<table class="list">
<caption> <%= header ||= "" -%> </caption>
<tr>
<th><%= order @search, :by => :name %></th>
<th>Operating system</th>
<th>Environment</th>
<th>Model</th>
<th>Host Group</th>
<th><%= order @search, :by => :last_report %></th>
<th>Edit</th>
<th>Delete</th>
</tr>
<% hosts.each do |host| -%>
<tr class="<%= cycle("even", "odd") -%>">
<td><%=name_column(host) %></td>
<td><%=h host.try(:os) %></td>
<td><%=h host.try(:environment) %></td>
<td><%=h host.try(:model) %></td>
<td><%=h host.try(:hostgroup) %></td>
<td><%=last_report_column(host) %></td>
<td><%= link_to 'Edit', edit_host_url(host) %></td>
<td><%= link_to "Destroy", host, :confirm => 'Are you sure?', :method => :delete %></td>
<br><input type='button' value='Toggle Check All' onClick='toggleCheck()' />
<% form_tag(edit_action_hosts_path,:method => "get", :id => "host_select_form") do %>
<%= submit_tag "Edit Checked Hosts" %>
<table class="list">
<caption> <%= header ||= "" -%> </caption>
<tr>
<th></th>
<th><%= order @search, :by => :name %></th>
<th>Operating system</th>
<th>Environment</th>
<th>Model</th>
<th>Host Group</th>
<th><%= order @search, :by => :last_report %></th>
<th>Edit</th>
<th>Delete</th>
</tr>
<% hosts.each do |host| -%>
<tr class="<%= cycle("even", "odd") -%>">
<td><%= check_box_tag "host_ids[]", host.id, false, :class => 'host_select_boxes', :onClick => 'insertHostVal(this)' %></td>
<td><%=name_column(host) %></td>
<td><%=h host.try(:os) %></td>
<td><%=h host.try(:environment) %></td>
<td><%=h host.try(:model) %></td>
<td><%=h host.try(:hostgroup) %></td>
<td><%=last_report_column(host) %></td>
<td><%= link_to 'Edit', edit_host_url(host) %></td>
<td><%= link_to "Destroy", host, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end -%>
</tr>
</table>
</table>
<%= submit_tag "Edit Checked Hosts" %>
<% end %>
<% else -%>
<p>No Hosts found</p>
<% end -%>
app/views/hosts/add_parameter.html.erb
<h3>Add/Remove Parameters Coming soon... </h3>
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
<h5>Please check the following list of hosts whose parameters will be changed:</h4>
<br>
<ul>
<% for host in @hosts %>
<li>
<%= hidden_field_tag "host_ids[]", host.id %>
<%= host.name %>
</li>
<% end %>
</ul>
app/views/hosts/edit_action.html.erb
<h2>Choose the action you want to apply to the selected Hosts:</h2>
<%= button_to 'Change Group', :controller => 'hosts', :action => 'select_hostgroup' %>
<br>
<%= button_to 'Edit Parameters', :controller => 'hosts', :action => 'edit_multiple' %>
<br>
<%= button_to 'Add/Remove Parameters', :controller => 'hosts', :action => 'add_parameter' %>
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
<h5>Please check the following list of hosts which will be edited:</h4>
<br>
<ul>
<% for host in @hosts %>
<li>
<%= hidden_field_tag "host_ids[]", host.id %>
<%=h host.name %>
</li>
<% end %>
</ul>
app/views/hosts/edit_multiple.html.erb
<% form_tag update_multiple_hosts_path, :method => :post do %>
<% for param in Parameter.find(:all, :select => "DISTINCT name") %>
<div>
<%= text_field :name, param.name, :size=>'150' %>
<%= param.name %>
</div>
<% end %>
<p><%= submit_tag "Edit these Parameter(s)" %></p>
<% end %>
<h4>Please check the following list of hosts whose parameters will be changed:</h4>
<h5>Click this button to clear all selected hosts and return to Hosts list:</h5>
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
<ul>
<% for host in @hosts %>
<li>
<%= hidden_field_tag "host_ids[]", host.id %>
<%= host.name %>
</li>
<% end %>
</ul>
app/views/hosts/select_hostgroup.html.erb
<% form_for :hostgroup, :url => { :action => "update_hostgroup" },:html => { :method => :post } do |f| %>
<%= f.label :hostgroup, "Select Hostgroup:<br>" %>
<%= f.collection_select :hostgroup_id_equals, Hostgroup.all, :id, :name, :include_blank => true %>
<p><%= f.submit "Change Hostgroup!" %></p>
<% end %>
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
<h5>Please check the following list of hosts whose Hostgroup will be changed:</h4>
<ul>
<% for host in @hosts %>
<li>
<%= hidden_field_tag "host_ids[]", host.id %>
<%= host.try(:hostgroup) %>
<%= host.try(:name) %>
</li>
<% end %>
</ul>
config/routes.rb
:member => {:report => :get, :reports => :get, :facts => :get,
:environment_selected => :post, :architecture_selected => :post, :os_selected => :post,
:storeconfig_klasses => :get, :externalNodes => :get, :setBuild => :get, :puppetrun => :get},
:collection => { :show_search => :get}
:collection => { :show_search => :get, :edit_action => :get, :edit_multiple => :get,
:update_multiple => :post, :save_checkbox => :get, :add_parameter => :get, :select_hostgroup => :get,
:update_hostgroup => :post}
map.dashboard '/dashboard', :controller => 'dashboard'
map.audit '/audit', :controller => 'audit'
map.statistics '/statistics', :controller => 'statistics'
public/javascripts/host_checkbox.js
function toggleCheck() {
$$('#host_select_form input.host_select_boxes').each(function(box){
box.checked=!box.checked;
//if (box.checked == true ) { box.checked = false; }
//if (box.checked == false) { box.checked = true; }
insertHostVal(box);
});
return false;
}
function insertHostVal(cbox) {
//alert(cbox.value)
//if (cbox.checked){
// alert(cbox.value)
//hosts
var request = new Ajax.Request('hosts/save_checkbox', {
method: 'get',
parameters: { box: cbox.value, is_checked: cbox.checked },
onSuccess: function(res){
return false;
},
onFailure: function(res){
alert("Something failed! Select the checkbox again.")
return false;
}
});
}
function clearSession() {
var request = new Ajax.Request('update_multiple', {
method: 'post',
parameters: { resetSession: "true" },
onSuccess: function(res){
return false;
},
onFailure: function(res){
alert("Something failed! Click the button again.")
return false;
}
});
}
    (1-1/1)