Understanding database cleaning strategies in tests
TLDR: In tests you need to clean out the database before each example. Use :transaction
where possible. Use :deletion
for Selenium features or when you have a lot of MyISAM tables.
Understanding database cleaning
You want to clean out your test database after each test, so the next test can start from a blank database. To do so you have three options:
- Wrap each test in a transaction which is rolled back when you're done (through
DatabaseCleaner.strategy = :transaction
or `config.use_transactional_fi...
Stub a request's IP address in a Cucumber scenario
The solution in this card is based on a stack overflow post by Leventix.
If you need to make request come from a fixed IP address for the duration of a Cucumber scenario, the code below lets you write this:
Given my IP address is 188.174.117.205
Rails 3
Given /^my IP address is "(.*?)"$/ do |ip|
ActionDispatch::Request.any_instance.stub(:remote_ip).and_return(ip)
end
Rails 2
-----...
How to reload a belongs_to association
To reload a single-item association in Rails 5+, call #reload_<association>
:
post.reload_author
In older Railses you can say
post.author(true)
How to fix: "rake db:rollback" does not work
When you run rake db:rollback
and nothing happens, you are probably missing the latest migration file (or have not migrated yet).
$ rake db:rollback
$
If that happens to you, check your migration status.
$ rake db:migrate:status
up 20160503143434 Create users
up 20160506134137 Create pages
up 20160517112656 Migrate pages to page versions
up 20160518112023 ********** NO FILE **********
When you tell Rails to roll back, it tries to roll back the latest change that was mi...
How to silence "I18n.enforce_available_locales" deprecation warnings
Before Rails 3.2.14, when supplying an invalid locale to I18n, it would fall back to its config.i18n.default_locale
(which is :en
by default). Eventually, this will be changed to raise an error by default -- for now, it shows a deprecation warning.
Since Rails 3.2.14 and 3.2.15 did not include security updates, you might not have applied them and probably now encounter these deprecation warnings after upgrading to 3.2.16 (or 4.0.2):
[deprecated] I...
Enable CSRF protection in Javascript tests
You might not know that Rails disables CSRF protection in tests. This means that if you accidentally forget to send the CSRF token for non-GET requests, your tests will be green even though your application is completely broken (a failed CSRF check usually logs out the user). Rails probably does this because CSRF protection sort of requires Javascript.
You want to enable CSRF protection in Cucumber scenarios that can speak Javascript. To do so, copy the a...
When Sass-generated stylesheets print a Encoding::CompatibilityError
We upgraded a Rails 2 application to Rails 3.2 and Ruby 2.1, changed the mysql adapter from mysql
to mysql2
, but did not activitate the asset pipeline. Instead we used Sass the old-school way (stylesheets in public/sass/*.sass
) and relied on stylesheet_link_tag
to activate the Sass compiler.
Now all Sass-generated stylesheets inserted the following text into body:before
:
Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT
I could get rid of this by removing all generated .css
files in `...
MySQL: Do not use "WHERE id IN (SELECT ....)"
Note: This applies specifically to MySQL. In PostgreSQL for example, this is not an issue.
If you care about performance, never use a query like
UPDATE users SET has_message = 1 WHERE users.id IN (SELECT user_id FROM messages)
MySQL does not optimize this and seems to scan the temporary table, which isn't indexed, for every row in the update statement. This applies to other statements than UPDATE
as well.
Instead, either use a JOIN
like
UPDATE users INNER JOIN messages ON messages.user_id = users.id SET has_message =...
Fix: "undefined method `bytesize' for #<Array>"
I believe that when WEBrick has trouble bringing up your Rails application, the WEBrick component that is supposed to print you a pretty error message has a bug and sometimes fails with this message:
"undefined method `bytesize' for #<Array>"
Starting the application in Passenger gave me a stacktrace in log/development.log
that pointed to the actual problem.
Possible causes discovered by looking at the logs
-----------------------------------------------------...
Even with bundler your gem order can be significant
Even when you're using bundler, it might be significant in which order your gems are listed in your Gemfile
. This can happen when gems are running around calling require
or require_dependency
on other gems or application classes when loaded (don't do that!).
A known culprit of this is the (otherwise wonderful) resource_controller gem, which requires ApplicationController
when loaded. When your ApplicationController
requires later-loaded gems when loaded, Rails will not boot.
He...
Aggregated RSpec/Cucumber test coverage with RCov
With defaults, RCov doesn't work the way you how you would like it to. To create a nice test coverage report, copy the attached file to lib/tasks/rcov.rake
. After that rake rcov:all
will run all RSpec examples and Cucumber features. The report will be written RAILS_ROOT/coverage/index.html
.
Here is what the task does in detail:
- Generates aggregated coverage of both RSpec and Cucumber
- Works with Rails 2 and Rails 3
- Reports for
app/**/*.rb
and nothing else - If called with an environment variable
IGNORE_SHARED_TRAITS=true
it ...
Using StaticMatic for static pages
Update: Staticmatic will not be further developed. They suggest to switch to middleman.
If you need to make a static web page and find yourself missing all your Rails comforts, take a look at StaticMatic.
This works like an extremely stripped down version of Rails, giving you
- HAML
- SASS
- helpers
- partials
When done, everything is simply compiled to s...
Alternative to #to_a and Array(...)
Note: In Rails 3+ you can use Array.wrap
instead of the solution here.
In the past you could use Array(...)
or #to_a
to turn an object into an array, unless it already is an array:
Array(5) # => [5]
Array([5]) # => [5]
Unfortunately this idiom has issues and it also triggers deprecation warnings like this one:
warning: default `to_a' will be obsolete
For an alternative you can copy the attached file to `config/i...
Stub methods on any instance of a class in Rspec 1 and Rspec 2
RSpec 1 (Rails 2)
With the most recent spec_candy.rb
helpers you can say:
User.stub_any_instance(:foo => :bar)
user = User.new
user.foo
# => :bar
RSpec 2 (Rails 3)
RSpec 2 comes with this feature built in:
User.any_instance.stub(:foo => :bar)
user = User.new
user.foo
# => :bar
RSpec 3
-------...
How to change the order of nested forms being rendered (especially blank forms)
Generally for nested forms, a blank form is placed below all other existing object forms. If you would like to change the position of the blank form(s) you can reorder the object's one-to-many association. For example you can put the blank form on top with the following snippet:
actors = movie.actors
actors.build
actors.unshift(actors.pop(1)) # won't work with Rails 4+
Because build_for_form
creates new objects and ap...
How to inspect controller filter chains in specs
Sometimes you need to look at the filter chain in specs. You can do it like that on Rails 2:
controller.class.filter_chain.map(&:method)
Note that we need to look at the controller's class since before_filter
and after_filter
stuff happens on the class level.
Also mind that the above code will give you all filters, both those run before and after an action. You can query before?
and after?
on the filter objects to scope down to only some of them:
controller.class.filter_chain.select(&:before?).map(&:method)
For Rails 3, ...
Unfreeze a frozen ActiveRecord
You can freeze any Ruby object to prevent further modification.
If you freeze an ActiveRecord and try to set an attribute you will an error like this:
can't modify frozen hash
This is because ActiveRecord delegates #freeze
to its attributes hash.
You can unfreeze most Ruby objects by creating a shallow copy of the frozen object by calling #dup
on it:
user = User.find(3)
user.freeze
unfrozen_user = user.dup
Notes for Rails 2 users
-----------------...
SSHKit 1.9.0 failure for Capistrano deploy
SSHKit 1.9.0 might fail with the following error, when trying to deploy a Rail application. Upgrading the gem to version 1.21.0 fixed the issue.
Traceback (most recent call last):
17: from /home/user/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sshkit-1.9.0/lib/sshkit/runners/parallel.rb:12:in `block (2 levels) in execute'
16: from /home/user/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sshkit-1.9.0/lib/sshkit/backends/abstract.rb:29:in `run'
15: from /home/user/.rbenv/versions/2.5.0/lib/ruby/gems/2.5.0/gems/sshkit-1.9....
Fix "couldn't parse YAML" error after upgrading Bundler
If you just upgraded to Bundler 10.0.10 you might get the following error when bringing up Rails:
/usr/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 17 column 14 (Psych::SyntaxError)
This is caused by Rails localization files (en.yml
, de.yml
, etc.) using symbols for various translation strings, and Bundler 10.0.10 defaults to a new YAML engine which cannot handle symbols.
You can switch back to the old YAML engine by ...
Minified JavaScript and CSS
JavaScripts and CSS should be minified for production use.
In Rails 3.1+ the asset pipeline will take care of this. Thus you're best off using an uncompressed version of your Javascript in development. Also load the non-minified versions of libraries. This way debugging will be easier and you will still get all the minification love once deployed.
In Rails 2.3 and 3.0 you should at least embed external JavaScript libraries in minified form, using something like JavaScript compressor.
How to render an html_safe string escaped
Once Rails knows a given string is html_safe
, it will never escape it. However, there may be times when you still need to escape it. Examples are some safe HTML that you pipe through JSON, or the display of an otherwise safe embed snippet.
There is no semantically nice way to do this, as even raw
and h
do not escape html_safe
strings (the former just marks its argument as html_safe
). You need to turn your string into an unsafe string to get the escaping love from Rails:
embed = javascript_tag('var foo = 1337;') # This is an h...
include_tags with the asset pipeline
You can include files from app/assets
or from the public
folder with javascript_include_tag
. The subtle difference that tells rails how to build the path correctly is a single slash at the beginning of the path:
<%= javascript_include_tag('ckeditor/config') %> # for assets/ckeditor/config.js
<%= javascript_include_tag('/ckeditor/ckeditor') %> # for public/ckeditor/ckeditor.js
This also applies to stylesheet_link_tag
.
Note that when you refer to a Javascript or stylesheet in /assets
you need to add it to [the list of asse...
Run specific version of bundler
You can specify the version of bundler
to execute a command (most often you need an older version of bundler, but don't want to uninstall newer ones):
bundle _1.0.10_ -v
Bundler version 1.0.10
An example is rails 3.2, which freezes bundler
at version ~> 1.0
:
Bundler could not find compatible versions for gem "bundler":
In Gemfile: rails (~> 3.2) was resolved to 3.2.0, which depends on bundler (~> 1.0)
Current Bundler version: bundler (1.13.6)
You can solve this with:
gem install bundler -v 1....
HTML5: Allow to choose multiple files with a vanilla file picker
Modern browsers natively suppport file pickers that allow the user to choose multiple files at once. To activate this feature, set the multiple
attribute:
<input type="file" name="images[]" multiple />
Or in a Rails view:
<%= file_field_tag "images[]", multiple: true %>
This works in IE10+.
Make sure that the field name ends in []
so your server-side code will parse the incoming files into an array. Obviously this naming convention is not compatible with default Rails nested attribute setters, so you'll need to write a form ...