This repository has been archived by the owner on Jul 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created import_equipment_models controller and started putting it tog…
…ether
- Loading branch information
Showing
13 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ImportEquipmentModelsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
spec/controllers/import_equipment_models_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
5 changes: 5 additions & 0 deletions
5
spec/views/import_equipment_models/import_page.html.erb_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |