diff --git a/Gemfile b/Gemfile index 5546b6d..1a4b8cb 100644 --- a/Gemfile +++ b/Gemfile @@ -37,6 +37,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri gem 'rspec-rails', '~> 3.5.2' + gem 'rails-controller-testing', '~> 1.0.1' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index f703db9..d062984 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -98,6 +98,10 @@ GEM bundler (>= 1.3.0, < 2.0) railties (= 5.0.2) sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.1) + actionpack (~> 5.x) + actionview (~> 5.x) + activesupport (~> 5.x) rails-dom-testing (2.0.2) activesupport (>= 4.2.0, < 6.0) nokogiri (~> 1.6) @@ -180,6 +184,7 @@ DEPENDENCIES pg (~> 0.18) puma (~> 3.0) rails (~> 5.0.0, >= 5.0.0.1) + rails-controller-testing (~> 1.0.1) rspec-rails (~> 3.5.2) sass-rails (~> 5.0) spring diff --git a/README.md b/README.md index a148363..cfe1586 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,16 @@ * [Setup - Legacy Initial Steps](#part-2000) * [Setup - Replace Unit Test with RSpec](#part-3000) * [Setup - Git Repo](#part-4000) + * [Setup - Git Releases and Tags](#part-5000) --- +## Goals + +* [X] - Import pre-populated CSV into database from web form. +* [X] - Present populated data from database in table view +* [ ] - Use AJAX and apply basic filters on table so data updated without refreshing whole page. + ## System Requirements and Info * Show System Setup `rails about` @@ -24,6 +31,7 @@ * Rack - 2.0.1 * Node.js - 7.7.1 (V8 runtime) * PostgreSQL - 9.6.2 + * RSpec - 3.5.4 * OS - macOS El Capitan * Show Codebase Stats @@ -38,6 +46,11 @@ * Open PostgreSQL Database console automatically http://guides.rubyonrails.org/command_line.html `rails dbconsole` +* Show database table contents + ``` + select * from products; + ``` + ## Documentation Links * Testing @@ -101,8 +114,7 @@ * Migrate into PostgreSQL Database ``` - rake db:create - rake db:migrate RAILS_ENV=development + rake db:create db:migrate RAILS_ENV=development ``` * Launch the Rails server in separate Terminal tab automatically and opens it in web browser after 10 seconds using Shell Script: @@ -149,4 +161,9 @@ `git pull --rebase origin master` * Force push to remote branch to overwrite existing history - `git push -f origin master` \ No newline at end of file + `git push -f origin master` + +## Setup - Git Release and Tags + +* Create New Release https://github.com/ltfschoen/rails_csv_app/releases/new + * Pre-Release (non-production) i.e. v0.1 \ No newline at end of file diff --git a/app/assets/javascripts/products.coffee b/app/assets/javascripts/products.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/products.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://coffeescript.org/ diff --git a/app/assets/stylesheets/products.scss b/app/assets/stylesheets/products.scss new file mode 100644 index 0000000..bff386e --- /dev/null +++ b/app/assets/stylesheets/products.scss @@ -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/ diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb new file mode 100644 index 0000000..35bc9c3 --- /dev/null +++ b/app/controllers/products_controller.rb @@ -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 diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..26bdec5 --- /dev/null +++ b/app/models/product.rb @@ -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 diff --git a/app/views/products/import.html.erb b/app/views/products/import.html.erb new file mode 100644 index 0000000..70052d6 --- /dev/null +++ b/app/views/products/import.html.erb @@ -0,0 +1,2 @@ +
Find me in app/views/products/import.html.erb
diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 0000000..91e312c --- /dev/null +++ b/app/views/products/index.html.erb @@ -0,0 +1,30 @@ +<%= flash[:notice] %> +Id | +Name | +Price | +Quantity | +Comments | +
---|---|---|---|---|
<%= product.id %> | +<%= product.name %> | +<%= product.price %> | +<%= product.quantity %> | +<%= product.comments %> | +