-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from ltfschoen/feature/csv
Feature / CSV Upload and Display - Tests pass. Updated controller, models, views.
- Loading branch information
Showing
15 changed files
with
180 additions
and
4 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
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
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://coffeescript.org/ |
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 Products 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,17 @@ | ||
class ProductsController < ApplicationController | ||
def index | ||
@products = Product.all | ||
end | ||
|
||
def import | ||
# Validate inputs with block | ||
begin | ||
file = params[:file] | ||
file_path = file.path | ||
Product.import(file_path) | ||
redirect_to root_url, notice: "Products imported." | ||
rescue | ||
redirect_to root_url, notice: "Invalid CSV file format." | ||
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,18 @@ | ||
class Product < ApplicationRecord | ||
require 'csv' | ||
|
||
def self.import(file_path) | ||
CSV.foreach(file_path, headers: true) do |row| | ||
|
||
product_hash = row.to_hash | ||
product = Product.where(id: product_hash["id"]) | ||
|
||
if product.count == 1 | ||
# Prevent CSV updates from changing the database comments attribute | ||
product.first.update_attributes(product_hash.expect("comments")) | ||
else | ||
Product.create!(product_hash) | ||
end | ||
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,2 @@ | ||
<h1>Products#import</h1> | ||
<p>Find me in app/views/products/import.html.erb</p> |
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,30 @@ | ||
<%= flash[:notice] %> | ||
<table> | ||
<thead> | ||
<tr> | ||
<th>Id</th> | ||
<th>Name</th> | ||
<th>Price</th> | ||
<th>Quantity</th> | ||
<th>Comments</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<% @products.each do |product| %> | ||
<tr> | ||
<td><%= product.id %></td> | ||
<td><%= product.name %></td> | ||
<td><%= product.price %></td> | ||
<td><%= product.quantity %></td> | ||
<td><%= product.comments %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
<div> | ||
<h3>Import a CSV File</h3> | ||
<%= form_tag import_products_path, multipart: true do %> | ||
<%= file_field_tag :file %> | ||
<%= submit_tag "Import CSV" %> | ||
<% end %> | ||
</div> |
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 |
---|---|---|
@@ -1,3 +1,8 @@ | ||
Rails.application.routes.draw do | ||
resources :products do | ||
collection { post :import } | ||
end | ||
|
||
root to: "products#index" | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
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,12 @@ | ||
class CreateProducts < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :products do |t| | ||
t.string :name | ||
t.integer :quantity | ||
t.decimal :price, precision: 12, scale: 2 | ||
t.string :comments | ||
|
||
t.timestamps | ||
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
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 @@ | ||
id,name,quantity,price,comments | ||
1,Guitar,20,199.99,none | ||
2,Trumpet,5,299.99,none | ||
3,Piano,3,699.99,none | ||
4,Clarinet,10,59.99,none |
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 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe ProductsController, type: :controller do | ||
|
||
describe "GET #index" do | ||
it "assigns @products" do | ||
product = Product.create | ||
get :index | ||
expect(assigns(:products)).to eq([product]) | ||
end | ||
|
||
it "renders the index template" do | ||
get :index | ||
expect(response).to render_template("index") | ||
end | ||
end | ||
|
||
describe "GET #import" do | ||
it "redirects upon CSV import success to root url with success message" do | ||
end | ||
|
||
it "redirects upon CSV import exception to root url with error message" do | ||
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,23 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Product, type: :model do | ||
|
||
describe 'Class' do | ||
subject { Product } | ||
|
||
it { should respond_to(:import) } | ||
|
||
let(:data) { "id,name,quantity,price,comments\r1,Guitar,20,199.99,none" } | ||
|
||
describe "#import" do | ||
it "should create a new record if id does not exist" do | ||
File.stub(:open).with("filename", {:universal_newline=>false, :headers=>true}) { | ||
StringIO.new(data) | ||
} | ||
Product.import("filename") | ||
expect(Product.find_by(name: 'Guitar').price).to eq 199.99 | ||
end | ||
end | ||
end | ||
|
||
end |