Skip to content

Instantly share code, notes, and snippets.

@codespore
Created July 31, 2012 18:27
Show Gist options
  • Select an option

  • Save codespore/3219256 to your computer and use it in GitHub Desktop.

Select an option

Save codespore/3219256 to your computer and use it in GitHub Desktop.
Rails Application Development Cook Book
If repeating "FactoryGirl" is too verbose for you, you can mix the syntax methods in:
# rspec
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
end
# Test::Unit
class Test::Unit::TestCase
include FactoryGirl::Syntax::Methods
end
# Cucumber
World(FactoryGirl::Syntax::Methods)
----------------------------------------------------------------------------------------------------------------------------
Fish shell with Git Branch using Fish Nuggets (Reference: https://github.com/nirvdrum/fish_config)
----------------------------------------------------------------------------------------------------------------------------
1) brew install fish
2) cd ~/.config/fish
3) git init .
4) git add .
5) git commit -am "Defaults"
6) git remote add github git://github.com/zmalltalker/fish-nuggets.git
7) git pull github master
Alternatively go to http://ridiculousfish.com/shell/beta.html
----------------------------------------------------------------------------------------------------------------------------
Uninstalling gem that exist but kept saying gem is not installed:
----------------------------------------------------------------------------------------------------------------------------
1) gem list -d 'name of gem'
2) sudo gem uninstall 'name of gem' -i 'the path noted above'
----------------------------------------------------------------------------------------------------------------------------
Patch for RVM when using Fish Shell
----------------------------------------------------------------------------------------------------------------------------
1) Download the files into ~/.config/fish/functions
- http://noosfero.org/Development/DepsWithRVMAndGems
- https://github.com/eventualbuddha/fish-nuggets/commit/186775c746dd86cd94900269ee89e0c75e1db9d8
Reference: http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/
PROBLEM:
----------------------------------------------------------------------------------------------------------------------------
Your bundle is complete! It was installed into ./vendor/bundle
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
Please see this article for troubleshooting help:
http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting
SOLUTION: Add the following into your application.rb
----------------------------------------------------------------------------------------------------------------------------
config.assets.initialize_on_precompile = false
Reference: http://www.simonecarletti.com/blog/2012/02/heroku-and-rails-3-2-assetprecompile-error/
PROBLEM:
----------------------------------------------------------------------------------------------------------------------------
Your bundle is complete! It was installed into ./vendor/bundle
Cleaning up the bundler cache.
-----> Writing config/database.yml to read from DATABASE_URL
-----> Preparing app for Rails asset pipeline
Running: rake assets:precompile
rake aborted!
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
Tasks: TOP => environment
(See full trace by running task with --trace)
Precompiling assets failed, enabling runtime asset compilation
Injecting rails31_enable_runtime_asset_compilation
Please see this article for troubleshooting help:
http://devcenter.heroku.com/articles/rails31_heroku_cedar#troubleshooting
SOLUTION: Add the following into your application.rb
----------------------------------------------------------------------------------------------------------------------------
config.assets.initialize_on_precompile = false
require 'spec_helper'
describe Community do
let(:community) { create(:community, :end_on => nil) }
describe '.newsletter!' do
let!(:community_2) { create(:community) }
let!(:expired_community) { create(:community, :end_on => 10.days.ago) }
let!(:user_2) { create(:user, :community => community_2, :newsletter_me => true)}
subject { Community.newsletter! }
before { user_2.send(:confirm_and_verify!) }
it 'should send newsletters' do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
describe '#unit_list' do
subject { community.unit_list = "1\n2\n3\r\n"}
context 'when there are no duplicates' do
it 'should create units' do
expect { subject }.to change { community.units.count }.by(3)
end
end
context 'when there are duplicates' do
before { community.units.create(:name => '3') }
it 'should create units' do
expect { subject }.to change { community.units.count }.by(2)
end
end
end
describe '#storage_exceeded?' do
subject { community }
before { create(:attachment, :community => community) }
context 'when not exceed' do
before { community.update_attribute(:storage_limit_in_bytes, 1000.megabytes)}
it { should_not be_storage_exceeded }
end
context 'when exceed' do
before { community.update_attribute(:storage_limit_in_bytes, 1)}
it { should be_storage_exceeded }
end
end
describe '#expired?' do
subject { community.reload }
context 'when there are no subscriptions' do
it { should be_expired }
end
context 'when subscriptions are expired' do
before { create(:subscription, :end_on => 1.year.ago, :community => community)}
it { should be_expired }
end
context 'when there is one unexpired subscription' do
before { create(:subscription, :start_on => Date.today, :months => 12, :community => community) }
it { should_not be_expired }
end
end
describe '#host' do
subject { community }
context 'when there is custom domain' do
before { community.update_attributes({:subdomain => 'nadayu', :custom_domain => 'nadayu.com'}) }
its(:host) { should == 'nadayu.com' }
end
context 'when there is a subdomain' do
before { community.update_attributes({:subdomain => 'nadayu', :custom_domain => nil}) }
its(:host) { should == 'nadayu.highrisepro.com' }
end
end
describe 'sms credit reset' do
subject { community.reload }
before do
community.update_attributes(:sms_credits => 1, :default_sms_credits => 1001)
end
describe '.reset_sms_credits!' do
before { Community.reset_sms_credits! }
its(:sms_credits) { should == 1001 }
end
describe '#reset_sms_credits!' do
before { community.reset_sms_credits! }
its(:sms_credits) { should == 1001 }
end
end
describe '#news_feed' do
it 'should show bulletins & forum topics' do
subject { community }
bulletin = create(:bulletin, :community => community)
forum_topic = create(:forum_topic, :community => community)
community.news_feed.should eq([bulletin,forum_topic])
end
end
end
***************************************************************************************************************************
File Upload Tests using fixture_file_upload
***************************************************************************************************************************
context 'when name is not set' do
let(:attributes) { {:name => nil, :file => fixture_file_upload('/auto_file_name.png', 'image/png')} }
# fixture_file_upload is a shortcut for Rack::Test::UploadedFile.new(ActionController::TestCase.fixture_path + path, type)
# /auto_file_name.png located in "spec/fixtures/auto_file_name.png"
it 'should auto set the name' do
expect { subject }.to change { Attachment.find_all_by_name('auto_file_name').count }.by(1)
end
end
# Read about factories at https://github.com/thoughtbot/factory_girl
include ActionDispatch::TestProcess
FactoryGirl.define do
factory :lookup do
name "Collateral_Map"
remarks "Some sort of remarks"
lookup {fixture_file_upload(Rails.root.join('spec', 'fixtures/files', 'collateral_map.xls'), 'application/vnd.ms-excel')}
lookup_cache ""
user_id 1
end
end
Reference:
1) http://apidock.com/rails/ActionDispatch/TestProcess/fixture_file_upload
2) http://edgar.tumblr.com/post/2841931378/how-i-test-a-file-upload-in-rails-3
3) http://stackoverflow.com/questions/5990835/factory-with-carrierwave-upload-field
# http://blog.railsonfire.com/2012/05/06/Unicorn-on-Heroku.html
# http://michaelvanrooijen.com/articles/2011/06/01-more-concurrency-on-a-single-heroku-dyno-with-the-new-celadon-cedar-stack/
web: bundle exec unicorn_rails -p $PORT -c ./config/unicorn.rb
clock: bundle exec clockwork clock.rb
spin: bundle exec spin serve
smtp: bundle exec mailcatcher -f
worker: bundle exec rake jobs:work
# unicorn_rails -c /data/github/current/config/unicorn.rb -E production -D
# RAILS_ENV = ENV['RAILS_ENV'] || 'production'
if ENV['RAILS_ENV'] == 'production'
worker_processes 3
else
worker_processes 2
end
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
# Restart any workers that haven't responded in 30 seconds
timeout 30
# Listen on a Unix data socket
# listen '/data/github/current/tmp/sockets/unicorn.sock', :backlog => 2048
##
# REE
# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
before_fork do |server, worker|
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = Rails.root + '/tmp/pids/unicorn.pid.oldbin'
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
##
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
ActiveRecord::Base.establish_connection
# CHIMNEY.client.connect_to_server
# Redis and Memcached would go here but their connections are established
# on demand, so the master never opens a socket
end
development:
adapter: postgresql
database: highrisepro_development
pool: 5
username: adrian
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
database: highrisepro_test
pool: 5
username: adrian
timeout: 5000
production:
adapter: postgresql
database: highrisepro_production
pool: 5
username: adrian
timeout: 5000
1) Multiple line with single line split = CMD + SHIFT + L
2) Comment Lines = CMD + /
3) Delete Line = CMD + L + Backspace OR CTRL + K
4) New Fresh Line Below = CMD + RETURN
5) New Fresh Line Before = CMD + SHIFT + RETURN
6) Run Test in Sublime = CMD + SHIFT + T
7) Run Single Test in Sublime = CMD + SHIFT + R
References:
------------------------------------------------------------------------------------------------------------------------------
https://github.com/maltize/sublime-text-2-ruby-tests
1) Multiple line with single line split = CMD + SHIFT + L
2) Comment Lines = CMD + /
3) Delete Line = CMD + L + Backspace OR CTRL + K
4) New Fresh Line Below = CMD + RETURN
5) New Fresh Line Before = CMD + SHIFT + RETURN
6) Run Test in Sublime = CMD + SHIFT + T
7) Run Single Test in Sublime = CMD + SHIFT + R
References:
------------------------------------------------------------------------------------------------------------------------------
https://github.com/maltize/sublime-text-2-ruby-tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment