Skip to content

Instantly share code, notes, and snippets.

@chewmanfoo
Created June 22, 2010 21:46
Show Gist options
  • Save chewmanfoo/449148 to your computer and use it in GitHub Desktop.
Save chewmanfoo/449148 to your computer and use it in GitHub Desktop.
## the network_hosts form
[jason@sourceden impress]$ cat app/views/network_hosts/_form.html.erb
<table>
<tr><td>
<% form_for @network_host do |f| %>
<%= f.error_messages %>
<h3>Server Info</h3>
<p>
<%= f.label :name, "Hostname:" %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :os_id, "Operating System:" %><br />
<%= collection_select(:network_host, :os_id, @oses, :id, :name, {:prompt => true}) %>
</p>
<p>
<%= f.label :server_id, "Server:" %><br />
<%= collection_select(:network_host, :server_id, @servers, :id, :name, {:prompt => true}) %>
</p>
<p>
<%= f.label :service_tag %><br />
<%= f.text_field :service_tag %>
</p>
<table>
<tr>
<td>
<%= f.label :location_id, "Location:" %><br />
<%= collection_select(:network_host, :location_id, @locs, :id, :name, {:prompt => true}) %>
</td>
<td>
<%= f.label :rack %><br />
<%= f.text_field :rack, :size => 20 %>
</td>
<td>
<%= f.label :space %><br />
<%= f.text_field :space, :size => 20 %>
</td>
</td>
</table>
<p>
<%= f.label :feature_id %><br />
<%= collection_select(:network_host, :feature_id, @features, :id, :name, {:prompt => true}) %>
</p>
<p>
<%= f.label :description, "Notes:" %><br />
<%= f.text_area :description %>
</p>
<h3>Server Functions</h3>
<%= render :partial => "functions_form", :locals => {:fs => @funcs} %>
<br />
</td>
<td>
<h3>Server IP's</h3>
<% f.fields_for :ip_addresses do |builder| %>
<%= render "ip_address_fields", :f => builder %>
<% end %>
<p>
<%= link_to_add_fields "Add IP", f, :ip_addresses %>
</p>
</td></tr>
<tr><td colspan="2">
<p><%= f.submit "Submit" %></p>
</td>
</table>
<% end %>
## which produces this in the development log:
Processing NetworkHostsController#create (for 10.10.54.55 at 2010-06-22 11:29:29) [POST]
Parameters: {"commit"=>"Submit", "network_host"=>{"name"=>"test-host", "ip_addresses_attributes"=>{"0"=>{"logical_interface_name_id"=>"3", "is_entry_point"=>"1", "_delete"=>"", "is_gateway"=>"1", "mac"=>"", "ip"=>"10.10.100.1"}, "1"=>{"logical_interface_name_id"=>"", "is_entry_point"=>"0", "_delete"=>"", "is_gateway"=>"0", "mac"=>"", "ip"=>""}, "2"=>{"logical_interface_name_id"=>"", "is_entry_point"=>"0", "_delete"=>"", "is_gateway"=>"0", "mac"=>"", "ip"=>""}, "3"=>{"logical_interface_name_id"=>"", "is_entry_point"=>"0", "_delete"=>"", "is_gateway"=>"0", "mac"=>"", "ip"=>""}}, "server_id"=>"2", "os_id"=>"1", "rack"=>"3", "location_id"=>"92", "description"=>"this is a test host", "space"=>"4", "feature_id"=>"1", "service_tag"=>"", "function_ids"=>["5"]}, "authenticity_token"=>"secrecy"}
## new and create in network_hosts controller:
def new
@network_host = NetworkHost.new
@oses = OperatingSystem.all
@features = Feature.all
@networks = Network.all
@servers = Server.all
@logical_interface_names = LogicalInterfaceName.all
@funcs = Function.find(:all, :order => "name ASC")
@locs = Address.find_all_by_carrier_id("99999")
4.times {@network_host.ip_addresses.build}
if (params[:new_net_profile_id])
session[:new_net_profile_id] = params[:new_net_profile_id]
@np = NetProfile.find(params[:new_net_profile_id])
@features = @np.carrier.features
@networks = @np.networks
# carrier 99999 is the hosting facility at FM and DAL
@locs = Address.find_all_by_carrier_id(@np.carrier) + Address.find_all_by_carrier_id("99999")
end
if (params[:new_ip])
@network_host.ip = params[:new_ip]
end
if (params[:new_network_id])
session[:new_network_id] = params[:new_network_id]
end
end
def create
@network_host = NetworkHost.new(params[:network_host])
@oses = OperatingSystem.all
@features = Feature.all
@networks = Network.all
@logical_interface_names = LogicalInterfaceName.all
@funcs = Function.find(:all, :order => "name ASC")
@servers = Server.all
# @locs = Location.all
@locs = Address.find_all_by_carrier_id("99999")
if (session[:new_net_profile_id])
@np = NetProfile.find(session[:new_net_profile_id])
@network_host.net_profile_id = @np.id
@features = @np.carrier.features
@networks = @np.networks
# @locs = Location.all + n.carrier.addresses
@locs = Address.find_all_by_carrier_id(@np.carrier) + Address.find_all_by_carrier_id("99999")
# session[:new_network_id] is for ip_address.network_id
end
if @network_host.save
network_host_msg = "Successfully created Network Host <b>#{@network_host.name}</b>."
redirect_to @network_host
else
render :action => 'new'
end
end
## the models
class NetworkHost < ActiveRecord::Base
attr_accessible :name, :os_id, :server_id, :service_tag, :feature_id, :description, :net_profile_id,
:ip_addresses_attributes, :function_ids, :location_id, :rack, :space
has_many :ip_addresses, :dependent => :destroy
# make networks unique since a network_host might have several ip_addresses on the same network, without uniq we'd get back
# dup networks for each ip_address
has_many :networks, :through => :ip_addresses, :uniq => true
belongs_to :os, :class_name => "OperatingSystem"
belongs_to :feature
belongs_to :server
belongs_to :location, :class_name => "Address"
belongs_to :net_profile
has_many :function_lists, :dependent => :destroy
has_many :functions, :through => :function_lists
# validates_format_of :ip, :with => /^(d{1,3}.d{1,3}.d{1,3}.d{1,3})?$/
validates_presence_of :name, :net_profile_id
validates_associated :ip_addresses
accepts_nested_attributes_for :ip_addresses, :reject_if => lambda { |a| a[:ip].blank? }, :allow_destroy => true
after_update :save_ip_addresses
def carrier
net_profile.carrier
end
def has_functions?
functions.count > 0
end
def new_ip_address_attributes=(ip_address_attributes)
ip_address_attributes.each do |attributes|
ip_addresses.build(attributes)
end
end
def existing_ip_address_attributes=(ip_address_attributes)
ip_addresses.reject(&:new_record?).each do |ip_address|
attributes = ip_address_attributes[ip_address.id.to_s]
if attributes
ip_address.attributes = attributes
else
ip_addresses.delete(ip_address)
end
end
end
def save_ip_addresses
ip_addresses.each do |ip_address|
ip_address.save(false)
end
end
end
class IpAddress < ActiveRecord::Base
attr_accessible :name, :ip, :logical_interface_name_id, :network_host_id, :network_id, :is_entry_point, :is_gateway, :mac
belongs_to :network_host
belongs_to :network
belongs_to :logical_interface_name
# validates_format_of :ip, :with => /\A(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)(?:\.(?:25[0-5]|(?:2[0-4]|1\d|[1-9])?\d)){3}\z/
# validates_presence_of :ip, :network_host_id
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment