Skip to content
This repository has been archived by the owner on Jul 24, 2020. It is now read-only.

Commit

Permalink
created import_equipment_models controller and started putting it tog…
Browse files Browse the repository at this point in the history
…ether
  • Loading branch information
orenyk committed Mar 25, 2014
1 parent 4923b13 commit a5feecc
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/import_equipment_models.js.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/import_equipment_models.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the ImportEquipmentModels controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
54 changes: 54 additions & 0 deletions app/controllers/import_equipment_models_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class ImportEquipmentModelsController < ApplicationController
include CsvImport

before_filter :require_admin

# fully modeled after import_users_controller
def import
# initialize
file = params[:csv_upload]
if file
overwrite = (params[:overwrite] == '1')
filepath = file.tempfile.path # the rails CSV class needs a filepath

imported_equipment = csv_import(filepath)
end

if valid_input_file?(imported_equipment, file)
# create the equipment and categories and exit
@hash_of_statuses = import_equipment(imported_equipment, overwrite)
render 'imported'
end

end

def import_page
render 'import'
end

private

# as in import_users_controller, not idiomatic in Rails but good for now
def valid_input_file?(imported_equipment, file)
# check for file
if !file
flash[:error] = 'Please select a file to upload.'
redirect_to :back and return
end

# check for total CSV import failure
if imported_equipment.nil?
flash[:error] = 'Unable to import CSV file. Please ensure it matches the input format and try again.'
redirect_to :back and return
end

# check for proper headings / columns
accepted_keys = [:name, :description, :category, :late_fee, :replacement_fee]
unless imported_equipment.first.keys == accepted_keys
flash[:error] = 'Unable to import CSV file. Please ensure that the first line of the file exactly matches the sample input (name, description, etc.) Note that headers are case sensitive and must be in the correct order.'
redirect_to :back and return
end
return true
end

end
2 changes: 2 additions & 0 deletions app/helpers/import_equipment_models_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ImportEquipmentModelsHelper
end
25 changes: 25 additions & 0 deletions app/views/import_equipment_models/_import_table.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Category</th>
<th>Late Fee</th>
<th>Replacement Fee</th>
<th>Reason</th>
</tr>
</thead>
<tbody>
<% equipment_models.each do |equipment_data| %>
<% equipment = equipment_data.first %>
<% message = equipment_data.last %>
<tr>
<td><%= equipment[:name] %></td>
<td><%= equipment[:description] %></td>
<td><%= equipment[:category] %></td>
<td><%= equipment[:late_fee] %></td>
<td><%= equipment[:replacement_fee] %></td>
<td><%= message %></td>
</tr>
<% end %>
</tbody>

26 changes: 26 additions & 0 deletions app/views/import_equipment_models/import.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<% title "Import Equipment" %>

<h3>CSV Import Format</h3>
<h5>Columns (must be in the first row of the CSV file)</h5>
<%= markdown("
Name, Description, Category, Late Fee, Replacement Fee") %>
<h5>Example</h5>
<%= markdown("
name,description,category,late_fee,replacement_fee
Nikon D70,Nikon D70 Digital SLR,DSLR,25,900
3-Prong Extension Cord,3-Prong Extension Cord,Cables,0,25") %>

<hr />

<%= form_tag equipment_imported_path, :multipart => true do %>

<p>
<label>CSV file with users to import:</label>
<%= file_field_tag 'csv_upload' %>
</p>
<p>
<label>Update existing equipment's information?</label>
<%= check_box_tag 'overwrite' %>
</p>
<%= submit_tag 'Import Equipment!', :class => 'btn' %>
<% end %>
50 changes: 50 additions & 0 deletions app/views/import_equipment_models/imported.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<% title "Import Summary" %>
<% if @hash_of_statuses %>
<% unless @hash_of_statuses[:fail].empty? %>
<h2>Equipment not successfully imported</h2>
<table class="datatable-wide table table-striped table-bordered" id="user_import_errors_table">
<%= render :partial => 'import_table', :locals => {:equipment_model => @hash_of_statuses[:fail]} %>
</table>
<% unless @hash_of_statuses[:success].empty? %>
<hr />
<% end %>
<% end %>
<% unless @hash_of_statuses[:success].empty? %>
<h2>Equipment successfully imported</h2>
<table id="table_woo" class="datatable-wide table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<%= "<th>Type</th>".html_safe unless @category %>
<%# these blank TH's necessary for the sort function %>
<% if current_user.is_admin?(:as => 'admin') %>
<th class="no_sort"> </th>
<th class="no_sort"> </th>
<% end %>
</tr>
</thead>
<tbody>
<% for equipment_model in @hash_of_statuses[:success]} %>
<tr>
<td><%= link_to equipment_model.name, equipment_model %></td>
<%= ("<td>" + (link_to equipment_model.category.name.singularize, equipment_model.category) + "</td>").html_safe unless @category %>
<% if current_user.is_admin?(:as => 'admin') %>
<td><%= link_to "Edit", edit_equipment_model_path(equipment_model), :class => "btn" %></td>
<td>
<% if equipment_model.deleted_at %>
<%= make_activate_btn(:equipment_models,equipment_model) %>
<% else %>
<%= make_deactivate_btn(:equipment_models,equipment_model) %>
<% end %>
</td>
<% end %>
</tr>
<% end %>
</tbody>
</table>
<% end %>
<% else %>
<h2>Please follow <%= link_to "this link", :controller => :import_equipment_models, :action => :import_page %> to import equipment.</h2>
<% end %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
get '/import_users/import' => 'import_users#import_page', :as => :csv_import_page
post '/import_users/imported' => 'import_users#import', :as => :csv_imported

get '/import_equipment/import' => 'import_equipment_models#import_page', :as => :equipment_import_page
post '/import_equipment/imported' => 'import_equipment_models#import', :as => :equipment_imported

resources :users do
collection do
get :find
Expand Down
64 changes: 64 additions & 0 deletions lib/csv_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,68 @@ def set_or_create_user_for_import(user_data)
end
return user
end

# modeled after import_user above... definitely not ideal to have both of these in the same file but we can refactor later
def import_equipment(array_of_equipment_data, overwrite=false)

@eq_array_of_success = [] # will contain user-objects
@eq_array_of_fail = [] # will contain user_data hashes and error messages
@eq_overwrite = overwrite

array_of_equipment_data.each do |equipment_data|
equipment_data[:csv_import] = true

attempt_save_equipment(equipment_data)
end

hash_of_statuses = {success: @array_of_success, fail: @array_of_fail}
end

def attempt_save_equipment(user_data)
equipment_model = set_or_create_equipment_for_import(user_data)

user.update_attributes(user_data)
# if the updated or new user is valid, save to database and add to array of successful imports
if user.valid?
user.save
@array_of_success << user
return true
else
return false
end
end

# attempts to save a user with ldap lookup
def attempt_save_with_ldap(user_data)
ldap_hash = import_with_ldap(user_data)
if ldap_hash
user_data = ldap_hash
else
@array_of_fail << [user_data, 'Incomplete user information. Unable to find user in online directory (LDAP).']
return
end

user = set_or_create_user_for_import(user_data)

if user.valid?
user.save
@array_of_success << user
return
else
@array_of_fail << [user_data, user.errors.full_messages.to_sentence.capitalize + '.']
return
end
end

# sets the equipment based on the overwrite parameter
def set_or_create_equipment_for_import(equipment_data)
# set the equuipment model and attempt to save with given data
if @overwrite and (EquipmentModel.where("name = ?", equipment_data[:name]).size > 0)
equipment_model = EquipmentModel.where("name = ?", equipment_data[:name]).first
else
equipment_model = EquipmentModel.new(equipment_data)
end
return equipment_model
end

end
19 changes: 19 additions & 0 deletions spec/controllers/import_equipment_models_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'spec_helper'

describe ImportEquipmentModelsController do

describe "GET 'import'" do
it "returns http success" do
get 'import'
response.should be_success
end
end

describe "GET 'import_page'" do
it "returns http success" do
get 'import_page'
response.should be_success
end
end

end
15 changes: 15 additions & 0 deletions spec/helpers/import_equipment_models_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the ImportEquipmentModelsHelper. For example:
#
# describe ImportEquipmentModelsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
describe ImportEquipmentModelsHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/views/import_equipment_models/import.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "import_equipment_models/import.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe "import_equipment_models/import_page.html.erb" do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit a5feecc

Please sign in to comment.