Skip to content

Commit

Permalink
Switching over to using rails engines
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Kelly committed Oct 3, 2010
1 parent 6a354e6 commit 47c2f21
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 30 deletions.
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ActionController::Routing::Routes.draw do |map|
map.resources :subnets
end
22 changes: 20 additions & 2 deletions install.rb
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
Dir.glob(File.join(File.dirname(__FILE__), "db", "migrate", "*")).each do |file|
FileUtils.cp file, File.join(Rails.root, "db", "migrate"), :verbose => true
#!/usr/bin/ruby
require 'fileutils'
root = File.join(File.dirname(__FILE__),"..", "..", "..")
Dir.glob(File.join(File.dirname(__FILE__), "lib", "db", "migrate", "*")).each do |file|
FileUtils.cp file, File.join(root, "db", "migrate"), :verbose => true
end
puts "********************************************************************"
puts "Please run rake db:migrate from the root of your installation"
puts "This will add support for vendors, servertypes and network databases"
puts "********************************************************************"

routes = File.join(root, "config", "routes")
new_routes = File.join(root, "config", "routes.new")
unless `grep subnets #{routes}`
File.open(routes, "r") do |src|
File.open(new_routes, "w") do |dst|
line = src.readline
dst.write("map.NetdbManagers") if line.match(/Routes.draw/)
dst.write line
end
end
FileUtils.mv src, dst
end
53 changes: 35 additions & 18 deletions lib/app/controllers/subnets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
class SubnetsController < ApplicationController
layout 'standard'
def index
@search = Subnet.search params[:search]
@subnets = @search.paginate(:page => params[:page])
end

def new
@subnet = Subnet.new
end

active_scaffold :subnet do |config|
config.columns = [:domain, :name, :number, :mask, :ranges, :dhcp, :vlanid]
config.create.columns = [:domain, :name, :number, :mask, :ranges, :dhcp, :priority, :vlanid]
config.columns[:domain].label = "Site"
config.columns[:vlanid].label = "VLAN id"
columns[:dhcp].label = "DHCP Server"
columns[:ranges].label = "Address ranges"
config.columns[:ranges].description = "A list of comma separated single IPs or start-end couples."
columns[:mask].label = "Netmask"
config.columns[:domain].form_ui = :select
config.columns[:dhcp].form_ui = :select
list.columns.exclude :created_at, :updated_at
list.sorting = {:domain => 'DESC' }
columns['domain'].sort_by :sql
def create
@subnet = Subnet.new(params[:subnet])
if @subnet.save
flash[:foreman_notice] = "Successfully created subnet."
redirect_to subnets_url
else
render :action => 'new'
end
end

# Deletes require a page update so as to show error messsages
config.delete.link.inline = false
def edit
@subnet = Subnet.find(params[:id])
end

def update
@subnet = Subnet.find(params[:id])
if @subnet.update_attributes(params[:subnet])
flash[:foreman_notice] = "Successfully updated subnet."
redirect_to subnets_url
else
render :action => 'edit'
end
end

config.nested.add_link "Hosts", [:hosts]
def destroy
@subnet = Subnet.find(params[:id])
@subnet.destroy
flash[:foreman_notice] = "Successfully destroyed subnet."
redirect_to subnets_url
end
end
7 changes: 7 additions & 0 deletions lib/app/models/servertype.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Servertype < ActiveRecord::Base
has_many :netdbs

validates_uniqueness_of :name
validates_presence_of :name
end

6 changes: 6 additions & 0 deletions lib/app/models/vendor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Vendor < ActiveRecord::Base
has_many :netdbs

validates_uniqueness_of :name
validates_presence_of :name
end
37 changes: 37 additions & 0 deletions lib/app/views/subnets/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<% form_for @subnet do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :domain %><br />
<%= f.collection_select :domain_id, Domain.all, :id, :name %>
</p>
<p>
<%= f.label :number %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :mask %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :ranges %><br />
<%= f.text_field :name, :title => ""A list of comma separated single IPs or start-end couples."" %>
</p>
<p>
<%= f.label :priority %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :vlanid %><br />
<%= f.text_field :name %>
</p>
<p>
<p><%= f.submit "Submit" %></p>

<% unless @subnet.new_record? -%>
Used by <%= @subnet.hosts.count %> hosts
<% end -%>
<% end %>
6 changes: 6 additions & 0 deletions lib/app/views/subnets/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<% title "Edit Subnet" %>

<%= render :partial => 'form' %>

<p><%= link_to "View All Subnets", subnets_path %></p>

32 changes: 32 additions & 0 deletions lib/app/views/subnets/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% title "Subnets" %>

<table class="list">
<tr>
<th>Name</th>
<th>Domain</th>
<th>Number</th>
<th>Mask</th>
<th>Ranges</th>
<th>Priority</th>
<th>VLAN id</th>
<th></th>
</tr>
<% for subnet in @subnets %>
<tr class="<%= cycle("even", "odd") -%>">
<td><%=link_to h(subnet.name), edit_subnet_path(subnet)%></td>
<td><%=subnet.domain%></td>
<td><%=subnet.number%></td>
<td><%=subnet.mask%></td>
<td><%=subnet.ranges%></td>
<td><%=subnet.priority%></td>
<td><%=subnet.vlanid%></td>
<td align="right">
<%= link_to "Destroy", subnet, :confirm => "Delete #{subnet.name}?", :method => :delete %>
</td>
</tr>
<% end %>
</table>
<%= page_entries_info @subnets %>
<%= will_paginate @subnets %>

<p><%= link_to "New Subnet", new_subnet_path %></p>
5 changes: 5 additions & 0 deletions lib/app/views/subnets/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% title "New Subnet" %>

<%= render :partial => 'form' %>

<p><%= link_to "Back to List", subnets_path %></p>
5 changes: 4 additions & 1 deletion lib/netdb_manager.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
require 'netdb_manager/host_ext'
#require "netdb_manager/routing"

%w{ models controllers helpers}.each do |dir|
path = File.join(File.dirname(__FILE__), 'app', dir)
$LOAD_PATH << path
ActiveSupport::Dependencies.load_paths << path
ActiveSupport::Dependencies.load_once_paths.delete(path)
end
end

ActionController::Base.prepend_view_path File.join(File.dirname(__FILE__), 'app', 'views')
18 changes: 9 additions & 9 deletions lib/netdb_manager/host_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ def self.included(base)
require 'ipaddr'
include DHCP
else
if SETTINGS[:unattended].nil? or SETTINGS[:unattended]
RAILS_DEFAULT_LOGGER.warn "*********************************************************************"
RAILS_DEFAULT_LOGGER.warn "DHCP and DNS management require that you install the memcache service"
RAILS_DEFAULT_LOGGER.warn "and that you add this line to environment.db"
RAILS_DEFAULT_LOGGER.warn "config.cache_store = :mem_cache_store"
RAILS_DEFAULT_LOGGER.warn "*********************************************************************"
end
@dhcp = nil
return
message = "*********************************************************************\n" +
"DHCP and DNS management require that you install the memcache service\n" +
"and that you add this line to environment.db \n" +
"config.cache_store = :mem_cache_store \n" +
"and edit config.initializers/session_store to set = :mem_cache_store \n" +
"*********************************************************************\n"
RAILS_DEFAULT_LOGGER.warn message
puts message
exit
end

base.extend ClassMethods
Expand Down
12 changes: 12 additions & 0 deletions lib/netdb_manager/routing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module NetdbManager
#:nodoc:
module Routing
#:nodoc:
module MapperExtensions
def NetdbManagers
@set.add_route("/subnets", {:controller => "subnets", :action => "index"})
end
end
end
end
ActionController::Routing::RouteSet::Mapper.send :include, NetdbManager::Routing::MapperExtensions

0 comments on commit 47c2f21

Please sign in to comment.