kastner (owner)

Revisions

gist: 195133 Download_button fork
public
Public Clone URL: git://gist.github.com/195133.git
Embed All Files: show embed
thing.diff #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb
index 913af3c..d136fcd 100644
--- a/app/controllers/assets_controller.rb
+++ b/app/controllers/assets_controller.rb
@@ -26,6 +26,8 @@ class AssetsController < ApplicationController
   def new
     @asset = Asset.new
     @asset.ips.build
+
+ @ips = Ip.unused
 
     respond_to do |format|
       format.html # new.html.erb
@@ -36,6 +38,7 @@ class AssetsController < ApplicationController
   # GET /assets/1/edit
   def edit
     @asset = Asset.find(params[:id])
+ @ips = Ip.unused + @asset.ips
     @asset.ips.build
   end
 
diff --git a/app/models/asset.rb b/app/models/asset.rb
index a49c28f..11276d8 100644
--- a/app/models/asset.rb
+++ b/app/models/asset.rb
@@ -1,5 +1,12 @@
 class Asset < ActiveRecord::Base
   has_many :ips, :dependent => :nullify
   validates_presence_of :hostname, :colo, :cage, :vendor
- accepts_nested_attributes_for :ips
+ # accepts_nested_attributes_for :ips
+
+ def ips_attributes=(ip_addresses)
+ self.ips = []
+ ip_addresses.each do |index, ip|
+ self.ips << Ip.find(ip["id"]) if (ip["id"] && !ip["id"].empty?)
+ end
+ end
 end
diff --git a/app/models/ip.rb b/app/models/ip.rb
index c3e5747..f140e52 100644
--- a/app/models/ip.rb
+++ b/app/models/ip.rb
@@ -2,4 +2,6 @@ class Ip < ActiveRecord::Base
   belongs_to :asset
   validates_presence_of :ip_addr
   validates_uniqueness_of :ip_addr
+
+ named_scope :unused, :conditions => "asset_id IS NULL"
 end
diff --git a/app/views/assets/_form.html.erb b/app/views/assets/_form.html.erb
index 802566b..2d54ed2 100644
--- a/app/views/assets/_form.html.erb
+++ b/app/views/assets/_form.html.erb
@@ -15,10 +15,10 @@
     <dd class="form"><%= f.text_field :cpu %></dd>
     <dt class="form"><%= f.label :ram %></dt>
     <dd class="form"><%= f.text_field :ram %></dd>
- <% f.fields_for :ips do |i| %>
- <% content_tag :dt, :class => "form" do %><%= i.label :asset_id, "Ip:" %><% end %>
+ <% f.fields_for :ips do |ip_form| %>
+ <% content_tag :dt, :class => "form" do %><%= ip_form.label :asset_id, "Ip:" %><% end %>
       <% content_tag :dd, :class => "form" do %>
- <%= i.select :asset_id, Ip.find(:all, :conditions => "asset_id IS NULL").map {|i| [i.ip_addr, i.ip_addr]}, :include_blank => true %>
+ <%= ip_form.collection_select :id, @ips, :id, :ip_addr, :include_blank => true %>
       <% end %>
     <% end %>
     <dt class="form"><%= f.label :role %></dt>
diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb
index 8fd732a..7b19a15 100644
--- a/app/views/assets/show.html.erb
+++ b/app/views/assets/show.html.erb
@@ -43,6 +43,9 @@
   <%=h @asset.subrole %>
 </p>
 
+<% @asset.ips.each do |ip| %>
+<p>IP: <%= ip.ip_addr %></p>
+<% end %>
 
 <%= link_to 'Edit', edit_asset_path(@asset) %> |
 <%= link_to 'Back', assets_path %>
\ No newline at end of file