├── LICENSE.txt ├── README.md ├── template └── template.rb └── test.sh /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021-present, Jason Swett 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Less time installing and troubleshooting, more time building 2 | 3 | ## What is Instant Rails? 4 | 5 | As easy as it is to set up a Rails project, it could be easier. 6 | 7 | When you run `rails new`, it does a lot for you, but it doesn't do everything. 8 | 9 | Rails will install gems for you but it won't install PostgreSQL, Redis, or any other dependencies that you need on your machine but which **don't come in a gem**. You have to manually install those dependencies yourself. And those manual installations can be a pain. ("Can't find libgq-fe.h header", anyone?) 10 | 11 | This is the problem that Instant Rails solves. **Instant Rails eliminates the need to manually install your development environment's dependencies.** 12 | 13 | How does it do this? 14 | 15 | When you create a new project using Instant Rails, you get a small **Docker configuration file** inside your project. If you're not familiar with Docker, don't be scared off. You do NOT need to understand Docker in order to use Instant Rails. 16 | 17 | All the Docker configuration file does is says "Hey, instead of using a database on the local machine, we want use _this_ Dockerized database instead." 18 | 19 | You're free to ignore the fact that the database is Dockerized. You can use your Dockerized database just like a "normal" database. 20 | 21 | The only difference you'll notice is that **you'll save a bunch of time and frustration by not having to manually install development dependencies**. 22 | 23 | # How to use Instant Rails 24 | 25 | ## Prerequisites 26 | 27 | In order to use Instant Rails, you must have Docker, Ruby and the `rails` gem installed. 28 | 29 | ## Usage instructions 30 | 31 | To create a new Rails app using Instant Rails, run the following command: 32 | 33 | ```bash 34 | rails new my_app_name \ 35 | -T \ 36 | -d postgresql \ 37 | -m https://raw.githubusercontent.com/jasonswett/instant_rails/main/template/template.rb 38 | ``` 39 | 40 | This will create a Rails application for you. Once the script finishes, you can `cd` into the project directory, run `rails server` and see your app running. 41 | 42 | # Get help 43 | 44 | To get help, just open an issue or email me at jason@codewithjason.com. 45 | 46 | # Acknowledgements 47 | 48 | This project was inspired in part by the late Mike Rogers' [Docker/Rails template](https://github.com/Ruby-Starter-Kits/Docker-Rails-Template). 49 | -------------------------------------------------------------------------------- /template/template.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | def source_paths 4 | [__dir__] 5 | end 6 | 7 | def instant_rails_file(filename, content) 8 | file filename, content, force: true 9 | end 10 | 11 | def random_port 12 | rand(10_000) + 10_000 13 | end 14 | 15 | instant_rails_file "config/initializers/generators.rb", <<-CODE 16 | Rails.application.config.generators do |g| 17 | g.orm :active_record, primary_key_type: :uuid 18 | g.stylesheets false 19 | g.helper false 20 | g.serializer false 21 | g.jbuilder false 22 | 23 | g.test_framework :rspec, 24 | fixtures: false, 25 | view_specs: false, 26 | helper_specs: false, 27 | routing_specs: false, 28 | request_specs: false, 29 | controller_specs: false 30 | end 31 | CODE 32 | 33 | File.open(".gitignore", "a") do |f| 34 | f.puts "\n.env" 35 | end 36 | 37 | instant_rails_file ".env", <<~CODE 38 | DATABASE_HOST=127.0.0.1 39 | DATABASE_PORT=#{random_port} 40 | REDIS_PORT=#{random_port} 41 | CODE 42 | 43 | gem "paranoia" 44 | 45 | gem "audited" 46 | after_bundle { rails_command("generate audited:install") } 47 | after_bundle { rails_command("db:migrate") } 48 | 49 | gem "devise" 50 | after_bundle { rails_command("generate devise:install") } 51 | after_bundle { rails_command("generate devise users") } 52 | after_bundle { rails_command("db:migrate") } 53 | 54 | gem_group :development, :test do 55 | gem "dotenv-rails" 56 | gem "capybara" 57 | gem "factory_bot_rails" 58 | gem "faker" 59 | gem "rspec-rails" 60 | end 61 | after_bundle { rails_command("generate rspec:install") } 62 | 63 | instant_rails_file "config/database.yml", <<-CODE 64 | default: &default 65 | adapter: postgresql 66 | encoding: unicode 67 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 68 | username: #{@app_name} 69 | host: <%= ENV.fetch("DATABASE_HOST") %> 70 | port: <%= ENV.fetch("DATABASE_PORT") %> 71 | 72 | development: 73 | <<: *default 74 | database: #{@app_name}_development 75 | 76 | test: 77 | <<: *default 78 | database: #{@app_name}_test 79 | 80 | production: 81 | <<: *default 82 | database: #{@app_name}_production 83 | username: #{@app_name} 84 | password: 85 | CODE 86 | 87 | instant_rails_file "docker-compose.yml", <<-CODE 88 | --- 89 | version: "3.8" 90 | 91 | services: 92 | postgres: 93 | image: postgres:13.1-alpine 94 | volumes: 95 | - postgresql:/var/lib/postgresql/data:delegated 96 | - ./init.sql:/data/application/init.sql 97 | ports: 98 | - "127.0.0.1:${DATABASE_PORT}:5432" 99 | environment: 100 | PSQL_HISTFILE: /usr/src/app/log/.psql_history 101 | POSTGRES_USER: #{@app_name} 102 | POSTGRES_HOST_AUTH_METHOD: trust 103 | restart: on-failure 104 | healthcheck: 105 | test: ["CMD-SHELL", "pg_isready -U #{@app_name}"] 106 | interval: 10s 107 | timeout: 2s 108 | retries: 10 109 | logging: 110 | driver: none 111 | 112 | redis: 113 | image: redis:4.0.14-alpine 114 | volumes: 115 | - redis:/data:delegated 116 | ports: 117 | - "127.0.0.1:${REDIS_PORT}:6379" 118 | restart: on-failure 119 | logging: 120 | driver: none 121 | 122 | volumes: 123 | postgresql: 124 | redis: 125 | CODE 126 | 127 | file "init.sql", "CREATE USER #{@app_name};" 128 | 129 | run "docker compose up -d" 130 | run "bin/setup" 131 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | rails new $1 -T \ 2 | -d postgresql \ 3 | -m template/template.rb 4 | --------------------------------------------------------------------------------