项目作者: ltfschoen

项目描述 :
Upload/persist CSV file to PSQL then search, sort, paginate with AJAX
高级语言: Ruby
项目地址: git://github.com/ltfschoen/rails_csv_app.git
创建时间: 2017-03-07T02:05:33Z
项目社区:https://github.com/ltfschoen/rails_csv_app

开源协议:MIT License

下载


README

Screenshot

alt tag


Table of Contents


" class="reference-link">Instructions

  • Install System Requirements
  • Install Gems bundle install
  • Run PostgreSQL
  • Run Rails Server rails s
  • Go to http://localhost:3000
  • Click “Choose File” (to upload a CSV)
  • Select the products.csv file located in the root directory
  • Click “Import CSV”
  • Click the “1”, “2”, “Next” or “Previous” to change Page using Pagination
  • Click the column Labels (i.e. “Uid”, “Name”, “Price”, “Quantity”, “Released”) to filter ordering ascending/descending
  • Enter a value in the input field (case sensitive). Click “Search” to filter list.

" class="reference-link">Goals

  • [X] - Import pre-populated CSV into database from web form.
  • [X] - Present populated data from database in table view
  • [X] - Use AJAX and apply basic filters on table so data updated without refreshing whole page.
  • [X] - Use Sass instead of CSS
  • [X] - Add Bootstrap 4 styling for buttons and tables and Responsive grid
  • [X] - Generate Fake CSV Data
  • [X] - Add Images using LorelPixel
  • - Switch front-end to React.js or Angular.js instead of jQuery
  • - Add more Unit Tests

" class="reference-link">System Requirements and Info

  • Show System Setup
    rails about

  • Versions used:

    • Rails - 5.0.2
    • Ruby - 2.4.0-p0 (see .ruby-version)
    • RubyGems - 2.6.10
    • Ruby Gemset - rails_csv_app (see .ruby-gemset)
    • 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
    rails stats

" class="reference-link">Database

  • Run PostgreSQL without background service:
    pg_ctl -D /usr/local/var/postgres start
  • Configure to start PostgreSQL and restart at login using launchd background service:
    brew services start postgresql
  • Open PostgreSQL Database console automatically http://guides.rubyonrails.org/command_line.html
    rails dbconsole

  • Show database table contents

    1. select * from products;

" class="reference-link">Setup - Legacy Initial Steps

  • Create Project

    1. rails new rails_csv_app --database=postgresql
    2. rvm list
  • Install latest RVM to install and use latest Ruby version. Update PostgreSQL.

    1. rvm get master
    2. rvm install ruby-2.4.0
    3. brew upgrade bash
    4. brew update
    5. brew reinstall postgresql
    6. rvm reinstall ruby-2.4.0
    7. rvm use ruby-2.4.0
  • Update to latest RubyGems version https://rubygems.org/pages/download

    1. gem install rubygems-update
    2. update_rubygems
    3. 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.

    1. node -v
    2. npm install -g npm
    3. nvm install 7.7.1
    4. nvm use 7.7.1
  • Create custom Gemset with RVM

    1. rvm gemset create rails_csv_app
    2. 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

    1. rvm --ruby-version use 2.4.0@rails_csv_app
    2. gem install bundler
    3. bundle install
  • Migrate into PostgreSQL Database

    1. 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

    • Alternatively: Run server command, and then manually go to url, or in a separate tab run command to open app in browser
      rails s
      open http://localhost:3000

" class="reference-link">Setup - Replace Test Unit / Minitest with RSpec

  • 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

    1. rvm --ruby-version use 2.4.0@rails_csv_app
    2. bundle install
  • Initialise /spec directory
    rails generate rspec:install

  • Run RSpec tests
    rspec

" class="reference-link">Setup - Git Repo

  • 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

" class="reference-link">Setup - Git Release and Tags

" class="reference-link">Feature - CSV Upload and Display

CSV Setup

  • Create new Git branch

    1. git checkout -b feature/csv
  • Generate Model

    1. 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:

    1. resources :products do
    2. collection { post :import }
    3. end
    4. 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

    1. rails dbconsole
    2. select * from products;
    3. rake db:drop
    4. 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

    • Feature Release (non-production) i.e. v0.2

" class="reference-link">Feature - Search and Filter Data Uploaded from CSV with Pagination

" class="reference-link">Feature - Bootstrap

" class="reference-link">Feature - Fake CSV Generator

  • Faked CSV Gem https://github.com/jiananlu/faked_csv
    • Create file fake_csv_config.csv.json
    • Configure it to output CSV data in format desired and supports Faker Gem https://github.com/stympy/faker
    • Execute it with the following to generate random CSV:
      faked_csv -i fake_csv_config.csv.json -o products.csv
    • Insert the Labels at the top of the generated file, i.e.
      uid,name,quantity,price,comments,released_at
    • Convert image to display correctly