Code Monkey home page Code Monkey logo

rails_new's Introduction

rails_new ย  Tweet

Build Status

A thoughtfully designed template for building modern Rails apps ๐Ÿ”ฅ

Get started on your new app within minutes instead of hours ๐Ÿš—๐Ÿ’จ

Getting started

To get going clone this repository and perform the following steps:

  1. Run rails credentials:edit to re-generate config/master.key and create config/credentials.yml.enc.
  2. If your application requires ActiveStorage, run rails active_storage:install to generate a migration that creates the necessary tables. Use rails db:migrate to run the migration.
  3. You can now run bin/configure, which will help you in configuring the template.

As an alternative to running the script you can perform all of the following steps manually.

  1. Change application name in config/application.rb.
  2. Update database.yml to reflect the new application name.
  3. Update TODO items in config/environments/production.rb.
  4. If you plan on using Figaro, copy config/application.yml.example to config/application.yml.
  5. ESLint is preconfigured for modern JS with React support (using Prettier). If you want to use it install packages with npm install / yarn [install], otherwise remove .eslintrc and package.json.
  6. If you don't plan on tracking the template, you can remove the remote, otherwise rename it and add your new remote as appropriate.:
    $ git remote rename origin rails_new
    $ git remote add origin ...

Optional configuration options

  • If you want to use AirBrake, make sure the following 2 environment variables are set:
    • AIRBRAKE_PROJECT_ID
    • AIRBRAKE_API_KEY
  • New Relic is pre configured in config/newrelic.yml, but you need to comment in the environment variables for it work on Heroku (lines 10 and 17).
  • The app is preconfigured for Google Analytics, just add GOOGLE_ANALYTICS_ID to the environment.
  • We prefer to use vanilla Sidekiq for worker/queue management. If you prefer to use ActiveJob please see the configuration/options that were removed in 59cf38d.

Environment variables

Variable Comment
AIRBRAKE_PROJECT_ID Used in config/initializers/airbrake.rb
AIRBRAKE_API_KEY Used in config/initializers/airbrake.rb
BLOCK_HTTP_TRACE Disables HTTP TRACE method if set to true/t/1
BUNDLE_GEMFILE Useful when using a Gemfile.dev
DATABASE_URL Used for production env, automatically set by Heroku
GOOGLE_ANALYTICS_ID Will be added to the main application layout if set
HOST Your base URI, e.g. https://myapp.herokuapp.com
NEW_RELIC_APP_NAME Used in config/newrelic.yml
NEW_RELIC_LICENSE_KEY Used in config/newrelic.yml
PORT Port Puma will listen on, defaults to 3000
RACK_TIMEOUT_SERVICE_TIMEOUT Limit for Rack::Timeout, defaults to 15 seconds
RAILS_LOG_TO_STDOUT Set by Heroku Ruby buildpack, set manually on other platforms if needed
RAILS_MAX_THREADS Number of Puma threads, defaults to 5
REDIS_URL Used in config/cable.yml
WEB_CONCURRENCY Number of Puma workers. We default to threads only, no workers

Contents

All of the following have been installed and pre-configured:

Base system

  • Rails 5.2.1
  • Ruby 2.5.1
  • PostgreSQL >= 9.2
  • pg for ActiveRecord

General

Frontend

All of these are managed by yarn.

Development

Test

Rspec has been preconfigured for Rails 5.1+ system tests.

Production

Removed

The following default Rails gems have been removed:

Notes

Account vs User

Authentication concerns (your typical Devise configuration) are handled by the Account model. To connect this to one of several potential user roles the polymorphic authenticatable relationship is used.

Webpacker, the asset pipeline, and ES6

By default ES6 will not work for files in app/assets/javascript since Uglifier will fail to process them. This is why we applied the following change to config/production.rb, which allows you to use ES6 project wide:

-  config.assets.js_compressor = :uglifier
+  config.assets.js_compressor = Uglifier.new(harmony: true)

Also note that for everything to work properly on Heroku, you need to set up your buildpacks like this:

heroku buildpacks:clear
heroku buildpacks:set heroku/nodejs
heroku buildpacks:add heroku/ruby

Custom classes

  • ApplicationDecorator: lightweight alternative to Draper or similar gems.
  • ApplicationForm: Minimal form class based on ActiveModel.
  • ApplicationPresenter: a subclass of ApplicationDecorator for presenters, includes tag helpers.

All custom classes are fully documented with yard and come with generators.

Use yard doc to generate documentation and yard server --reload or yard server --gems to start a local documentation server.

Gemfile.dev / Gemfile.dev.lock

If you want to add specific gems for development that may not be interesting for other developers, you can add a Gemfile.dev (ignored by our .gitignore). Gems listed there can be installed with bundle install --gemfile Gemfile.dev and the resulting lock file is gitignored too.

Example Gemfile.dev:

source 'https://rubygems.org'

eval_gemfile 'Gemfile'

gem 'awesome_print'

The eval_gemfile line will ensure that all gems from your regular Gemfile will be included too. The BUNDLE_GEMFILE variable can be used to let Bundler now which gemfile to use:

BUNDLE_GEMFILE=Gemfile.dev rails c

Rack::RejectTrace middleware

There's a custom middleware (Rack::RejectTrace) for completely disabling the HTTP TRACE method as required by certain security audits. It can be enabled via the BLOCK_HTTP_TRACE environment variable.

Favicons

Favicons were generated with Real Favicon Generator, consider using the same tool when replacing them for your project.

Docker

For those wishing to use Docker for development the whole app has been dockerized and the setup is fairly well-documented. Features:

  • Images use Alpine Linux to keep their size small.
  • docker-compose.yml sets up and starts Postgres, Redis, Sidekiq, Rails and the Webpack dev server.
  • Uses a persistent bundle cache, so there's no need to rebuild the image to add gems.
  • Persistent volumes for Postgres and Redis.
  • No exposed ports except for Rails (mapped to port 3000 by default)

The following files relate to our Docker setup:

  • Dockerfile: main setup for the app container
  • docker-compose.yml: Ties Postgres, Redis and app together
  • docker-entrypoint.sh: Custom entry point to facilitate bundle caching
  • Procfile.docker: Used by the entrypoint script to bring up services
  • .dockerignore: similar to .gitignore, specifies files we don't want copied into the container

Usage examples:

Start the environment and build the images if necessary:

$ docker-compose up --build
Building app
[Step 1/12 : FROM ruby:2-alpine
 ---> 8302cc790fbf
Step 2/12 : RUN apk update && apk add --update --no-cache   build-base   chromium   chromium-chromedriver   git   imagemagick   libxml2-dev   libxslt-dev   nodejs   tzdata   postgresql-dev
 ---> Using cache
 ---> 9d1d0b398c26
Step 3/12 : RUN bundle config build.nokogiri --use-system-libraries
 ---> Using cache
 ---> 0a5ca06d7700
Step 4/12 : WORKDIR /app
 ---> Using cache
 ---> c61498ba7e64
[...]

Start the environment without (re-)building images (add -d to daemonize):

$ docker-compose up
Starting rails_new_postgres_1 ... done
Starting rails_new_redis_1    ... done
Starting rails_new_app_1      ... done
Attaching to rails_new_postgres_1, rails_new_redis_1, rails_new_app_1
[...]

Stop the environment but keep the containers:

$ docker-compose stop
Stopping rails_new_app_1      ... done
Stopping rails_new_postgres_1 ... done
Stopping rails_new_redis_1    ... done

Stop the environment and remove the containers:

$ docker-compose down
Stopping rails_new_app_1      ... done
Stopping rails_new_postgres_1 ... done
Stopping rails_new_redis_1    ... done
Removing rails_new_app_1      ... done
Removing rails_new_postgres_1 ... done
Removing rails_new_redis_1    ... done
Removing network rails_new_default

Execute a command inside the app container:

$ docker-compose exec app ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-musl]

Execute a command inside the app container that needs env variables:

$ docker-compose exec --env RAILS_ENV=test app rails db:setup
Created database 'rails_new_test'
-- enable_extension("plpgsql")
   -> 0.0251s
-- create_table("users", {:force=>:cascade})
   -> 0.0366s

Adding a new gem (does not require image rebuild):

# update Gemfile
$ docker-compose exec app bundle

Running specs:

$ docker-compose exec app rspec
....................

Finished in 0.47352 seconds (files took 16.36 seconds to load)
20 examples, 0 failures

TODO

Nothing right now.

License

This project is MIT licensed, see LICENSE.

rails_new's People

Contributors

citizen428 avatar cselmer avatar hiattp avatar janklimo avatar karunsiri avatar mkaschenko avatar roonglit avatar vvmarulin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.