Firefox: Remove dotted border from focused buttons

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.

Cucumber steps to test input fields for equality (with wildcard support)

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...

Inspect the page content in a Cucumber session

When you need to see the content of a page (i.e. not all the HTML but the relevant text body)

  • you can do pp (html_content)
    • pp will format the html String human readable pretty printed
  • where html_content can be replaced by one of the following commands:

Rails

body or response.body

Capybara:

  • page.driver.html.content
  • page.body

Webrat:

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...

Check if a field or button is disabled with Cucumber

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

Capybara

This step part of Spreewald.

Webrat

Then /^"([^\"]*)" should( not)? be disabled$/ do |label, negate|
  attributes = field_labeled(label).element.attributes.keys
  attributes.send(negate ? :should_not : :should...

Encode or decode HTML entities

Use the htmlentities gem.

Encoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "<élan>"
coder.encode(string)               # => "&lt;élan&gt;"
coder.encode(string, :named)       # => "&lt;&eacute;lan&gt;"
coder.encode(string, :decimal)     # => "&#60;&#233;lan&#62;"
coder.encode(string, :hexadecimal) # => "&#x3c;&#xe9;lan&#x3e;"

Decoding works like this:

require 'htmlentities'
coder = HTMLEntities.new
string = "&eacute;lan"
cod...

Virtual attributes for array fields

When a has_many association basically serves to store a list of associated strings (tags, categories, ...), it can be convenient to represent this association as a string array in the containing model. Here is an example for this pattern from the acts-as-taggable-on gem:

post = Post.last
p post.tag_list # ['foo', 'bar', 'baz']
post.tag_list = ['bam']
p post.tag_list # ['bam']

This string array tag_list is magical in several ways:

  • It is read from and written to a `has...

Virtual attributes for integer fields

Note that this card is very old. You might want to use ActiveType for your auto-coerced virtual attributes instead.


We sometimes give our models virtual attributes for values that don't need to be stored permanently.

When such a virtual attribute should contain integer values you might get unexpected behavior with forms, because every param is a string and you don't get the magic type casting that...

Useful collection of Sass mixins

This collection of Sass mixins enables cross-browser styling (including IE with CSS3PIE) with less lines of code.

This enables PIE for IE up to version 8 only (the first part is not possible in Haml, so use ERB):

<!--[if !IE]><!-->
  <%= stylesheet_link_tag 'screen', :media => 'screen' %>
<!--<![endif]-->
<!--[if lte IE 8]>
  <%= stylesheet_link_tag 'screen_with_pie', :media => 'screen' %>
<![endif]-->

These would be your two screen Sasses:

# screen_with_pie.sass

...

Aliases for routes

The following initializer provides an :alias => "my_route_name" option to restful routes in your route.rb. This simply makes the same route also available under a different ..._path / ..._url helpers.

For example,

map.resources :notes, :alias => :snippets

Gives you

notes_path, notes_url, new_note_path... #as always
snippets_path, snippets_url, new_snippet_path... #from the alias

Put this into an initializer:

Generate a Unicode nonbreaking space in Ruby

Regular spaces and non-breaking spaces are hard to distinguish for a human.
Instead of using the &nbsp; HTML entity or code like " " # this is an nbsp, use a well-named helper method instead.

def nbsp
  [160].pack('U*')
end

160 is the ASCII character code of a non-breaking space.

Request a non-HTML format in controller specs

If a controller action responds to other formats than HTML (XML, PDF, Excel, JSON, ...), you can reach that code in a controller spec like this:

describe UsersController do
  describe '#index' do
    it 'should be able to send an excel file' do
       # stubs and expectations go here
       get :index, :format => 'xls'
    end
  end
end

Remember that both the :format parameter and the HTTP_ACCEPT header can m...

Get the current layout's name in a view or partial

This returns the name (including path) of your current layout:

response.layout
=> "layouts/admin" # inside views that are using the 'admin' layout

You most likely do not need the full path, so go ahead and do this:

File.basename(response.layout)
=> "admin"

Use form_for without the enclosing form tag

In rare cases you might need something like form_for (for using form builder methods on the resulting block element) but without the surrounding form. One such case would be updating some of a form's fields via XHR.

You can simply use Rails' fields_for to do things like this in your views (HAML here):

- fields_for @user do |form|
  = form.label :email, 'E-Mail'
  = form.text_field :email

You will only receive the form content you gave, no hidden inputs incl...

Generate a path or URL string from an array of route components

When using form_for you can give the form's target URL either as a string or an array:

form_for(admin_user_path(@user)) do ... end
# same as:
form_for([:admin, @user]) do ... end

Same for link_to:

link_to("Label", edit_admin_user_path(@user))
# same as
link_to("Label", [:edit, :admin, @user])

polymorphic_path and polymorphic_url

If you would like to generate a path or URL string from an array of route components just as form_for does, you can use polymorphic_path or polymorphic_url:

polymorphic...

Match strings in a given order with Cucumber and Capybara

Sometimes the order in which strings appear on a page matters to you.

Spreewald gives you steps like these:

Then I should see in this order:
  | Alpha Group |
  | Augsburg    |
  | Berlin      |
  | Beta Group  |

Or, if you prefer multiline strings:

Then I should see in this order:
  """
  Alpha Group
  Augsburg
  Berlin
  Beta Group
  """

The step ignores all HTML tags and only tests on plain text.

Pay attention to the order of your submit buttons

If you have several submit elements (inputs or buttons with type="submit") that each cause different things to happen (e.g. you might have a button that sends an extra attribute) you might run into trouble when submitting the form by pressing the return key in a field.

When nothing fancy like a tabindex is defined it seems as if the first submit element inside a form is chosen (and has its attributes submitted) when pressing return.\
So, if possible, put your "default" (aka least harmful) submit element before others.

NB: If you s...

Places where cron jobs can hide

  1. In /etc/crontab
  2. In /etc/cron.d/*
  3. In /etc/cron.hourly/*
  4. In /etc/cron.daily/*
  5. In /etc/cron.weekly/*
  6. In /etc/cron.monthly/*
  7. In the personal crontab of any user. This is a magic file you can view with crontab -l and edit with crontab -e. You'll need to su to the respective user to view or edit her crontab.

Use Sass without Rails

You don't need a Rails application to use Sass. Even when you're working on a static site you can generate your CSS through Sass.

  • Install Sass with sudo gem install haml
  • Create a folder sass in the folder, that stores your stylesheets, e.g. mkdir css/sass
  • In a separate terminal window, run sass --watch css/sass:css. This will watch your sass files for changes and rewrite stylesheets as required.

This even works on Windows.

Note about your .gitignore

You might want to change our [typical .gitignor...

Define an array condition that selects on dynamic columns

For some reason you want to define a find condition in array form. And in that condition both column name and value are coming from user input and need to be sanitized.

Unfortunately this works in SQLite but does not in MySQL:

named_scope :filter, lambda { |attribute, value|
  { :conditions => [ 'articles.? = ?', attribute, value ] }
}

The solution is to use [sanitize_sql_array](http://apidock.com/rails/ActiveRecord/Base/sa...

Copying validation errors from one attribute to another

When using virtual attributes, the attached trait can be useful to automatically copy errors from one attribute to another.

Here is a typical use case where Paperclip creates a virtual attribute :attachment, but there are validations on both :attachment and :attachment_file_name. If the form has a file picker on :attachment, you would like to highlight it with errors from any attribute:

class Note < ActiveRecord::Base
  has_attached_file :attachment
  validates_attachment_presence :a...