This is the base application used at Codeminer 42. It uses Rails 5 to manage the backend and Node.js / Webpack to manage the frontend. The purpose of this project is to:
- Provide a minimal and well-configured application generator.
- Improve some Rails defaults.
- Enforce code style guidelines.
- Provide basic security.
- Have Ruby and JS test frameworks configured out of the box with a few hand-picked tools.
- Provide opinionated defaults on infrastructure: Travis for CI and Heroku deploys working out of the box. We want apps to leverage the best Heroku has to offer, such as decreased devops maintenance burden, pipelines, review apps, etc.
This template does NOT aim to:
- Encourage having too many dependencies. Dependencies are not cheap, and each bundled tool must have a good reason to be included.
- Provide "pattern" gems (e.g. decorator). Patterns are usually project-specific and most of them can be achieved without any gems or libraries.
- Be a silver bullet. This is tailored for the majority of Rails applications you may want to build, but we know it won't work sometimes.
- Ruby >= 2.3.1
- Either rbenv / ruby-build or rvm
- Node.js >= 6.2.0
- Heroku Toolbelt
- Redis - primarily for use with Sidekiq
Install the gem:
gem install code42template
This will make the
code42template command accessible throughout your
Configuring your environment
development and test databases are automatically setup for you. While running the generator it's assumed that:
- The PostgreSQL server is running.
- You have a PostgreSQL user named after your UNIX login.
- Your PostgreSQL user has a blank password.
It's OK if your PG settings happen to be different from this; DB creation will fail, but you can do it manually thereafter.
At a basic level, here's how to setup PostgreSQL on Linux:
# Creates a user sudo -u postgres createuser -s my_user_name # Runs psql sudo -u postgres psql # Change your use password within psql. Leave it blank. [local] thiago@thiago=# \password my_user_name
Automatic Heroku setup is optional, but if you want to use it we assume you've
already logged in with your Heroku credentials. If you haven't, you'll need to
heroku login. Also remember to set the correct account if you happen to
use the multiple accounts plugin.
Generating your app
You can generate a new app with the following command:
To generate and configure your new app with Heroku:
code42template myapp --heroku true
Starting up your app
$ cd my_app_folder $ foreman start
This command runs Webpack dev server, Rails server and Sidekiq all at once.
Note that Your redis server has to be up and running because of Sidekiq. To
customize these processes you can edit
Now you can work as you'd usually work in any Rails application, with automatic Ruby and JS file reloads out of the box.
Basic hands-on guide
Your team can use the following command after cloning the git repository:
Deploy your app with the following command:
# Replace `MY_ENV` with `production` or `staging`. bin/deploy MY_ENV
This command pushes your code to Heroku, migrates your database and restarts
your dynos. It will work out of the box if you've generated your app with
--heroku true. If not, please create
production git remotes
pointing to the respective Heroku remotes.
Check your app's health with the
rake health command. It runs the following
rspecruns your Ruby and Rails specs.
- If you app happens to be below 90% test coverage the
rspeccommand will fail.
npm run test: runs JS unit and integration tests
brakemancheck if your app does not have basic security holes.
rubocopmakes sure your code adheres to style guidelines.
travis.yml file is also included: it runs the
rake health command, among
other setup tasks. You must still manually configure your remote repo with
Travis integration, though.
JS files must live at the
managed with NPM and the
You can write your JS code in ES2015 because of babel. ES2015 Import statements will automatically work without having to specify the full path.
We still use the asset pipeline for CSS.
Use the following command to run all your specs:
We use the following tools:
- capybara and database_cleaner
- poltergeist (for JS feature tests)
- simplecov for helping out with test coverage
Refer to the rspec-rails to learn which kinds of specs are available.
Note that you can require the following files to setup your tests:
- For light unit tests you can require
spec_helper.rb. It won't boot up the Rails environment.
- For tests needing Rails you can require
- For feature tests require
feature_helper.rb. It will compile your assets and make feature tests run correctly.
Regarding feature tests, they are configured to run seamlessly with Webpack.
tests are included in every app.
mochais used for unit tests. Unit tests must not depend on global JS objects such as
window. They must live at
npm test:unit npm test:unit:watch # runs tests automatically in every change
phantomjsare used for integration tests. That means you can use browser-only global JS objects such as
window. They must live at
npm test:integration npm test:integration:watch
Run all the tests (unit + integration) with the following command:
Debug your tests in the browser with webpack-dev-server. This command will output a URL where you can run all tests:
To run eslint over all JS files issue the following command:
npm run lint
The template comes with
sinon already pre-configured. Sinon is a popular
following ES6 import instruction:
import sinon from 'sinon';
Our tool of choice is Sidekiq, which is configured as ActiveJob's backend. We
sidekiq.yml configuration file with default settings, but you are
encouraged to tune it to your application needs.
Performance and Profiling
- rack-mini-profiler for helping out with performance issues
- spring for fast Rails actions via pre-loading
- bullet yeah, it's very easy to miss out N+1 queries, that's why we include this gem by default
Spring binstubs are automatically generated within the
- Dotenv for loading environment variables
The template uses the Suspenders gem from Thoughtbot as starting point.