From 04ba0ba42351342e6356c01e0114c3999cda9b31 Mon Sep 17 00:00:00 2001 From: Jochen Schalanda Date: Sun, 23 May 2010 06:57:06 +0000 Subject: [PATCH] Implements feature #236 - Installation media are now usable with more than one operating system. --- app/controllers/medias_controller.rb | 2 +- app/models/media.rb | 6 +++--- app/models/operatingsystem.rb | 2 +- app/views/medias/_form.html.erb | 4 ++-- app/views/medias/index.html.erb | 3 ++- ...142706_remove_operatingsystem_id_from_medias.rb | 9 +++++++++ ...0100522143100_create_medias_operatingsystems.rb | 12 ++++++++++++ test/fixtures/medias.yml | 1 - 8 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20100522142706_remove_operatingsystem_id_from_medias.rb create mode 100644 db/migrate/20100522143100_create_medias_operatingsystems.rb diff --git a/app/controllers/medias_controller.rb b/app/controllers/medias_controller.rb index cf0340e..e397abf 100644 --- a/app/controllers/medias_controller.rb +++ b/app/controllers/medias_controller.rb @@ -1,6 +1,6 @@ class MediasController < ApplicationController def index - @medias = Media.all(:include => [:operatingsystem]) + @medias = Media.all(:include => [:operatingsystems]) end def new diff --git a/app/models/media.rb b/app/models/media.rb index 6026e3a..a76a02f 100644 --- a/app/models/media.rb +++ b/app/models/media.rb @@ -1,8 +1,8 @@ class Media < ActiveRecord::Base - belongs_to :operatingsystem + has_and_belongs_to_many :operatingsystems has_many :hosts - validates_uniqueness_of :name, :scope => :operatingsystem_id - validates_uniqueness_of :path, :scope => :operatingsystem_id + validates_uniqueness_of :name + validates_uniqueness_of :path validates_presence_of :name, :path validates_format_of :name, :with => /\A(\S+\s?)+\Z/, :message => "can't be blank or contain trailing white spaces." validates_format_of :path, :with => /^(http|https|ftp|nfs):\/\//, diff --git a/app/models/operatingsystem.rb b/app/models/operatingsystem.rb index a100faf..d053a6f 100644 --- a/app/models/operatingsystem.rb +++ b/app/models/operatingsystem.rb @@ -1,9 +1,9 @@ class Operatingsystem < ActiveRecord::Base has_many :hosts - has_many :medias has_and_belongs_to_many :ptables has_and_belongs_to_many :architectures has_and_belongs_to_many :puppetclasses + has_and_belongs_to_many :medias validates_presence_of :major, :message => "Operating System version is required" validates_presence_of :name validates_numericality_of :major diff --git a/app/views/medias/_form.html.erb b/app/views/medias/_form.html.erb index ed730dd..6b83e31 100644 --- a/app/views/medias/_form.html.erb +++ b/app/views/medias/_form.html.erb @@ -14,8 +14,8 @@ and $version, $major and $minor will be substituted for the version of the operating system.

- <%= f.label :operatingsystem_id %>
- <%= f.collection_select :operatingsystem_id, Operatingsystem.all, :id, :to_label %> + <%= f.label :operatingsystems %>
+ <%= edit_habtm @media, Operatingsystem %>

<% unless @media.new_record? -%>

Currently used by <%= @media.hosts.count %> hosts

diff --git a/app/views/medias/index.html.erb b/app/views/medias/index.html.erb index b0536d0..9f2b595 100644 --- a/app/views/medias/index.html.erb +++ b/app/views/medias/index.html.erb @@ -5,12 +5,13 @@ Name Path Operatingsystem + <% for media in @medias %> <%= link_to h(media), edit_media_path(media) %> <%=h media.path %> - <%=h media.operatingsystem %> + <%=h media.operatingsystems.to_sentence %> <%= link_to "Destroy", media, :confirm => 'Are you sure?', :method => :delete %> <% end %> diff --git a/db/migrate/20100522142706_remove_operatingsystem_id_from_medias.rb b/db/migrate/20100522142706_remove_operatingsystem_id_from_medias.rb new file mode 100644 index 0000000..37a694f --- /dev/null +++ b/db/migrate/20100522142706_remove_operatingsystem_id_from_medias.rb @@ -0,0 +1,9 @@ +class RemoveOperatingsystemIdFromMedias < ActiveRecord::Migration + def self.up + remove_column :medias, :operatingsystem_id + end + + def self.down + add_column :medias, :operatingsystem_id, :integer + end +end diff --git a/db/migrate/20100522143100_create_medias_operatingsystems.rb b/db/migrate/20100522143100_create_medias_operatingsystems.rb new file mode 100644 index 0000000..1145fcf --- /dev/null +++ b/db/migrate/20100522143100_create_medias_operatingsystems.rb @@ -0,0 +1,12 @@ +class CreateMediasOperatingsystems < ActiveRecord::Migration + def self.up + create_table :medias_operatingsystems , :id => false do |t| + t.references :media, :null => false + t.references :operatingsystem, :null => false + end + end + + def self.down + drop_table :medias_operatingsystems + end +end diff --git a/test/fixtures/medias.yml b/test/fixtures/medias.yml index d10755a..a192df8 100644 --- a/test/fixtures/medias.yml +++ b/test/fixtures/medias.yml @@ -3,4 +3,3 @@ one: name: CentOS 5.4 path: http://mirror.averse.net/centos/6.0/os/$arch - operatingsystem_id: 1 -- 1.6.3.3