Upload/persist CSV file to PSQL then search, sort, paginate with AJAX
bundle install
rails s
Show System Setup
rails about
Versions used:
Show Codebase Stats
rails stats
pg_ctl -D /usr/local/var/postgres start
brew services start postgresql
Open PostgreSQL Database console automatically http://guides.rubyonrails.org/command_line.html
rails dbconsole
Show database table contents
select * from products;
Create Project
rails new rails_csv_app --database=postgresql
rvm list
Install latest RVM to install and use latest Ruby version. Update PostgreSQL.
rvm get master
rvm install ruby-2.4.0
brew upgrade bash
brew update
brew reinstall postgresql
rvm reinstall ruby-2.4.0
rvm use ruby-2.4.0
Update to latest RubyGems version https://rubygems.org/pages/download
gem install rubygems-update
update_rubygems
gem update --system
Update to latest JavaScript Runtime. Install NVM.
Check latest stable Node.js version https://nodejs.org
Check current version and update.
Install latest version of NPM.
node -v
npm install -g npm
nvm install 7.7.1
nvm use 7.7.1
Create custom Gemset with RVM
rvm gemset create rails_csv_app
rvm --ruby-version use 2.4.0@rails_csv_app
Check latest Rails version that is available: https://rubygems.org/gems/rails/versions
Install latest specific Rails version
gem install rails --version 5.0.2
Check database.yml is setup correctly for development
Check that using custom GemSet. Install default Gems in Gemfile
rvm --ruby-version use 2.4.0@rails_csv_app
gem install bundler
bundle install
Migrate into PostgreSQL Database
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:
bash launch.sh
rails s
open http://localhost:3000
Optionally run Test Unit one last time before sending it to oblivion
rake test
Remove Test Unit’s directory and files
rm -rf test/
Add RSpec to test group within Gemfile to retrieve latest patch https://github.com/rspec/rspec-rails
gem 'rspec-rails', '~> 3.5.2'
Check that using Custom GemSet. Install Gems
rvm --ruby-version use 2.4.0@rails_csv_app
bundle install
Initialise /spec directory
rails generate rspec:install
Run RSpec tests
rspec
Create new project on GitHub with MIT licence i.e. https://github.com/ltfschoen/rails_csv_app
Show remote branches for current repo
git remote -v
Set a remote URL using SSH
git remote add origin git@github.com:ltfschoen/rails_csv_app.git
Use Bulletproof Git Workflow to rebase with remote branch and get the MIT licence before pushing new changes
git pull --rebase origin master
Force push to remote branch to overwrite existing history
git push -f origin master
Create new Git branch
git checkout -b feature/csv
Generate Model
rails g model Product name:string quantity:integer price:decimal comments:string
Modify the migration file as follows:
t.decimal :price, precision: 12, scale: 2
Migrate
rake db:migrate RAILS_ENV=development
Generate Controller with index and import Actions
rails g controller Products index import
Modify Routes as follows:
resources :products do
collection { post :import }
end
root to: "products#index"
Update Product Model import function to accept CSV and process each row by
comparing with Product table of database, and either updating or creating new entry
Update Product Controller’s index action to fetch all Products to be available in view
as @products. Also update its import action to call the Product Model’s import function
passing a given file parameter as argument, and then redirecting user to the root url
Update Product’s index View to display list of products, including form allowing user to
upload the CSV by submitting form
Create a CSV file called products.csv
Run server and upload the CSV file, then check it exists in database. Drop database and re-migrate to further test
rails dbconsole
select * from products;
rake db:drop
rake db:create db:migrate RAILS_ENV=development
Add Unit Tests by adding the following gem to allow use of assigns
in Controller tests
gem 'rails-controller-testing', '~> 1.0.1'
Modify Unit Tests for Product Controller and Model
Create New Release https://github.com/ltfschoen/rails_csv_app/releases/new
Update to jQuery v3 and install jQuery UI Rails
//= require jquery3
//= require jquery_ujs
Add Willpaginate Gem
Since using latest version of Rails 5.0.2 and Ruby 2.4.0 it was
necessary to make the following key changes to the RailsCast #240 code that was
written for Rails 3 back in 2010, to make it run without error:
scope
with where(nil)
in app/models/product.rbparams.merge
with request.parameters
in app/helpers/application_helper.rbsort_column
with self.sort_column
, and sort_direction
with self.sort_direction
in app/helpers/application_helper.rbinclude ApplicationHelper
in app/controllers/products_controller.rbproduct_params[:search]
instead of just params[:search]
:
def product_params
params.permit(:id, :uid, :name, :price, :released_at, :search, :page, :sort, :utf8, :direction, :_)
end
.live
(deprecated) to .on
<% content_for :title, "Products" %>
instead of just <% title "Products" %>
Add Sass Rails Gem
fake_csv_config.csv.json
faked_csv -i fake_csv_config.csv.json -o products.csv
uid,name,quantity,price,comments,released_at