Common Screen Resolutions
Target layouts for common notebook, desktop, netbook and iPad screen sizes to reduce clipping and horizontal scrolling.
Load all models into an Array
Dir.glob(File.join RAILS_ROOT, 'app', 'models', '*.rb').collect{ |path| path[/.+\/(.+).rb/,1] }.collect(&:camelize).collect(&:constantize)
Cucumber Webrat steps
Custom Cucumber step definitions for checking fields, HTML content, regex matches, and table data in Webrat-based tests.
Reprocess only missing images with Paperclip
Adding a new image variant to many Paperclip attachments can trigger costly full reprocessing; patching Paperclip::Attachment skips styles that already exist.
Convert colorspace of images attached with Paperclip
has_attached_file(
:avatar,
:styles => { :large => "300x300", :small => "100x100" },
:convert_options => { all => "-colorspace RGB" }
)
Reprocess Paperclip attachments in one line
script/runner -e development 'Article.all.each { |a| a.image.reprocess! if a.image.exists? }; "done"'
Release gem; Deploy gem; Update a gem created with Jeweler
Publishing an older Jeweler-based gem can require version bumps, gemspec regeneration, tagging, and sometimes a manual gem push to RubyGems.org.
Aggregated RSpec/Cucumber test coverage with RCov
RCov defaults fail to aggregate RSpec and Cucumber coverage cleanly; a custom rake rcov:all task produces a Rails report for app/**/*.rb only.
Better output for Cucumber
cucumber_spinner adds a progress bar for Cucumber features and shows failing scenarios immediately, with optional browser opening for failed pages.
Test concurrent Ruby code
Concurrent Ruby code with blocking system calls can fail under Thread-based tests; using forked processes, pipes, and separate ActiveRecord connections keeps synchronization tests reliable.
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
Parsing XML and HTML with Nokogiri needs the right selector syntax; namespaces can make XPath queries miss nodes unless prefixes are used.
Rails - Multi Language with Fast_Gettext
Rails localization with fast_gettext enables multi-language apps using .po files, _() and n_() for translated text and plural forms.
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)
Run a Rails app locally with Apache and Passenger for development, including optional HTTPS using a self-signed certificate.
Revive a shell that is not working any more
A frozen or garbled shell often recovers with Ctrl-Q after accidental Ctrl-S, or with reset when terminal output settings are corrupted.
Convert Haml to ERB
Haml markup can be migrated to ERB with haml2erb, but the conversion is only partly automated and still needs manual cleanup.
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)