ActiveRecord gives you the :include
option to load records and their associations in a fixed number of queries. This is called preloading or eager loading associations. By preloading associations you can prevent the n+1 query problem that slows down a many index view.
You might have noticed that using :include
randomly seems to do one of the following:
It is common in Rails 3.0 applications that you want to provide default views for a group of controllers. Let’s say you have a bunch of controllers inside the Admin namespace and you would like each action to fallback to a default template. So if you are rendering the index action for Admin::PostsController and “app/views/admin/posts/index.html.*” is not available, it should then render “app/views/admin/defaults/index.html”.
Since Rails 3.0, we have a new abstraction called resolvers that holds the logic to find a template.
I prefer using Opera's "User mode" to toggle an outlining of HTML elements quickly. This helps greatly when you want to see the actual dimensions of elements, e.g. for floating elements inside containers, instead of opening up the Dragonfly inspector every time.
Navigate to View → Style → "Manage Modes..." and tick the checkboxes like in the attached image. Then, switch to the User Mode by pressing the shortcut (Shift+G for the 9.2-compatible layout or for the default layout with enabled "single-key shortcuts") and select "Outline" from...
Within an event handler, there are multiple methods to cancel event propagation, each with different semantics.
event.preventDefault()
Only prevents the default browser behavior for the click, i.e. going to a different url or submitting a form.
When invoked on a touchstart
event, this also prevents mouse events like click
to be triggered.
event.stopPropagation()
Prevents the event from bubbling up the DOM.
`event.st...
Back when Steak was first released, Capybara didn’t have any of the nice RSpec helpers it does now. A lot has changed since. Besides the helpers, it got its own RSpec acceptance testing DSL recently, essentially eating Steak’s functionality and turning it into a complete acceptance testing solution (on top of RSpec).
The information in this card is only relevant for Rails 2.3-era apps.
This note gives a quick introduction into caching methods (page caching, action caching and fragment caching) in rails and describes some specific problems and solutions.
The descriptions below are valid for Rails 2 and 3. Recently, caching with timestamp- or content-based keys has become more popular which saves you the pain of invalidating stale caches.
To enable or disable caching in rails you ca...
Liquid Canvas is a JavaScript library which allows you to draw inside an HTML canvas element with an easy yet powerful description language.
It can be used to add graphics to your web page without ever touching an image creation tool such as The Gimp, Inkscape or Photoshop.
Check out the Demo and the basic example and then download version 0.3.
Specify these gem versions in your Gemfile:
gem 'cucumber', '~> 1.3.0'
gem 'cucumber-rails', '= 0.3.2' # max version for Rails 2
gem 'capybara', '< 2' # capybara 2+ requires Rails 3
gem 'mime-types', '< 2' # dependeny of capybara
gem 'nokogiri', '< 1.6' # dependency of capybara
gem 'rubyzip', '< 1' # dependency of selenium-webdriver, rubyzip 1+ requires Ruby 1.9
gem 'cucumber_factory'
gem 'database_cleaner', '< 1'
gem 'cucumber_spinner', '~> 0.2.5'
gem 'launchy', '~> 2.1.2'
With these versions set, `...
When you click a link or a press a button on a Selenium-controlled browser, the call will return control to your test before the next page is loaded. This can lead to concurrency issues when a Cucumber step involves a Selenium action and a Ruby call which both change the same resources.
Take the following step which signs in a user through the browser UI and then sets a flag on the user that was just signed in:
Given /^the user "([^"]*)" signed in (\d) days ago$/ do |name, days|
visit new_session_path
fill_in 'Username', :w...
"Wallaby" is the codename for an experimental technology that converts the artwork and animation contained in Adobe® Flash® Professional (FLA) files into HTML. This allows you to reuse and extend the reach of your content to devices that do not support the Flash runtimes.
If you have content inside a page that is hidden by CSS, the following will work with Selenium, but not when using the Rack::Test driver. The Selenium driver correctly only considers text that is actually visible to a user.
Then I should not see "foobear"
This is because the Rack::Test driver does not know if an element is visible, and only looks at the DOM.
Spreewald offers steps to check that an element is hidden by CSS:
Then "foo" should be hidden
You can also check that an el...
If you use a form (or form fields) multiple times inside one view, Rails will generate the same id
attributes for fields again.
This card presents you with a way to call something like
- form_for @user, :prefix => 'overlay' do |form|
= form.text_field :email
and get this as HTML:
<input name="user[email]" id="overlay_user_email" (...) />
You can also put a :prefix
into a field's options. Note how only the id
but not the name
changes as we would not want to pass an overlay_user[email]
param to the controller. Sett...
Flash movies (.swf files) can talk with Javascript code embedded in the same HTML page. There are two ways to do this:
Sometimes you want to run a command forever, e.g. to compile a haml to html file on the console. Use this:
$ while(true) do haml index.haml index.html; sleep 1.5; done
Javascript that implements client-side hyphenation of HTML-Documents.
You know Firebug as a Firefox extension but there is also a "Lite" version which runs purely off JavaScript.
Though all major browsers offer inspection tools you may like the Firebug style. Also, for me this is a lot better than the IE8 developer tools -- and it works in older versions of IE, too.
Get the bookmarklet over at http://getfirebug.com/firebuglite#Stable. It usually loads the JavaScript code from a remote server but you can also download it to have it run locally. If adding the bookmarklet does not work in IE, add a new book...
Note: Consider not doing this. Use form models or vanilla methods instead.
The :conditions
option for Rails associations cannot take a lambda. This makes it hard to define conditions that must be evaluated at runtime, e.g. if the condition refers to the current date or other attributes.
A hack to fix this is to use faux string interpolation in a single-quoted :conditions
string:
class User < ActiveRecord::Base
has_many :contracts
has_one :current_contract, :class_name => 'Contract', :conditions => '...
This card describes a Cucumber step that lets you say:
When I perform basic authentication as "username/password" and I visit the admin area
The path component ("... the admin area") is parsed through your path_to
helper in features/support/paths.rb
.
The step definition is part of Spreewald. The step has been tested with multiple versions of Capybara, Rack::Test and Selenium.
This is a simpler version of the step above:
When /...
You cannot use Array#join
on an array of strings where some strings are html_safe
and others are not. The result will be an unsafe string and will thus be escaped when rendered in a view:
unsafe_string = '<span>foo</span>'
safe_string = '<span>bar</span>'.html_safe
[unsafe_string, safe_string].join(' ') # will incorrectly render as '<span>foo</span><span&t;bar</span>'
The solution is not to call html_safe
on the joined array and if you thought it would be, you [don't understand how XSS prot...
If you want your application to display properly on iPad, iPhone or Android there are two things to do:
Using the media
attribute on stylesheet
HTML tags allows you to have a CSS for mobile browsers:
<!--[if !IE]><!-->
<%= stylesheet_link_tag 'mobile', :media => 'only screen and (max-device-width: 1024px)' %>
<!--<![endif]-->
Here I chose 1024 pixels as the maximum device width to include the iPad. If you want to target only mobile phones, pick 960 to include high-end Android d...
The following Sass will do the trick:
button,
input[type="reset"],
input[type="button"],
input[type="submit"],
input[type="file"] > input[type="button"]
&::-moz-focus-inner
border: none
There's also a plain CSS version.
Note that you can no longer visually navigate through a form with the keyboard without these borders.
Our collection of the most useful Cucumber steps, Spreewald, now supports exact matching of form fields and lets you use wildcards.
Examples:
And the "Money" field should contain "134"
# -> Only is green if that field contains the exact string "134", neither "134,50" nor "1000134"
And the "Name" field should contain "*Peter*"
# -> Accepts if the field contains "Peter" or "Anton Peter" or "Peter Schödl" etc.
And the "Comment" field should contain "Dear*bye"
# -> Accepts if the field contains "De...
When you need to see the content of a page (i.e. not all the HTML but the relevant text body)
pp (html_content)
body
or response.body
page.driver.html.content
page.body
Nokogiri::HTML(response.body).content
The returned strings can be cleaned up by calling .gsub(/^\s*$/, '').squeeze("\n")
on them.\
Although this may be useful for d...
Using this step definition you can check if any form field (text field, checkbox, etc) or button is disabled:
Then the "Name" field should be disabled
And the "Save" button should be disabled
But the "Locked" field should not be disabled
This step part of Spreewald.
Then /^"([^\"]*)" should( not)? be disabled$/ do |label, negate|
attributes = field_labeled(label).element.attributes.keys
attributes.send(negate ? :should_not : :should...