Skip to content

Instantly share code, notes, and snippets.

@clairvy
Last active January 18, 2016 16:06
Show Gist options
  • Save clairvy/3caf9961a558d4695bb1 to your computer and use it in GitHub Desktop.
Save clairvy/3caf9961a558d4695bb1 to your computer and use it in GitHub Desktop.

how to run

make project

$ docker run --rm rails:4.2.5 rails new . -d mysql
$ git init
$ git add .
$ git commit -m 'initial revision'
$ cat docker-compose.yml
app:
    build: .
    volumes:
        - .:/usr/src/app
    ports:
        - 3000:3000
    links:
        - db
    env_file:
        - local.env
db:
    image: mysql:5.7.10
    env_file:
        - local.env
$ cat Dockerfile
FROM rails:onbuild
$ cat local.env
MYSQL_ROOT_PASSWORD=my-secret-pw
$ vi .gitignore
$ vi config/database.yml
$ git diff
diff --git a/.gitignore b/.gitignore
index 5b61ab0..748f216 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,3 +11,6 @@
 /log/*
 !/log/.keep
 /tmp
+
+# Ignore docker config
+/local.env
diff --git a/config/database.yml b/config/database.yml
index d709096..1f68536 100644
--- a/config/database.yml
+++ b/config/database.yml
@@ -14,8 +14,8 @@ default: &default
   encoding: utf8
   pool: 5
   username: root
-  password:
-  host: localhost
+  password: <%= ENV['MYSQL_ROOT_PASSWORD'] %>
+  host: db

 development:
   <<: *default
$ git add .
$ git commit -m 'append docker config files'
$ docker-compose run --rm app rake db:create
$ docker-compose up

other settings

$ vi Gemfile
$ git diff
diff --git a/Gemfile b/Gemfile
index 940b714..c78aeda 100644
--- a/Gemfile
+++ b/Gemfile
@@ -37,6 +37,10 @@ group :development, :test do
   gem 'byebug'
 end

+group :test do
+  gem 'minitest-reporters', '1.0.5'
+end
+
 group :development do
   # Access an IRB console on exception pages or by using <%= console %> in views
   gem 'web-console', '~> 2.0'
$ docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app ruby:2.2 bundle install
$ git add .
$ vi test/test_helper.rb
$ git diff
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 92e39b2..a4a9c04 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -1,6 +1,8 @@
 ENV['RAILS_ENV'] ||= 'test'
 require File.expand_path('../../config/environment', __FILE__)
 require 'rails/test_help'
+require "minitest/reporters"
+Minitest::Reporters.use!

 class ActiveSupport::TestCase
   # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
$ git add .
$ git commit -m 'append test coloring config'
$ vi Gemfile
$ git diff
diff --git a/Gemfile b/Gemfile
index c78aeda..9efe146 100644
--- a/Gemfile
+++ b/Gemfile
@@ -35,10 +35,12 @@ gem 'sdoc', '~> 0.4.0', group: :doc
 group :development, :test do
   # Call 'byebug' anywhere in the code to stop execution and get a debugger console
   gem 'byebug'
+  gem 'guard', '2.13.0'
 end

 group :test do
   gem 'minitest-reporters', '1.0.5'
+  gem 'guard-minitest', '2.4.4'
 end

 group :development do
$ docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app ruby:2.2 bundle install
$ git add .
$ docker-compose build
$ docker-compose run --rm app guard init
$ vi Guardfile
$ cat Guardfile
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
#  .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
#  $ mkdir config
#  $ mv Guardfile config/
#  $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Defines the matching rules for Guard.
guard :minitest, spring: true, all_on_start: false do
  watch(%r{^test/(.*)/?(.*)_test\.rb$})
  watch('test/test_helper.rb') { 'test' }
  watch('config/routes.rb')    { integration_tests }
  watch(%r{^app/models/(.*?)\.rb$}) do |matches|
    "test/models/#{matches[1]}_test.rb"
  end
  watch(%r{^app/controllers/(.*?)_controller\.rb$}) do |matches|
    resource_tests(matches[1])
  end
  watch(%r{^app/views/([^/]*?)/.*\.html\.erb$}) do |matches|
    ["test/controllers/#{matches[1]}_controller_test.rb"] +
    integration_tests(matches[1])
  end
  watch(%r{^app/helpers/(.*?)_helper\.rb$}) do |matches|
    integration_tests(matches[1])
  end
end

# Returns the integration tests corresponding to the given resource.
def integration_tests(resource = :all)
  if resource == :all
    Dir["test/integration/*"]
  else
    Dir["test/integration/#{resource}_*.rb"]
  end
end

# Returns the controller tests corresponding to the given resource.
def controller_test(resource)
  "test/controllers/#{resource}_controller_test.rb"
end

# Returns all tests for the given resource.
def resource_tests(resource)
  integration_tests(resource) << controller_test(resource)
end
$ git add .
$ git commit -m 'append guard config'

make controller

$ docker-compose run --rm app rails generate controller <controller name> <actions...>

make model

$ docker-compose run --rm app rails generate model <model name> <columns...>
$ docker-compose run --rm app rake db:migrate

run test

$ docker-compose run --rm app rake test

generate Gemfile.lock

$ docker run --rm -v $(pwd):/usr/src/app -w /usr/src/app ruby:2.2 bundle install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment