Goals of this tutorial:
- deploy a new Rails app with capistrano
- make it fast (total process takes less than 5 minutes)
- make it simple (no unecessary config)
- manual ssh to the server not required
Rails application stack:
- nginx
- unicorn
- postgresql
function parse_git_branch { | |
git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/' | |
} | |
function proml { | |
local BLUE="\[\033[0;34m\]" | |
local RED="\[\033[0;31m\]" | |
local LIGHT_RED="\[\033[1;31m\]" | |
local GREEN="\[\033[0;32m\]" | |
local LIGHT_GREEN="\[\033[1;32m\]" |
location ~ ^/assets/ { | |
expires 1y; | |
add_header Cache-Control public; | |
add_header ETag ""; | |
break; | |
} |
Goals of this tutorial:
Rails application stack:
# e.g.: | |
# @user.should have_ability(:create, for: Post.new) | |
# @user.should have_ability([:create, :read], for: Post.new) | |
# @user.should have_ability({create: true, read: false, update: false, destroy: true}, for: Post.new) | |
RSpec::Matchers.define :have_ability do |ability_hash, options = {}| | |
match do |user| | |
ability = Ability.new(user) | |
target = options[:for] | |
@ability_result = {} | |
ability_hash = {ability_hash => true} if ability_hash.is_a? Symbol # e.g.: :create => {:create => true} |
# in test.rb | |
require Rails.root.join('config/initializers/diagnostic') | |
config.middleware.use(MyApp::DiagnosticMiddleware) | |
# config/initializers/diagnostic.rb | |
module MyApp | |
class DiagnosticMiddleware | |
FILENAME = 'log/diagnostic.log' unless defined? FILENAME |
Hey! | |
Here is what I currently use: | |
Put this in your application helper: | |
def image_assets | |
image_asset_dir = Rails.root.join('app', 'assets', 'images') | |
assets = {} |
#!/bin/bash | |
# Fail if any commands fail | |
set -e | |
set -o pipefail | |
# These are ec2 tags from which we will determine where to deploy | |
ec2_stage=$DEPLOY_STAGE | |
ec2_app="core" | |
ec2_role="app" |
images = @album.image_assignments | |
changed = Array.new | |
# Get the images that changed order | |
for image in images | |
order = params["order_#{image.image_id}".to_sym] | |
if !order.blank? && order =~ /^\d+$/ && image.position.to_i != order.to_i | |
image.position = order.to_i | |
changed << image |
# Options: | |
# * :tries - Number of retries to perform. Defaults to 1. | |
# * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception. | |
# | |
# Example | |
# ======= | |
# retryable(:tries => 1, :on => OpenURI::HTTPError) do | |
# # your code here | |
# end | |
# |
# put in config/application.rb | |
config.to_prepare do | |
::PagesController.module_eval do | |
caches_page :show, :unless => proc {|c| c.user_signed_in? || c.flash.any? } | |
caches_page :home, :unless => proc {|c| c.user_signed_in? || c.flash.any? } | |
end | |
::Page.module_eval do | |
after_save :clear_static_caching! | |
after_destroy :clear_static_caching! |