commit 5f6b2196e7bb3283bc2a294cdeba8b59f088812c
Author: Paul Kelly <pikelly@blueyonder.co.uk>
Date:   Fri May 7 08:31:51 2010 +0100

    Fixes #204 - Remove AS from environments page
    
    Merged import facility in Environment and Puppetclass controllers

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index dce0b8e3b..6faab4a1b 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -27,6 +27,14 @@ class ApplicationController < ActionController::Base
     render :partial => 'common/fact_selected', :layout => false
   end
 
+  def import_environments
+    ec, pc = Environment.count, Puppetclass.count
+    Environment.importClasses
+
+    flash[:foreman_notice] = "Environments old:#{ec} current:#{Environment.count}<br>PuppetClasses old:#{pc} current:#{Puppetclass.count}"
+    redirect_to :back
+  end
+
   protected
 
   def require_ssl
diff --git a/app/controllers/environments_controller.rb b/app/controllers/environments_controller.rb
index fb382942d..cded02d0c 100644
--- a/app/controllers/environments_controller.rb
+++ b/app/controllers/environments_controller.rb
@@ -1,10 +1,49 @@
 class EnvironmentsController < ApplicationController
-  active_scaffold :environment do |config|
-    config.columns = %w{name}
-    config.nested.add_link("Hosts", [:hosts])
-    config.nested.add_link("Classes", [:puppetclasses])
+  def index
+    @search       = Environment.search(params[:search])
+    @environments = @search.paginate :page => params[:page]
+  end
+
+  def show
+    @environment = Environment.find(params[:id])
+  end
+
+  def new
+    @environment = Environment.new
+  end
+
+  def create
+    @environment = Environment.new(params[:environment])
+    if @environment.save
+      flash[:foreman_notice] = "Successfully created environment."
+      redirect_to environments_path
+    else
+      render :action => 'new'
+    end
+  end
+
+  def edit
+    @environment = Environment.find(params[:id])
+  end
 
-    config.action_links.add 'import_classes_and_environments', :label => 'Import environments and classes', :inline => false,
-        :page => :true , :type => :table
+  def update
+    @environment = Environment.find(params[:id])
+    if @environment.update_attributes(params[:environment])
+      flash[:foreman_notice] = "Successfully updated environment."
+      redirect_to environments_path
+    else
+      render :action => 'edit'
+    end
   end
+
+  def destroy
+    @environment = Environment.find(params[:id])
+    if @environment.destroy
+      flash[:foreman_notice] = "Successfully destroyed '#{@environment.name}''"
+    else
+      flash[:foreman_error]  = @environment.errors.full_messages.join("<br>")
+    end
+    redirect_to environments_url
+  end
+
 end
diff --git a/app/controllers/puppetclasses_controller.rb b/app/controllers/puppetclasses_controller.rb
index f03c14611..4feca3ed6 100644
--- a/app/controllers/puppetclasses_controller.rb
+++ b/app/controllers/puppetclasses_controller.rb
@@ -41,13 +41,6 @@ class PuppetclassesController < ApplicationController
     redirect_to puppetclasses_url
   end
 
-  def import
-    ec, pc = Environment.count, Puppetclass.count
-    Environment.importClasses
-    flash[:foreman_notice] = "Environments   old:#{ec}\tcurrent:#{Environment.count}<br>PuppetClasses old:#{pc}\tcurrent:#{Puppetclass.count}"
-    redirect_to :back
-  end
-
   # AJAX methods
 
   # adds a puppetclass to an existing host or hostgroup
diff --git a/app/models/environment.rb b/app/models/environment.rb
index 77ed5764f..9711229d6 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -3,7 +3,7 @@ class Environment < ActiveRecord::Base
   has_many :hosts
   validates_presence_of :name
   validates_uniqueness_of :name
-  validates_format_of   :name, :with => /^\S+$/, :message => "Name cannot contain spaces"
+  validates_format_of   :name, :with => /^\S+$/, :message => "cannot contain spaces"
   default_scope :order => 'name'
 
   def to_label
@@ -14,6 +14,10 @@ class Environment < ActiveRecord::Base
     name
   end
 
+  def self.per_page
+    10
+  end
+
   # returns an hash of all puppet environments and their relative paths
   def self.puppetEnvs
     env = Hash.new
diff --git a/app/models/model.rb b/app/models/model.rb
index 6dfa3edbb..83a91f137 100644
--- a/app/models/model.rb
+++ b/app/models/model.rb
@@ -8,5 +8,5 @@ class Model < ActiveRecord::Base
   def to_label
     name
   end
-
+  alias_method :to_s, :to_label
 end
diff --git a/app/views/environments/_form.html.erb b/app/views/environments/_form.html.erb
new file mode 100644
index 000000000..218ff591c
--- /dev/null
+++ b/app/views/environments/_form.html.erb
@@ -0,0 +1,9 @@
+<% form_for @environment do |f| %>
+  <%= f.error_messages %>
+  <table>
+    <tr>
+      <td> Name<br> <%= f.text_field :name %> </td>
+    </tr>
+  </table>
+  <%= f.submit "Submit" %></p>
+<% end %>
diff --git a/app/views/environments/edit.html.erb b/app/views/environments/edit.html.erb
new file mode 100644
index 000000000..ba322eed0
--- /dev/null
+++ b/app/views/environments/edit.html.erb
@@ -0,0 +1,7 @@
+<% title "Edit Environment" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+  <%= link_to "View All", environments_path %>
+</p>
diff --git a/app/views/environments/index.html.erb b/app/views/environments/index.html.erb
new file mode 100644
index 000000000..e09bc09d4
--- /dev/null
+++ b/app/views/environments/index.html.erb
@@ -0,0 +1,27 @@
+<% title "Environments" %>
+
+<table class="list">
+  <tr>
+    <th>Name</th><th></th>
+  </tr>
+
+  <% for environment in @environments %>
+    <tr class="<%= cycle("even", "odd") -%>">
+      <td>
+        <%= link_to h(environment.name), edit_environment_path(environment) %>
+      </td>
+      <td style="float:right;">
+        <%= link_to "Hosts",   hosts_path         + "?search[environment_id_equals]=#{environment.id}"%>
+        <%= link_to "classes", puppetclasses_path + "?search[environments_id_equals]=#{environment.id}"%>
+        <%= link_to "Destroy", environment, :confirm => "Delete environment '#{environment.name}'?", :method => :delete %>
+      </td>
+    </tr>
+  <% end %>
+</table>
+
+<%= page_entries_info @environments %>
+<%= will_paginate @environmentsS %>
+
+<p><%= link_to "New Environment", new_environment_path %>
+   <%= link_to "Import all classes and environments",  import_environments_environments_path %>
+</p>
diff --git a/app/views/environments/new.html.erb b/app/views/environments/new.html.erb
new file mode 100644
index 000000000..e1db8660f
--- /dev/null
+++ b/app/views/environments/new.html.erb
@@ -0,0 +1,5 @@
+<% title "New Environment" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", environments_path %></p>
diff --git a/app/views/puppetclasses/index.html.erb b/app/views/puppetclasses/index.html.erb
index 9e4704fc7..254ef9bae 100644
--- a/app/views/puppetclasses/index.html.erb
+++ b/app/views/puppetclasses/index.html.erb
@@ -25,5 +25,5 @@
 <%= page_entries_info @puppetclasses %>
 <%= will_paginate @puppetclasses %>
 <p><%= link_to "New Puppetclass", new_puppetclass_path %> |
-<%= link_to "Import new puppet classes and environments", import_classes_path %>
+<%= link_to "Import new puppet classes and environments", import_environments_puppetclasses_path %>
 </p>
diff --git a/config/routes.rb b/config/routes.rb
index cd89524e6..3474bc789 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -27,11 +27,10 @@ ActionController::Routing::Routes.draw do |map|
   map.resources :medias
   map.resources :models
   map.resources :architectures
-  map.resources :puppetclasses, :member => { :assign => :post }
-  map.import_classes '/puppetclass/import', :controller => 'puppetclasses', :action => 'import'
+  map.resources :puppetclasses, :member => { :assign => :post }, :collection => {:import_environments => :get}
   map.resources :hostgroups
   map.resources :common_parameters
-  map.resources :environments, :active_scaffold => true
+  map.resources :environments, :collection => {:import_environments => :get}
   map.resources :subnets, :active_scaffold => true
   map.resources :fact_values, :active_scaffold => true
   map.resources :ptables
diff --git a/test/fixtures/environments.yml b/test/fixtures/environments.yml
index 05d5f44aa..b9183afe4 100644
--- a/test/fixtures/environments.yml
+++ b/test/fixtures/environments.yml
@@ -1,7 +1,7 @@
 # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
 
- one:
+ production:
    name: production
-#
-# two:
-#   column: value
+
+ global_puppetmaster:
+   name: global_puppetmaster
diff --git a/test/functional/environments_controller_test.rb b/test/functional/environments_controller_test.rb
index 1e9fe62f8..cf5fb101c 100644
--- a/test/functional/environments_controller_test.rb
+++ b/test/functional/environments_controller_test.rb
@@ -1,15 +1,11 @@
 require 'test_helper'
 
 class EnvironmentsControllerTest < ActionController::TestCase
-  test "ActiveScaffold should look for Environment model" do
-    assert_not_nil EnvironmentsController.active_scaffold_config
-    assert EnvironmentsController.active_scaffold_config.model == Environment
-  end
 
   test "should get index" do
     get :index, {}, set_session_user
     assert_response :success
-    assert_not_nil assigns(:records)
+    assert_not_nil assigns(:environments)
   end
 
   test "should get new" do
@@ -19,7 +15,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
 
   test "should create new environment" do
     assert_difference 'Environment.count' do
-      post :create, { :commit => "Create", :record => {:name => "some_environment"} }, set_session_user
+      post :create, { :commit => "Create", :environment => {:name => "some_environment"} }, set_session_user
     end
 
     assert_redirected_to environments_path
@@ -37,7 +33,7 @@ class EnvironmentsControllerTest < ActionController::TestCase
     environment = Environment.new :name => "some_environment"
     assert environment.save!
 
-    put :update, { :commit => "Update", :id => environment.id, :record => {:name => "other_environment"} }, set_session_user
+    put :update, { :commit => "Update", :id => environment.id, :environment => {:name => "other_environment"} }, set_session_user
     environment = Environment.find_by_id(environment.id)
     assert environment.name == "other_environment"
 
diff --git a/test/unit/host_test.rb b/test/unit/host_test.rb
index fd7d9149c..b3cc96996 100644
--- a/test/unit/host_test.rb
+++ b/test/unit/host_test.rb
@@ -72,7 +72,7 @@ class HostTest < ActiveSupport::TestCase
       :architecture => Architecture.first, :environment => Environment.first, :disk => "aaa"
 
     # dummy external node info
-    nodeinfo = {"environment" => "production", "parameters"=>{"puppetmaster"=>"puppet", "MYVAR"=>"value"}, "classes"=>["base","apache"]}
+    nodeinfo = {"environment" => "global_puppetmaster", "parameters"=>{"puppetmaster"=>"puppet", "MYVAR"=>"value"}, "classes"=>["base","apache"]}
 
     host.importNode nodeinfo
 
