/cocodrilo

A Rails template with some standard defaults

Primary LanguageRuby

Cocodrilo

This is the base application used at CROCODΞ. It uses Rails 5 to manage the backend. 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.

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.

Pre-requisites

Installation

Install the gem:

gem install cocodrilo

This will make the cocodrilo command accessible throughout your PATH.

Configuring your environment

PostgreSQL

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] local@user=# \password my_user_name

Generating your app

You can generate a new app with the following command:

cocodrilo myapp

Starting up your app

$ cd my_app_folder
$ foreman start

This command runs 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 Procfile.

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

App setup

Your team can use the following command after cloning the git repository:

bin/setup

Deploy

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 staging and production git remotes pointing to the respective Heroku remotes.

Health check

Check your app's health with the rake health command. It runs the following tasks:

  • rspec runs your Ruby and Rails specs.
  • If you app happens to be below 90% test coverage the rspec command will fail.
  • bundle-audit and brakeman check if your app does not have basic security holes.
  • rubocop makes sure your code adheres to style guidelines.

Testing

Ruby tests

Use the following command to run all your specs:

rspec

We use the following tools:

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 rails_helper.rb.
  • 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.

Application server

We use puma as our application server, which happens to be Heroku's default recommendation.

Background jobs

Our tool of choice is Sidekiq, which is configured as ActiveJob's backend. We include a sidekiq.yml configuration file with default settings, but you are encouraged to tune it to your application needs.

Debugging

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 bin folder.

Environment variables

  • Dotenv for loading environment variables

Credits

The template uses the Suspenders gem from Thoughtbot as starting point.

codeminer42