Project

General

Profile

feat179.patch

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

View differences:

app/controllers/hosts_controller.rb
47 47
    end
48 48
  end
49 49

  
50
  def edit_action
51
    if session[:selected].nil?
52
      flash[:foreman_error] = 'No Hosts selected'
53
      redirect_to(hosts_path)
54
    else
55
      @hosts = Host.find(session[:selected].keys)
56
    end
57
  end
58

  
50 59
  def edit
51 60
    @host = Host.find(params[:id])
52 61
    @environment = @host.environment
......
64 73
    end
65 74
  end
66 75

  
76
  def edit_multiple
77
    if session[:selected].nil?
78
      flash[:foreman_error] = 'No Hosts selected'
79
      redirect_to(hosts_path)
80
    else
81
      @hosts = Host.find(session[:selected].keys)
82
    end
83
  end
84

  
85
  def update_multiple
86
    if params[:reset] == "true"
87
      reset_session
88
      @search = Host.search(params[:search])
89
      @hosts = @search.all.paginate(:page => params[:page])
90
      flash[:foreman_notice] = 'Session cleared.'
91
      redirect_to(hosts_path) and return
92
    end
93
    @hosts_without_params ||= {}
94
    @hosts_to_edit = Host.find(session[:selected].keys)
95
    @hosts_to_edit.each do |host_to_edit|
96
      myparams = []
97
      host_to_edit.host_parameters.each do |hp|
98
        myparams << hp.name.to_str
99
        unless params[:name][hp.name].chomp.empty?
100
           hp.value = params[:name][hp.name]
101
           host_to_edit.save(perform_validation = false)
102
        end
103
      end
104
      params[:name].each do |pname,pvalue|
105
        if !myparams.include?(pname) && !pvalue.chomp.empty?
106
          @hosts_without_params[host_to_edit.name] ||= []
107
          @hosts_without_params[host_to_edit.name] << pname
108
        end
109
      end
110
    end
111
    if @hosts_without_params.length !=0
112
        reset_session
113
        render :text => "\#These hosts did not have the selected parameters and were not updated: <br></br>" + @hosts_without_params.to_yaml.gsub("\n","<br>")
114
    else
115
    reset_session
116
    flash[:foreman_notice] = 'Updated hosts!'
117
    redirect_to(hosts_path)
118
    end
119
  end
120

  
121
  def add_parameter
122
    if session[:selected].nil?
123
      flash[:foreman_error] = 'No Hosts selected'
124
      redirect_to(hosts_path)
125
    else
126
      @hosts = Host.find(session[:selected].keys)
127
    end
128
  end
129

  
130
  def select_hostgroup
131
    if session[:selected].nil?
132
      flash[:foreman_error] = 'No Hosts selected'
133
      redirect_to(hosts_path)
134
    else
135
      @hosts = Host.find(session[:selected].keys, :order => "hostgroup_id ASC")
136
    end
137
  end
138

  
139
  def update_hostgroup
140
    if params["hostgroup"]["hostgroup_id_equals"].empty?
141
      flash[:foreman_error] = 'No Hostgroup selected!'
142
      redirect_to(select_hostgroup_hosts_path) and return
143
    end
144
    @hosts_to_edit = Host.find(:all, :conditions => {:id => session[:selected].keys})
145
    @hosts_to_edit.each do |host_to_edit|
146
      hg = Hostgroup.find(params["hostgroup"]["hostgroup_id_equals"])
147
      !hg.nil? && host_to_edit.hostgroup=hg
148
      host_to_edit.save(perform_validation = false)
149
    end
150
    reset_session
151
    flash[:foreman_notice] = 'Updated hosts: Changed Hostgroup'
152
    redirect_to(hosts_path)
153
  end
154

  
67 155
  def destroy
68 156
    @host = Host.find(params[:id])
69 157
    if @host.destroy
......
92 180
    assign_parameter "operatingsystem"
93 181
  end
94 182

  
183
  def save_checkbox
184
    session[:selected] ||= {}
185
    params[:is_checked] == "true" && session[:selected][params[:box]] = params[:box]
186
    params[:is_checked] == "false" && session[:selected][params[:box]] = nil
187
    render :nothing => true
188
  end
189

  
95 190
 # list AJAX methods
96 191
  def fact_selected
97 192
    @fact_name_id = params[:search_fact_values_fact_name_id_eq].to_i
......
232 327
      return head(:not_found)
233 328
    end
234 329
  end
235

  
236 330
end
app/views/hosts/_minilist.html.erb
1
<%= javascript_include_tag "host_checkbox" %>
1 2
<div id="hostlist">
2 3
<%= link_to_function "Search", toggle_div(:search) %>
3 4

  
......
16 17
</div>
17 18

  
18 19
  <% if hosts.size > 0 -%>
19
    <table class="list">
20
      <caption> <%= header ||= "" -%> </caption>
21
      <tr>
22
        <th><%= order @search, :by => :name %></th>
23
        <th>Operating system</th>
24
        <th>Environment</th>
25
        <th>Model</th>
26
        <th>Host Group</th>
27
        <th><%= order @search, :by => :last_report %></th>
28
        <th>Edit</th>
29
        <th>Delete</th>
30
      </tr>
31
      <% hosts.each do |host| -%>
32
        <tr class="<%= cycle("even", "odd") -%>">
33
          <td><%=name_column(host) %></td>
34
          <td><%=h host.try(:os) %></td>
35
          <td><%=h host.try(:environment) %></td>
36
          <td><%=h host.try(:model) %></td>
37
          <td><%=h host.try(:hostgroup) %></td>
38
          <td><%=last_report_column(host) %></td>
39
          <td><%= link_to  'Edit', edit_host_url(host)  %></td>
40
          <td><%= link_to "Destroy", host, :confirm => 'Are you sure?', :method => :delete %></td>
20
    <br><input type='button' value='Toggle Check All' onClick='toggleCheck()' />
21
    <% form_tag(edit_action_hosts_path,:method => "get", :id => "host_select_form") do %>
22
    <%= submit_tag "Edit Checked Hosts" %>
23
      <table class="list">
24
        <caption> <%= header ||= "" -%> </caption>
25
        <tr>
26
          <th></th>
27
          <th><%= order @search, :by => :name %></th>
28
          <th>Operating system</th>
29
          <th>Environment</th>
30
          <th>Model</th>
31
          <th>Host Group</th>
32
          <th><%= order @search, :by => :last_report %></th>
33
          <th>Edit</th>
34
          <th>Delete</th>
35
        </tr>
36
        <% hosts.each do |host| -%>
37
          <tr class="<%= cycle("even", "odd") -%>">
38
            <td><%= check_box_tag "host_ids[]", host.id, false, :class => 'host_select_boxes', :onClick => 'insertHostVal(this)' %></td>
39
            <td><%=name_column(host) %></td>
40
            <td><%=h host.try(:os) %></td>
41
            <td><%=h host.try(:environment) %></td>
42
            <td><%=h host.try(:model) %></td>
43
            <td><%=h host.try(:hostgroup) %></td>
44
            <td><%=last_report_column(host) %></td>
45
            <td><%= link_to  'Edit', edit_host_url(host)  %></td>
46
            <td><%= link_to "Destroy", host, :confirm => 'Are you sure?', :method => :delete %></td>
47
          </tr>
41 48
        <% end -%>
42
      </tr>
43
    </table>
49
      </table>
50
    <%= submit_tag "Edit Checked Hosts" %>
51
    <% end %>
44 52
  <% else -%>
45 53
    <p>No Hosts found</p>
46 54
  <% end -%>
app/views/hosts/add_parameter.html.erb
1
<h3>Add/Remove Parameters Coming soon... </h3>
2

  
3
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
4
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
5
<h5>Please check the following list of hosts whose parameters will be changed:</h4>
6
<br>
7
<ul>
8
  <% for host in @hosts %>
9
    <li>
10
      <%= hidden_field_tag "host_ids[]", host.id %>
11
      <%= host.name %>
12
    </li>
13
  <% end %>
14
</ul>
app/views/hosts/edit_action.html.erb
1
<h2>Choose the action you want to apply to the selected Hosts:</h2>
2

  
3
<%= button_to 'Change Group', :controller => 'hosts', :action => 'select_hostgroup' %>
4
<br>
5
<%= button_to 'Edit Parameters', :controller => 'hosts', :action => 'edit_multiple' %>
6
<br>
7
<%= button_to 'Add/Remove Parameters', :controller => 'hosts', :action => 'add_parameter' %>
8

  
9
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
10
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
11
<h5>Please check the following list of hosts which will be edited:</h4>
12
<br>
13
<ul>
14
  <% for host in @hosts %>
15
    <li>
16
      <%= hidden_field_tag "host_ids[]", host.id %>
17
      <%=h host.name %>
18
    </li>
19
  <% end %>
20
</ul>
app/views/hosts/edit_multiple.html.erb
1
<% form_tag update_multiple_hosts_path, :method => :post do %>
2
  <% for param in Parameter.find(:all, :select => "DISTINCT name") %>
3
    <div>
4
      <%= text_field :name, param.name, :size=>'150' %>
5
      <%= param.name %>
6
    </div>
7
  <% end %>
8
 <p><%= submit_tag "Edit these Parameter(s)" %></p>
9
<% end %>
10

  
11
<h4>Please check the following list of hosts whose parameters will be changed:</h4>
12
<h5>Click this button to clear all selected hosts and return to Hosts list:</h5>
13
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
14
<ul>
15
  <% for host in @hosts %>
16
    <li>
17
      <%= hidden_field_tag "host_ids[]", host.id %>
18
      <%= host.name %>
19
    </li>
20
  <% end %>
21
</ul>
app/views/hosts/select_hostgroup.html.erb
1
<% form_for :hostgroup, :url => { :action => "update_hostgroup" },:html => { :method => :post } do |f| %>
2
  <%= f.label :hostgroup, "Select Hostgroup:<br>" %>
3
  <%= f.collection_select :hostgroup_id_equals, Hostgroup.all, :id, :name, :include_blank => true %>
4
  <p><%= f.submit "Change Hostgroup!" %></p>
5
<% end %>
6

  
7
<h4>Click this button to clear all selected hosts and return to Hosts list:</h5>
8
<%= button_to 'Clear Selected Hosts', :controller => 'hosts', :action => 'update_multiple', :reset => "true" %>
9
<h5>Please check the following list of hosts whose Hostgroup will be changed:</h4>
10
<ul>
11
  <% for host in @hosts %>
12
    <li>
13
      <%= hidden_field_tag "host_ids[]", host.id %>
14
      <%= host.try(:hostgroup) %>
15
      <%= host.try(:name) %>
16
    </li>
17
  <% end %>
18
</ul>
config/routes.rb
8 8
                :member => {:report => :get, :reports => :get, :facts => :get,
9 9
                  :environment_selected => :post, :architecture_selected => :post, :os_selected => :post,
10 10
                  :storeconfig_klasses => :get, :externalNodes => :get, :setBuild => :get, :puppetrun => :get},
11
                :collection => { :show_search => :get}
11
                  :collection => { :show_search => :get, :edit_action => :get, :edit_multiple => :get,
12
                  :update_multiple => :post, :save_checkbox => :get, :add_parameter => :get, :select_hostgroup => :get,
13
                  :update_hostgroup => :post}
12 14
  map.dashboard '/dashboard', :controller => 'dashboard'
13 15
  map.audit '/audit', :controller => 'audit'
14 16
  map.statistics '/statistics', :controller => 'statistics'
public/javascripts/host_checkbox.js
1
function toggleCheck() {
2
  $$('#host_select_form input.host_select_boxes').each(function(box){
3
                                                                box.checked=!box.checked;
4
                                                                //if (box.checked == true ) { box.checked = false; }
5
                                                                //if (box.checked == false) { box.checked = true;  }
6
                                                                insertHostVal(box);
7
                                                                });
8
  return false;
9
}
10
function insertHostVal(cbox) {
11
  //alert(cbox.value)
12
  //if (cbox.checked){
13
  //    alert(cbox.value)
14
  //hosts
15
  var request = new Ajax.Request('hosts/save_checkbox', {
16
                method: 'get',
17
                parameters: { box: cbox.value, is_checked: cbox.checked },
18
                onSuccess: function(res){
19
                   return false;
20
                },
21
                onFailure: function(res){
22
                   alert("Something failed! Select the checkbox again.")
23
                   return false;
24
                }
25
              });
26
}
27

  
28
function clearSession() {
29
  var request = new Ajax.Request('update_multiple', {
30
                method: 'post',
31
                parameters: { resetSession: "true" },
32
                onSuccess: function(res){
33
                   return false;
34
                },
35
                onFailure: function(res){
36
                   alert("Something failed! Click the button again.")
37
                   return false;
38
                }
39
              });
40
}
0
-