From cad46e45c590f859e632cd76e7fbd4a570ea56ab Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Mon, 23 Sep 2013 18:34:18 -0700 Subject: [PATCH] Adding users without cards report, fixing mac registration for existing macs --- app/controllers/macs_controller.rb | 44 ++++++++++++++----------- app/controllers/users_controller.rb | 1 + app/views/macs/_form.html.erb | 2 +- app/views/macs/edit.html.erb | 2 +- app/views/macs/index.html.erb | 2 +- app/views/macs/new.html.erb | 2 +- app/views/macs/show.html.erb | 2 +- app/views/users/activity.html.erb | 50 +++++++++++++++++++++++++++-- 8 files changed, 79 insertions(+), 26 deletions(-) diff --git a/app/controllers/macs_controller.rb b/app/controllers/macs_controller.rb index 29381f1..d26bc89 100644 --- a/app/controllers/macs_controller.rb +++ b/app/controllers/macs_controller.rb @@ -9,7 +9,7 @@ rescue_from CanCan::AccessDenied do |exception| redirect_to main_app.root_url, :alert => "Nothing to see here!" end end -load_and_authorize_resource :mac +load_and_authorize_resource :mac, :except => :create load_and_authorize_resource :user, :through => :mac, :except => [:index, :show, :scan, :import] #require "active_record" @@ -80,26 +80,34 @@ end end # POST /macs - # POST /user def create @mac = Mac.new(params[:mac]) - authorize! :update, @mac - - if can? :manage, Mac then - @users = User.accessible_by(current_ability).sort_by(&:name) - else - @users = [current_user] - end - - respond_to do |format| - if @mac.save - format.html { redirect_to macs_path, :notice => 'Mac was successfully created.' } - format.json { render :json => @mac, :status => :created, :location => @mac } + @existing_mac = Mac.find_by_mac(@mac.mac) + if can? :manage, Mac then + @users = User.accessible_by(current_ability).sort_by(&:name) + else + @users = [current_user] + end + + if @existing_mac.present? + if @existing_mac.user_id.nil? + redirect_to edit_mac_path(@existing_mac), :notice => 'This MAC already exists, edit it here:' + else +@mac.errors.add(:user,"for this MAC is already set to #{@existing_mac.user.name} -- please contact them or an admin if this is incorrect.") + render :action => "new" + end else - format.html { render :action => "new" } - format.json { render :json => @mac.errors, :status => :unprocessable_entity } + + respond_to do |format| + if @mac.save + format.html { redirect_to macs_path, :notice => 'MAC was successfully created.' } + format.json { render :json => @mac, :status => :created, :location => @mac } + else + format.html { render :action => "new" } + format.json { render :json => @mac.errors, :status => :unprocessable_entity } + end + end end - end end # PUT /macs/1 @@ -119,7 +127,7 @@ end respond_to do |format| if @mac.save - format.html { redirect_to macs_path, :notice => 'Mac 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" } diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a9ba487..b0025d6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -42,6 +42,7 @@ class UsersController < ApplicationController def activity @user_logins = User.where(:last_sign_in_at => 1.month.ago..Date.today) @new_users = User.where(:created_at => 3.months.ago..Date.today) + @cardless_users = User.includes('cards').where(['users.member_level >= ?','50']).where('cards.id IS NULL') end # GET /users/1 diff --git a/app/views/macs/_form.html.erb b/app/views/macs/_form.html.erb index a45e6e4..61586dd 100644 --- a/app/views/macs/_form.html.erb +++ b/app/views/macs/_form.html.erb @@ -1,7 +1,7 @@ <%= form_for(@mac) do |f| %> <% if @mac.errors.any? %>
-

<%= pluralize(@mac.errors.count, "error") %> prohibited this Mac from being saved:

+

<%= pluralize(@mac.errors.count, "error") %> prohibited this MAC from being saved: