Test concurrent Ruby code
To test concurrent code, you will need to run multiple threads. Unfortunately, when you use blocking system calls (e.g. locks on the database), Ruby 1.8 threads won't work because system calls will block the whole interpreter.
Luckily you can use processes instead. fork
spins off a new process, IO.pipe
sends messages between processes, Process.exit!
kills the current process. You will need to take care of ActiveRecord database connections.
Here is a full-fledged example:
describe Lock, '.acquire' do
before :each do
...
Better Output for RSpec
rspec_spinner is a progress bar for RSpec which outputs failing examples as they happen (instead of all at the end).
Installation
gem install rspec_spinner
Usage
script/spec -r rspec_spinner -f RspecSpinner::Bar -c
To make a shortcut in your .bashrc
alias ss='script/spec -r rspec_spinner -f RspecSpinner::Bar -c'
There's also an alternate runner RSpecSpinner::Spinner
which shows a spinner and the name of the current spec instead of a progress bar.
Array.uniq does not work in Prototype
For arrays of objects, uniq
does not work as expected, since it uses strict equality. So
[[1], [1]].uniq() == [[1], [1]]
In some cases, this might be a workaround:
[[1], [1]].invoke("toJSON").uniq().invoke("evalJSON")
// == [[1]]
Concurrent Tests
Install gem and plugin
sudo gem install parallel
script/plugin install git://github.com/grosser/parallel_tests.git
Adapt config/database.yml
test:
database: xxx_test<%= ENV['TEST_ENV_NUMBER'] %>
Create test databases
script/dbconsole -p
CREATE DATABASE `xxx_test2`;
...
Generate RSpec files
script/generate rspec
(you'll probably only let it overwrite files in script/
)
Prepare test databases...
Parse XML or HTML with Nokogiri
To parse XML-documents, I recommend the gem nokogiri.
A few hints:
-
xml = Nokogiri::XML("<list><item>foo</item><item>bar</item></list>")
parses an xml string. You can also callNokogiri::HTML
to be more liberal about accepting invalid XML. -
xml / 'list item'
returns all matching nodes;list item
is used like a CSS selector -
xml / './/list/item'
also returns all matching nodes, but.//list/item
is now an XPath selector- XPath seems to be triggered by a leading
.
...
- XPath seems to be triggered by a leading
Rails - Multi Language with Fast_Gettext
sudo gem install gettext --no-ri --no-rdoc
sudo gem install fast_gettext --no-ri --no-rdoc
-
script/plugin install git://github.com/grosser/gettext_i18n_rails.git
(didn't work as gem) - environment.rb: see code example at the bottom
-
if this is your first translation:
cp locale/app.pot locale/de/app.po
for every locale you want to use - use method "_" like
_('text')
in your rails code - run
rake gettext:find
to let GetText find all translations used - translate messages in 'locale/de/app.po' (leave msgstr blank and ms...
Automatically build sprites with Lemonade
How it works
See the lemonade descriptions.
Unfortunately, the gem has a few problems:
- it does not work with Sass2
- it always generates all sprites when the sass file changes, which is too slow for big projects
- it expects a folder structure quite different to our usual
All these problems are solved for us, in our own lemonade fork. This fork has since been merged to the original gem, maybe we can use t...
Using Passenger for development (with optional SSL)
- install apache
sudo apt-get install ruby1.8-dev
sudo gem install passenger
sudo passenger-install-apache2-module
- follow the instructions
Manually: configure a vhost in /etc/apache2/sites-available
and link it to /etc/apache2/sites-enabled
with something like the following
^
NameVirtualHost *:80
<VirtualHost *:80>
ServerName application.local
DocumentRoot /opt/application/public
RailsEnv development
RailsAllowModRewrite off
</VirtualHost>
<VirtualH...
Revive a shell that is not working any more
If your shell seems frozen
You probably pressed Ctrl-S
which stops the buffer. Try Ctrl-Q
to resume.
If your shell behaves strangely (no input being displayed, strange character output)
Try typing this, followed by pressing the return key:
reset
That should clear the screen and reset output settings etc.
Convert Haml to ERB
This is about converting Haml to ERB and not the other way round which you probably want!
This process can not be automated 100%, but you can still save time.
First do
script/plugin install http://github.com/cgoddard/haml2erb.git
Then in the console type
hamls = Dir["app/views/**/*.haml"] - ['app/views/layouts/screen.html.haml'];
hamls.each do |haml|
puts haml
erb = haml.sub(/\.haml$/, '.erb')
File.open(erb, 'w') do |file|
file.write Haml2Erb.convert(File.read(haml))
end
end
After th...
Basic styles for flash notifications
.notice,
.error,
.information,
.warning {
font-weight: bold;
}
.notice {
color: #11bb00;
}
.error {
color: #F53A31;
}
.information {
color: #557;
}
.warning {
color: #d07d2d;
}
Start delayed_job in the background
This starts delayed_job, hiding all the output and hiding the process in the background.
rake jobs:work &>/dev/null &
How to install the date_performance gem
sudo gem install zip
git clone git://github.com/rtomayko/date-performance.git
cd date-performance
rake package:build
cd dist
sudo gem install --no-ri --no-rdoc date-performance-0.4.7.gem
Clear a Solr index with acts_as_solr
ActsAsSolr::Post.execute(Solr::Request::Delete.new(:query => "#{Location.solr_configuration[:type_field]}:#{ModelClass}"))
ActsAsSolr::Post.execute(Solr::Request::Commit.new)
Starting and stopping Solr
Solr listens to different ports for different environments.
Start Solr (and hide useless output)
rake solr:start PORT=8981 &>/dev/null
rake solr:start PORT=8982 &>/dev/null
Stop Solr
rake solr:stop PORT=8981
rake solr:stop PORT=8982
Run a single Cucumber feature
script/cucumber features/feature_name.feature
Or, if you don't care about speed, you can use rake:
rake features FEATURE=features/feature_name.feature
Install a gem without RI and RDdoc
To improve installation times of gems you can use the following approach:
gem install xyz --no-document
To permanently ignore ri
and rdoc
when installing gems, add this line to ~/.gemrc
:
gem: --no-document
Be aware that eliding local documentation may disable documentation support in your IDE.
Calling class methods
To call a class method (or static method) from an instance method:
class.method()
To call a class method from a class method:
method()
Fixing "A copy of Klass has been removed from the module tree but is still active"
This can happen during development when classes without automatic reloading are pointing to classes with automatic reloading. E.g. some class in lib
is calling Model.static_method
.
Workaround A
Stop referencing autoloaded classes from static files. If you can't, see workaround B and C.
Workaround B
Make sure the offending file (the one referencing the autoloaded class) is autoloaded, too. You may do this:
# config/application.rb
config.paths.add 'offending/file/parent/directory', eager_load: true
Workaroun...
Create an application with an older Rails version
sudo gem install rails --version="=1.2.3"
rails _1.2.3_ new-project-folder
Freeze a specific Rails version
sudo apt-get install unzip
rake rails:freeze:edge RELEASE=2.2.2
Show the current Git branch on your Bash prompt
Append this to your ~/.bashrc
:
export PS1='\[\033[01;32m\]\h\[\033[01;34m\] \w\[\033[31m\]$(__git_ps1 "(%s)") \[\033[01;34m\]$\[\033[00m\] '
Reload the changes by saying
source ~/.bashrc
For some customized prompts that also show the current Git prompt, see the examples section at the bottom of our bash prompt customizing card.
Run a single test in Test::Unit
To run a single test file:
rake test:units TEST=test/unit/post_test.rb
rake test:functionals TEST=test/functional/posts_controller_test.rb
rake test:integration TEST=test/integration/admin_news_posts_test.rb
You may even run a single test method:
ruby -I test test/unit/post_test.rb -n "name of the test"
ruby -I test test/functional/posts_controller_test.rb -n test_name_of_the_test # underscored, prefixed with 'test_'
Or all tests matching a regular expression:
ruby -I test test/integration/admin_news_posts_test.r...