Cancelling event propagation
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 likeclick
to be triggered. -
event.stopPropagation()
Prevents the event from bubbling up the DOM.
-
`event.st...
Acceptance testing using Capybara's new RSpec DSL
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).
Caching in Rails
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.
How to enable/disable caching
To enable or disable caching in rails you ca...
Liquid Canvas
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.
Upgrading Cucumber and Capybara to the latest versions available for Rails 2
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, `...
Customize your Bash prompt
The shell variable PS1
holds your bash prompt. You might want to change it to serve your needs best. Here is how to:
General
- non-printing escape sequences in your prompt have to be inclosed in
\[\e[
and\]
so your shell can correctly count its prompt's length - we recommend to highlight your prompt on production machines
- you can also [show different root prompts for each user](https://makandracards.com/makandra/9569-get-the-username-w...
Ruby and Rails deprecation warnings and how to fix them
Add deprecation warnings and their solution or link to available solutions.
Global access to Rake DSL methods is deprecated. Please include Rake::DSL into classes and modules which use the Rake DSL methods.
Open your Rakefile
and add the following line above YourApp::Application.load_tasks
:
YourApp::Application.class_eval do
include Rake::DSL
end
Use of ole/file_system is deprecated. Use ole/storage (the file_system api is recommended and enabled by default)...
JavaScript Garden
JavaScript Garden is a growing collection of documentation about the most quirky parts of the JavaScript programming language. It gives advice to avoid common mistakes, subtle bugs, as well as performance issues and bad practices that non-expert JavaScript programmers may encounter on their endeavours into the depths of the language.
JavaScript Garden does not aim to teach you JavaScript. Former knowledge of the language is strongly recommended in order to understand the topics covered in this guide
RSpec matcher to check if an ActiveRecord exists in the database
The attached RSpec matcher exist_in_database
checks if a given record still exists in the database and has not been destroyed:
describe Ticket do
describe '.purge_expired' do
fresh_ticket = Ticket.create(:expiry => Date.tomorrow)
expired_ticket = Ticket.create(:expiry => Date.yesterday)
Ticket.purge_expired
fresh_ticket.should exist_in_database
expired_ticket.should_not exist_in_database
end
end
Note that there is also [ActiveRecord::Base#destroyed?
](http://apidock.com/rails/ActiveRecord/Base/destroyed...
Synchronize a Selenium-controlled browser with Capybara
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...
Monitoring Theory
Around the time in my life when I stopped ordering drinks made with more than one ingredient, I was woken up for the last time by a hypochondriac Nagios monitoring installation. If you are on-call long enough, you cultivate a violent reaction to the sound of your cell phone's text message alert. If your monitoring is overconfigured, that reaction boils over hastily, as it will interrupt you during meals, sex, sleep — all of the basics — with the excruciating operational details of your web site.
I've since developed, with the help of some n...
Keyboard shortcuts for Pivotal Tracker
Pressing the ?
key will display a list of keyboard shortcuts. Available shortcuts are:
|search |/
|
|help|?
|
|add story|a
|
|toggle backlog|<Shift> + b
|
|toggle charts (graphs)|<Shift> + g
|
|toggle current|<Shift> + c
|
|toggle done|<Shift> + d
|
|toggle history|<Shift> + h
|
|toggle icebox|<Shift> + i
|
|toggle my work|<Shift> + w
|
|toggle labels & searches|<Shift> + l
|
This is awesome.
Convert Adobe Flash FLA files into HTML5 with Wallaby
"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.
Check that an element is hidden via CSS with Spreewald
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...
Add a prefix to form field IDs
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...
Exchange messages between Javascript and Flash
Flash movies (.swf files) can talk with Javascript code embedded in the same HTML page. There are two ways to do this:
- The preferred way is to use the ExternalInterface class to call Javascript functions from ActionScript, and to bind ActionScript functions to the Flash movie's DOM element so they can be called from Javascript.
- The deprecated way is to use the global [fscommand](http://help.adobe....
Simple database lock for MySQL
Note: For PostgreSQL you should use advisory locks. For MySQL we still recommend the solution in this card.
If you need to synchronize multiple rails processes, you need some shared resource that can be used as a mutex. One option is to simply use your existing (MySQL) database.
The attached code provides a database-based model level mutex for MySQL. You use it by simply calling
Lock.acquire('string to synchronize on') do
# non-th...
Continously run command under bash
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
hyphenator
Javascript that implements client-side hyphenation of HTML-Documents.
Using Firebug Lite to inspect HTML in Internet Explorer and other browsers
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...
OscarGodson/jKey - GitHub
jQuery plugin to register callback functions to keyboard shortkuts. Keyboard events in vanilla Javascripts are super-painful to work with, so hopefully this library can help.
Dynamic conditions for belongs_to, has_many and has_one associations
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 => '...
The Difference Between jQuery’s .bind(), .live(), and .delegate()
The difference between .bind(), .live(), and .delegate() is not always apparent. Having a clear understanding of all the differences, though, will help us write more concise code and prevent bugs from popping up in our interactive applications.
Perform HTTP basic authentication in Cucumber (with or without Selenium)
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
.
Capybara
The step definition is part of Spreewald. The step has been tested with multiple versions of Capybara, Rack::Test and Selenium.
Webrat (legacy)
This is a simpler version of the step above:
When /...