Last active
August 29, 2015 14:02
-
-
Save dfasolin/97de0713ec65ab5c5fb8 to your computer and use it in GitHub Desktop.
Create a Rails Project from Zero
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Get RVM and configure it | |
New - $ curl -sSL https://get.rvm.io | bash -s stable | |
Existent - $ rvm get stable | |
$ rvm requirements | |
Mac - $ brew install libyaml | |
Linux - $ apt-get install libyaml-dev | |
Mac - $ rvm install 2.0.0 --with-openssl-dir=$HOME/.rvm/opt/openssl | |
Linux - $ rvm install 2.0.0 --with-openssl-dir=$HOME/.rvm/usr | |
$ rvm use 2.0.0@PROJECTNAME_rails_4_0 --create --default | |
Freeze the version to prevent conflicts! | |
$ gem update --system 2.1.9 | |
Creating a gem configuration file | |
$ subl ~/.gemrc | |
Suppressing the ri and rdoc documentation in .gemrc | |
install: --no-rdoc --no-ri | |
update: --no-rdoc --no-ri | |
Installing Rails | |
$ gem install rails --version 4.0.5 | |
Only for Linux install | |
$ sudo apt-get install libxslt-dev libxml2-dev libsqlite3-dev | |
Create the Project | |
$ rails new PROJECTNAME | |
Edit Gemfile | |
$ cd PROJECTNAME/ | |
$ subl Gemfile | |
Gemfile content | |
source 'https://rubygems.org' | |
ruby '2.0.0' | |
#ruby-gemset=railstutorial_rails_4_0 | |
gem 'rails', '4.0.5' | |
gem 'pg', '0.15.1' | |
gem 'bootstrap-sass', '2.3.2.0' | |
gem 'sprockets', '2.11.0' | |
gem 'bcrypt-ruby', '3.1.2' | |
group :development, :test do | |
gem 'rspec-rails', '2.13.1' | |
gem 'guard-rspec', '2.5.0' | |
gem 'spork-rails', '4.0.0' | |
gem 'guard-spork', '1.5.0' | |
gem 'childprocess', '0.3.6' | |
end | |
group :test do | |
gem 'selenium-webdriver', '2.35.1' | |
gem 'capybara', '2.1.0' | |
# Uncomment this line on OS X. | |
# gem 'growl', '1.0.3' | |
# Uncomment these lines on Linux. | |
gem 'libnotify', '0.8.0' | |
# Uncomment these lines on Windows. | |
# gem 'rb-notifu', '0.0.4' | |
# gem 'wdm', '0.1.0' | |
end | |
gem 'sass-rails', '4.0.1' | |
gem 'uglifier', '2.1.1' | |
gem 'coffee-rails', '4.0.1' | |
gem 'jquery-rails', '3.0.4' | |
gem 'turbolinks', '1.1.1' | |
gem 'jbuilder', '1.0.2' | |
group :doc do | |
gem 'sdoc', '0.3.20', require: false | |
end | |
group :production do | |
gem 'rails_12factor', '0.0.2' | |
end | |
Installing gems | |
$ bundle update | |
$ bundle install --without production | |
Dynamically generating a secret token. config/initializers/secret_token.rb | |
# Be sure to restart your server when you modify this file. | |
# Your secret key is used for verifying the integrity of signed cookies. | |
# If you change this key, all old signed cookies will become invalid! | |
# Make sure the secret is at least 30 characters and all random, | |
# no regular words or you'll be exposed to dictionary attacks. | |
# You can use `rake secret` to generate a secure secret key. | |
# Make sure your secret_key_base is kept private | |
# if you're sharing your code publicly. | |
require 'securerandom' | |
def secure_token | |
token_file = Rails.root.join('.secret') | |
if File.exist?(token_file) | |
# Use the existing token. | |
File.read(token_file).chomp | |
else | |
# Generate a new token and store it in token_file. | |
token = SecureRandom.hex(64) | |
File.write(token_file, token) | |
token | |
end | |
end | |
SampleApp::Application.config.secret_key_base = secure_token | |
Initializing Spork | |
$ spork --bootstrap | |
Adding environment loading to the Spork.prefork block. spec/spec_helper.rb | |
require 'rubygems' | |
require 'spork' | |
Spork.prefork do | |
ENV["RAILS_ENV"] ||= 'test' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'rspec/autorun' | |
# Requires supporting ruby files with custom matchers and macros, etc, | |
# in spec/support/ and its subdirectories. | |
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} | |
# Checks for pending migrations before tests are run. | |
# If you are not using ActiveRecord, you can remove this line. | |
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration) | |
RSpec.configure do |config| | |
# ## Mock Framework | |
# | |
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: | |
# | |
# config.mock_with :mocha | |
# config.mock_with :flexmock | |
# config.mock_with :rr | |
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures | |
config.fixture_path = "#{::Rails.root}/spec/fixtures" | |
# If you're not using ActiveRecord, or you'd prefer not to run each of your | |
# examples within a transaction, remove the following line or assign false | |
# instead of true. | |
config.use_transactional_fixtures = true | |
# If true, the base class of anonymous controllers will be inferred | |
# automatically. This will be the default behavior in future versions of | |
# rspec-rails. | |
config.infer_base_class_for_anonymous_controllers = false | |
# Run specs in random order to surface order dependencies. If you find an | |
# order dependency and want to debug it, you can fix the order by providing | |
# the seed, which is printed after each run. | |
# --seed 1234 | |
config.order = "random" | |
config.include Capybara::DSL | |
end | |
end | |
Spork.each_run do | |
# This code will be run each time you run your specs. | |
end | |
Configuring RSpec to automatically use Spork. .rspec | |
--colour | |
--drb | |
Initializing Guard | |
$ guard init spork | |
Edit Guardfile for Spork | |
require 'active_support/inflector' | |
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, | |
:rspec_env => { 'RAILS_ENV' => 'test' } do | |
watch('config/application.rb') | |
watch('config/environment.rb') | |
watch('config/environments/test.rb') | |
watch(%r{^config/initializers/.+\.rb$}) | |
watch('Gemfile') | |
watch('Gemfile.lock') | |
watch('spec/spec_helper.rb') { :rspec } | |
watch('test/test_helper.rb') { :test_unit } | |
watch(%r{features/support/}) { :cucumber } | |
end | |
guard 'rspec', all_after_pass: false, cli: '--drb' do | |
. | |
. | |
. | |
watch('config/routes.rb') | |
# Custom Rails Tutorial specs | |
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) do |m| | |
["spec/routing/#{m[1]}_routing_spec.rb", | |
"spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", | |
"spec/acceptance/#{m[1]}_spec.rb", | |
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : | |
"spec/requests/#{m[1].singularize}_pages_spec.rb")] | |
end | |
watch(%r{^app/views/(.+)/}) do |m| | |
(m[1][/_pages/] ? "spec/requests/#{m[1]}_spec.rb" : | |
"spec/requests/#{m[1].singularize}_pages_spec.rb") | |
end | |
watch(%r{^app/controllers/sessions_controller\.rb$}) do |m| | |
"spec/requests/authentication_pages_spec.rb" | |
end | |
. | |
. | |
. | |
end | |
Start Guard | |
$ guard | |
Adding a line for asset pipeline compatibility. config/application.rb | |
require File.expand_path('../boot', __FILE__) . | |
. | |
. | |
module SampleApp | |
class Application < Rails::Application | |
. | |
. | |
. | |
config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif) | |
end | |
end | |
Adding custom CSS | |
$ subl app/assets/stylesheets/custom.css.scss | |
custom.css.scss content | |
@import "bootstrap"; | |
/* mixins, variables, etc. */ | |
$grayMediumLight: #eaeaea; | |
@mixin box_sizing { | |
-moz-box-sizing: border-box; | |
-webkit-box-sizing: border-box; | |
box-sizing: border-box; | |
} | |
/* universal */ | |
html { | |
overflow-y: scroll; | |
} | |
body { | |
padding-top: 60px; | |
} | |
section { | |
overflow: auto; | |
} | |
textarea { | |
resize: vertical; | |
} | |
.center { | |
text-align: center; | |
h1 { | |
margin-bottom: 10px; | |
} | |
} | |
/* typography */ | |
h1, h2, h3, h4, h5, h6 { | |
line-height: 1; | |
} | |
h1 { | |
font-size: 3em; | |
letter-spacing: -2px; | |
margin-bottom: 30px; | |
text-align: center; | |
} | |
h2 { | |
font-size: 1.2em; | |
letter-spacing: -1px; | |
margin-bottom: 30px; | |
text-align: center; | |
font-weight: normal; | |
color: $grayLight; | |
} | |
p { | |
font-size: 1.1em; | |
line-height: 1.7em; | |
} | |
/* header */ | |
#logo { | |
float: left; | |
margin-right: 10px; | |
font-size: 1.7em; | |
color: white; | |
text-transform: uppercase; | |
letter-spacing: -1px; | |
padding-top: 9px; | |
font-weight: bold; | |
line-height: 1; | |
&:hover { | |
color: white; | |
text-decoration: none; | |
} | |
} | |
/* footer */ | |
footer { | |
margin-top: 45px; | |
padding-top: 5px; | |
border-top: 1px solid $grayMediumLight; | |
color: $grayLight; | |
a { | |
color: $gray; | |
&:hover { | |
color: $grayDarker; | |
} | |
} | |
small { | |
float: left; | |
} | |
ul { | |
float: right; | |
list-style: none; | |
li { | |
float: left; | |
margin-left: 10px; | |
} | |
} | |
} | |
/* miscellaneous */ | |
.debug_dump { | |
clear: both; | |
float: left; | |
width: 100%; | |
margin-top: 45px; | |
@include box_sizing; | |
} | |
Add page title helper in app/helpers/application_helper.rb | |
def full_title(page_title) | |
base_title = "PROJECTNAME" | |
if page_title.empty? | |
base_title | |
else | |
"#{base_title} | #{page_title}" | |
end | |
end | |
Default view/layout/application.html.erb | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title><%= full_title(yield(:title)) %></title> | |
<%= stylesheet_link_tag "application", media: "all", | |
"data-turbolinks-track" => true %> | |
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> | |
<%= csrf_meta_tags %> | |
<%= render 'layouts/shim' %> | |
</head> | |
<body> | |
<%= render 'layouts/header' %> | |
<div class="container"> | |
<%= yield %> | |
<%= render 'layouts/footer' %> | |
<%= debug(params) if Rails.env.development? %> | |
</div> | |
</body> | |
</html> | |
Default view/layout/_footer.html.erb | |
<footer class="footer"> | |
<small> | |
<a href="http://PROJECTNAME/">PROJECTNAME</a> | |
by Company | |
</small> | |
<nav> | |
<ul> | |
<li><%= link_to "About", about_path %></li> | |
<li><%= link_to "Contact", contact_path %></li> | |
</ul> | |
</nav> | |
</footer> | |
Default view/layout/_header.html.erb | |
<header class="navbar navbar-fixed-top navbar-inverse"> | |
<div class="navbar-inner"> | |
<div class="container"> | |
<%= link_to "PROJECTNAME", '#', id: "logo" %> | |
<nav> | |
<ul class="nav pull-right"> | |
<li><%= link_to "Home", root_path %></li> | |
<li><%= link_to "Help", help_path %></li> | |
<li><%= link_to "Sign in", '#' %></li> | |
</ul> | |
</nav> | |
</div> | |
</div> | |
</header> | |
Default view/layout/_shim.html.erb | |
<!--[if lt IE 9]> | |
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> | |
<![endif]--> | |
First time using Git | |
$ git config --global user.name "Your Name" | |
$ git config --global user.email [email protected] | |
$ git config --global alias.co checkout | |
$ git config --global core.editor "subl -w" | |
First time repository | |
$ git init | |
Edit .gitignore | |
$ subl -w ~/.gitignore | |
.gitignore content | |
# Ignore bundler config. | |
/.bundle | |
# Ignore the default SQLite database. | |
/db/*.sqlite3 | |
/db/*.sqlite3-journal | |
# Ignore all logfiles and tempfiles. | |
/log/*.log | |
/tmp | |
# Ignore other unneeded files. | |
database.yml | |
doc/ | |
*.swp | |
*~ | |
.project | |
.DS_Store | |
.idea | |
.secret | |
Automated tests with Guard | |
Add all files to Git | |
$ git add . | |
Commit | |
$ git commit -m "Initialize repository" | |
Rollback | |
$ git checkout -f | |
Configuring Github | |
http://github.com/new | |
After configuration, add remote repo and push files | |
$ git remote add origin https://github.com/USER/PROJECTNAME.git | |
$ git push -u origin master | |
Create a new branch | |
$ git checkout -b branch-NAME | |
After changes, add files and commit into new branch | |
$ git add . | |
$ git commit -m "Impoves into new branch" | |
Merge into master branch | |
$ git checkout master | |
$ git merge branch-NAME | |
Remove new branch | |
$ git branch -d branch-NAME | |
Push to GitHub | |
$ git push | |
Deploy to Heroku | |
$ heroku login | |
$ heroku create | |
$ git push heroku master | |
Rename Heroku to project name | |
$ heroku rename PROJECTNAME | |
Check Heroku Logs | |
$ heroku logs | |
Heroku commands | |
http://devcenter.heroku.com/heroku-command | |
------------------------------------------------ | |
Project using Scaffold | |
$ rails generate scaffold User name:string email:string | |
$ rake db:migrate | |
$ rails s | |
Project not using Scaffold | |
$ rails generate controller StaticPages home help --no-test-framework | |
Generate tests | |
$ rails generate integration_test static_pages | |
Code to test the contents of the Home page. spec/requests/static_pages_spec.rb | |
require 'spec_helper' | |
describe "Static pages" do | |
describe "Home page" do | |
it "should have the content 'Sample App'" do | |
visit '/static_pages/home' | |
expect(page).to have_content('Sample App') | |
end | |
end | |
end | |
Run test | |
rspec spec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment