From f3498ddcac3f7427e1bfd543513c4939991934c8 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Fri, 1 Feb 2013 00:06:13 -0700 Subject: [PATCH] Added mac logs, improved mac editing/viewing --- app/assets/javascripts/mac_logs.js.coffee | 3 ++ app/assets/stylesheets/mac_logs.css.scss | 3 ++ app/controllers/mac_logs_controller.rb | 7 +++ app/controllers/macs_controller.rb | 30 +++++++++---- app/helpers/mac_logs_helper.rb | 2 + app/models/mac.rb | 2 + app/models/mac_log.rb | 2 + app/views/mac_logs/index.html.erb | 10 +++++ app/views/macs/import.html.erb | 1 + app/views/macs/index.html.erb | 49 +++++++++++++-------- config/database.yml | 2 +- config/routes.rb | 3 ++ test/functional/mac_logs_controller_test.rb | 7 +++ test/unit/helpers/mac_logs_helper_test.rb | 4 ++ 14 files changed, 98 insertions(+), 27 deletions(-) create mode 100644 app/assets/javascripts/mac_logs.js.coffee create mode 100644 app/assets/stylesheets/mac_logs.css.scss create mode 100644 app/controllers/mac_logs_controller.rb create mode 100644 app/helpers/mac_logs_helper.rb create mode 100644 app/views/mac_logs/index.html.erb create mode 100644 app/views/macs/import.html.erb create mode 100644 test/functional/mac_logs_controller_test.rb create mode 100644 test/unit/helpers/mac_logs_helper_test.rb diff --git a/app/assets/javascripts/mac_logs.js.coffee b/app/assets/javascripts/mac_logs.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/mac_logs.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/mac_logs.css.scss b/app/assets/stylesheets/mac_logs.css.scss new file mode 100644 index 0000000..f369717 --- /dev/null +++ b/app/assets/stylesheets/mac_logs.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the MacLogs controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/mac_logs_controller.rb b/app/controllers/mac_logs_controller.rb new file mode 100644 index 0000000..ad8c7ee --- /dev/null +++ b/app/controllers/mac_logs_controller.rb @@ -0,0 +1,7 @@ +class MacLogsController < ApplicationController + +def index + @mac_logs = MacLog.desc.limit(1000) +end + +end diff --git a/app/controllers/macs_controller.rb b/app/controllers/macs_controller.rb index 46d4aff..14083d5 100644 --- a/app/controllers/macs_controller.rb +++ b/app/controllers/macs_controller.rb @@ -8,7 +8,8 @@ def index @active_macs = Mac.where(:active => true, :hidden => false) @active_macs += Mac.where(:active => true, :hidden => nil) @hidden_macs = Mac.where(:active => true, :hidden => true) - @inactive_macs = Mac.where(:active => false) + + @all_macs = Mac.find(:all, :order => "LOWER(mac)") end # GET /macs/1 @@ -45,8 +46,7 @@ end def create @mac = Mac.new(params[:mac]) @mac.user_id = params[:user_id] - - Rails.logger.info @macs.inspect + @users = User.all.sort_by(&:name) respond_to do |format| if @mac.save @@ -64,11 +64,11 @@ end def update #Log who updated this @mac = Mac.find(params[:id]) + @users = User.all.sort_by(&:name) respond_to do |format| if @mac.update_attributes(params[:mac]) - Rails.logger.info @mac.inspect - format.html { redirect_to macs_path, :notice => 'User certification was successfully updated.' } + format.html { redirect_to macs_path, :notice => 'Mac was successfully updated.' } format.json { head :no_content } else format.html { render :action => "edit" } @@ -153,8 +153,7 @@ Rails.logger.info "starting scan..." # Scan the existing macs and update each record as necessary Mac.find(:all).each { |entry| - Rails.logger.info "Existing MAC: "+entry.inspect - mac = entry.mac + mac = entry.mac.downcase ip = entry.ip if macs.has_key?(mac) if ! entry.active || ! entry.since @@ -185,7 +184,7 @@ Rails.logger.info "starting scan..." macs.each { |mac, ip| Rails.logger.info "Activating new entry #{mac} at #{ip}" if options[:verbose] Mac.new(:mac => mac, :ip => ip, :active => 1, :since => Time.now, :refreshed => Time.now).save - MacLog.new(:mac => mac, :ip => ip, :action => "activate").save + Rails.logger.info MacLog.new(:mac => mac, :ip => ip, :action => "activate").save } @log = MacLog.all @@ -193,4 +192,19 @@ Rails.logger.info "starting scan..." end #def scan +def import + +require 'csv' + +csv_text = File.read('mac_log.csv') +csv = CSV.parse(csv_text) + +@output = [] + +csv.each do |row| + @output += [row[1], Mac.create({:mac => row[0], :note => row[1], :hidden => row[2]}) ] +end + +end + end diff --git a/app/helpers/mac_logs_helper.rb b/app/helpers/mac_logs_helper.rb new file mode 100644 index 0000000..7a83d90 --- /dev/null +++ b/app/helpers/mac_logs_helper.rb @@ -0,0 +1,2 @@ +module MacLogsHelper +end diff --git a/app/models/mac.rb b/app/models/mac.rb index 817c50a..45a7951 100644 --- a/app/models/mac.rb +++ b/app/models/mac.rb @@ -1,4 +1,6 @@ class Mac < ActiveRecord::Base belongs_to :user attr_accessible :active, :ip, :mac, :refreshed, :since, :hidden, :note, :user_id + + validates_uniqueness_of :mac, :case_sensitive => false end diff --git a/app/models/mac_log.rb b/app/models/mac_log.rb index 142c825..e29dd58 100644 --- a/app/models/mac_log.rb +++ b/app/models/mac_log.rb @@ -1,3 +1,5 @@ class MacLog < ActiveRecord::Base attr_accessible :action, :ip, :mac + + scope :desc, order("mac_logs.created_at DESC") end diff --git a/app/views/mac_logs/index.html.erb b/app/views/mac_logs/index.html.erb new file mode 100644 index 0000000..7baf7ba --- /dev/null +++ b/app/views/mac_logs/index.html.erb @@ -0,0 +1,10 @@ + +<% @mac_logs.each do |mac_log| %> + + + + + + +<% end %> +
<%= mac_log.mac %><%= mac_log.ip %><%= mac_log.action %><%= mac_log.created_at %>
diff --git a/app/views/macs/import.html.erb b/app/views/macs/import.html.erb new file mode 100644 index 0000000..843824d --- /dev/null +++ b/app/views/macs/import.html.erb @@ -0,0 +1 @@ +<%= @output %> diff --git a/app/views/macs/index.html.erb b/app/views/macs/index.html.erb index 6148684..bcfc91b 100644 --- a/app/views/macs/index.html.erb +++ b/app/views/macs/index.html.erb @@ -1,29 +1,42 @@

What machines are on our network?

+<%= link_to "New Mac", new_mac_path %> + + + +