Zeus promises to make rails development faster
I am talking about development speed. When your application starts growing and you start adding gems, it starts to take really long to start up, be it the server, console or just running a single spec.
Zeus is smart, you don’t have to put it in your Gemfile or run it with Bundler, all you need to do is create a JSON config file via
zeus init
and then start the serverzeus start
.
After that, you’re ready to go, all you need to do is prefix every command with zeus. That means
rails server
becomeszeus server
, `rails console...
A jQuery plugin for producing bar charts from tables.
As the title says: this jQuery plugin creates bar charts from HTML tables. It comes in some different flavors.
Check the examples page: http://alphagov.github.com/magna-charta/.
Capybara 2.0 has been released
The gem author Jonas Nicklas highlights in a Google Groups post that the release
- is not backwards compatible to 1.x versions of Capybara
- does not support Ruby 1.8.x anymore
- removes confusion with Rails' built in integration tests (you put capybara rspec integration tests into the
spec/feature/...
folder) and the:type
metadata has been changed from:request
to:feature
- throws exceptions when trying to interact with an element whose identifier is...
How to set the default monospace font on Xfce (Xubuntu)
While you can set your own font in your terminal or other tools, it will not change the default "Monospace" font that some applications use.
To change that, edit ~/.fonts.conf
and add settings for the "monospace" family. Here is how it looks on my machine now:
<fontconfig>
<match target="pattern">
<test qual="any" name="family">
<string>monospace</string>
</test>
<edit name="family" mode="assign">
<string>DejaVu Sans Mono</string>
</edit>
</match>
</fontconfig>
...
Rails: When to use :inverse_of in has_many, has_one or belongs_to associations
When you have two models in a has_many
, has_one
or belongs_to
association, the :inverse_of
option in Rails tells ActiveRecord that they're two sides of the same association.
Example with a has_many
/ belongs_to
association:
class Forum < ActiveRecord::Base
has_many :posts, inverse_of: :forum
end
class Post < ActiveRecord::Base
belongs_to :forum, inverse_of: :posts
end
Knowing the other side of the same association Rails can optimize object loading so forum
and forum.posts[0].forum
will reference the same o...
Taming icon fonts for use in Rails views
Icon fonts like Font Awesome are infinitely scalable, look great on high-DPI displays and will give your app a modern look.
However, icon fonts can be very awkward to use compared to raster icons. Elements are given icons by giving them a special class like icon-plus
or icon-home
:
<span class="icon-plus">Create</span>
The icon font's stylesheet will then recognize this class and insert the icon as the element's :before
style.
In practic...
Internet Explorer will download CSS files twice, if referenced via scheme-less URLs
You can use scheme-less URLs (or protocol-relative URLs) to have browsers use the current protocol (HTTP or HTTPS) when loading content referenced with such an URL.
A protocol relative URL doesn’t contain a protocol. For example,
http://stevesouders.com/images/book-84x110.jpg
becomes//stevesouders.com/images/book-84x110.jpg
Browsers substitute the protocol of the page itself for the resource’s missing protocol. Problem solved!
But:
Internet Explorer 7 & 8 will download st...
Cucumber: Wait for any requests to finish before moving on to the next scenario
Background
Generally, Selenium tests use the browser to interact with the page. If it's unavailable, a timeout error is thrown.
Now, consider a scenario like this:
@javascript
Scenario: Receive an e-mail after clicking the fancy link
When I follow "fancy link"
Then I should have an e-mail with the subject "Hello"
When the last step in the scenario passes, you are done. Right? Wrong.
Why it's not enough
What if clicking our "fancy link" above sends the e-mail that we expect, but it also does stuff on the server...
Waiting for page loads and AJAX requests to finish with Capybara
If you're using the Capybara webdriver, steps sometimes fail because the browser hasn't finished loading the next page yet, or it still has a pending AJAX request. You'll often see workarounds like
When I wait for the page to load
Then ...
Workarounds like this do not work reliably, will result in flickering tests and should be avoided. There is no known reliable way to detect if the browser has finished loading the page.
Solution
Instead you should wait until you can observe the result of a page load. E.g. if y...
Regex pattern to validate email addresses
Our most recent pattern is
EMAIL = /\A[a-z0-9\+\-_\.]+@[a-z\d\-.]+\.[a-z]+\z/i
Notes
- Don't replace
[a-z0-9\+\-_\.]
with\w
! Otherwise the pattern would allowß
s and many other invalid characters. - The email address standard allows more patterns of emails than those which work in practice (e.g.
a@a
is valid). In result our pattern is more strictly. - Caution:
john..doe@example.com
andjohn.doe@example..com
are accepted as well.
Advice: Reduce scopes with joins to simple IN-queries
In theory you can take any scope and extend it with additional joins or conditions. We call this chaining scopes.
In practice chaining becomes problematic when scope chains grow more complex. In particular having JOINs
in your scope will reduce the scope's ability to be chained with additional JOINs
without crashes or side effects. This is because ActiveRecord doesn't really "understand" your scope chain, it only mashes together strings that mostly happen to look like a MySQL query in the end.
**I don't generally advice against u...
Cucumber pitfall: "Around" does not apply to your "Background" steps
Around
will not happen until after a feature's Background
has been processed. Use Before
and After
to avoid that.
Details
Consider this Cucumber feature file:
Feature: Something that needs to be tested
Background:
Given a user
And I sign in
Scenario: Sign out
When I sign out
Then I should see "Signed out"
Scenario: Something else
# ...
Now, assume you have these step definitions:
Around do
puts "** Around: before yield"
...
Manually requiring your application's models will lead to trouble
In a nutshell:
If you require your Rails models manually, pay attention to the path you use. Unless you have to, don't do it at all.
Background
Consider these classes:
# app/models/user.rb
class User < ActiveRecord::Base
validate :magic
def magic
errors.add_to_base('failed') if bad_things?
end
end
^
# app/models/foo.rb
require 'user'
class Foo
# something happens here
end
Now, when your environment is booted, Rails will automatically load your models, like User
...
grosser/rspec-instafail
Show failing specs instantly. Show passing spec as green dots as usual.
Configuration:
# spec/spec.opts (.rspec for rspec 2)
--require rspec/instafail
--format RSpec::Instafail
CSS: Emulate linear gradients with inset box shadows
Why is this useful?
- You can have a background image on the same element, overlaying it with a transparent gradient.
- Since you are probably using gradients to give an element a glossy or three-dimensional feel, box shadows work much better for this. This is because linear gradients always stretch to the full size of the element (which can grow with user input), while a natural shine or shadow only highlights a fixed size on the top or bottom.
- Browser support for linear gradients is a mess. I avoid using them. In part...
CSS: Emulate borders with inset box shadows
When is this useful?
- When both parent and child elements have borders, with this technique you don't get two borders (which looks ugly). This is because child elements are rendered over inset box shadows, not inside inset box shadows.
- You want more than one border on the same element. You can have as many inset box shadows on the same element as you like, e.g. allowing you to make a picture-frame-like border.
Examples
Remember the attribute list of box-shadow
is x-offset, y-offset, blur radius, shadow r...
RSpec: Change the type of a spec regardless of the folder it lives in
In a Rails application, *_spec.rb
files get special treatment depending on the file's directory. E.g. when you put a spec in spec/controllers
your examples will have some magic context like controller
, post
or get
that appears out of nowhere.
If you want that magic context for a spec in another folder, use the :type
option:
describe CakesController, :type => :controller do
...
end
mattheworiordan/capybara-screenshot
Takes a screenshot when you call it, or when a test fails.
Literally 101 Ruby tricks
The linked slidedeck holds many tips, of which I list the most interesting to me below
DATA and END
The __END__
keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA
object.
Running the attached example file data.rb
prints:
DATA is a File object to everything in the current file after "__END__"
No braces needed for 'special variables'
@instance, @@class, $global = [ 'instance', 'class', 'global' ]
puts "#@instance, #@@class, #$global"
...
How Ruby lets you keep script and data in *one* file
The __END__
keyword tells Ruby where a file ends – but you don't have to stop there. Any text you add after it is accessible via the DATA
object.
The attached example file data.rb
looks like this:
puts DATA.read
__END__
DATA is a File object to everything in the current file after "__END__"
Running it with ruby data.rb
prints:
DATA is a File object to everything in the current file after "__END__"
Geordi: Running Selenium tests in a VNC buffer
Geordi now supports our solution for running Selenium tests without having Firefox or Chrome windows popping up all over your workspace.
This will stop Selenium windows from appearing on your desktop, but you can still inspect them when necessary.
Installation
Update geordi with gem install geordi
.
Run geordi vnc --setup
and follow the instructions.
Usage
geordi cucumber
will automatically use VNC. Launchy will still open pages in the usual place.
geordi vnc
will allow...
Howto transfer a single mysql table between several deployment stages
Example task: Multiply the table holidays
between several stages.
-
Open two terminals:
shell-for stage_1 shell-for stage_2
-
Get the stage1 and stage2 MySQL credentials:
cat /opt/www/the_stage.host.tld/current/config/database.yml cat config/database.yml # should do it
-
Dump the table to a path reachable by the stage2 user (e.g. home):
mysqldump -h mysql1 -u stage_1_user -p stage_1_database table_name > ~/table_name_dump.mysql # Select certain records using --where "some_id > 666"
(-...
Asset pipeline may break Javascript for IE (but only on production)
If some of your JavaScripts fail on Internet Explorer, but only in staging or production environments, chances are that JavaScript compression is the culprit.
By default, Rails 3.2 compresses JavaScript with UglifyJS. I have seen a few cases where this actually breaks functioning JavaScript on IE (one example is the CKEditor).
I fixed this by switching to Yahoo's YUI Compressor.
To do this, do the following:
- replace the
uglifier
gem with theyui-compressor
gem...
Machinist blueprints do not work with a column :display
This didn't work for me. Seems display
is already taken in Machinist
.
# in spec/support/blueprints.rb
Partner.blueprint do
company_name
display { true }
end