Trigger an event with Javascript
This is non-trivial because you need to fake event objects and require different code for different browsers. Luckily, there is tool support for most types of events.
In jQuery you can say:
$('a#close_window').click();
In Prototype you can use event.simulate.js from the Protolicious library to say:
$$('a#close_window')[0].simulate('click');
To trigger custom events with Prototype, you can use the [built-in Element.fire()
](http://api.prototypejs.org/dom/...
kangax's protolicious - GitHub
Added utility methods for the Prototype Javascript framework.
Cross-browser height and line-height
When using an odd value for line-height
in CSS the result will vary across all major browsers.\
Use an even line-height whenever possible.
When you are styling block elements you often apply both height
and line-height
to them. This Sass mixin helps you out with odd heights by always setting an even line height:
=height($height)
height: $height
line-height: floor($height / 2) * 2
So when you call +height(19px)
in Sass this will be the resulting CSS:
height: 19px
line-...
Retrieve the total number of records when paginating with will_paginate
When you use will_paginage to paginate a scope, and you want to obtain the total number of records matched before pagination, you can use total_entries
:
users = User.active.paginate
puts users.count # number of records on this page, e.g. 50
puts users.total_entries # total number of records before pagination, e.g. one billion trillion
This will trigger a second database query in order to retrieve the count (which will run anyway if you render any kind of pagination widget).
`wil...
Keep Pidgin up to date
If your pidgin IM fails to connect to ICQ, you may need to update it. The ubuntu default sources are usually outdated.
Just follow the instructions 1-4 and 1-3 on the page linked below.
Making SSL connection work
If you can not connect using SSL, open up the account settings and enable "clientLogin". Also, your Server should be set to slogin.icq.com
.
Making AJAX Applications Crawlable
This document describes an agreement between web servers and search engine crawlers that allows for dynamically created content to be visible to crawlers. Google currently supports this agreement. The hope is that other search engines will also adopt this proposal.
Problems with Rails 3 Remote Links and Forms Using jQuery .live() in IE
There is a problem with AJAX response handling for Rails 3 remote links and forms in Internet Explorer. This problem affects applications still using jQuery 1.4.2.
Git: Stash unstaged changes
This will stash all modifications that you did not git add
:
git stash -k
Note that newly created (and non-added) files will remain in your working directory unless you also use the -u
switch.
git stash -k -u
Also, your working directory must be clean (i.e. all changes need to be added) when you git stash pop
later on.
Bookmarklet to generate a commit message with Pivotal Tracker story ID and title
For clarity and traceability, your commit messages should include the ID and title of the Pivotal Tracker story you're working on. For example:
[#12345] Add Google Maps to user profiles
Optional further commit messages in the body
Also see Howto: Write a proper git commit message
To quickly generate such commit messages, add a new link "Commit" to your bookmarks and use the following Javascript as the link URL:
javascript:(function() { ...
scribd's flash_heed at master - GitHub
Fixes all Flash elements on a page so that they heed DOM stacking order
Change the id of an ActiveRecord record
You most likely never want to do this. But if you do:
Model.update_all({:id => new_id}, {:id => old_id})
Migrate or revert only some migrations
To only run the next two migrations:
rake db:migrate STEP=2
To revert the previous two migrations:
rake db:rollback STEP=2
To revert the last two migrations and then migrate to the most current version:
rake db:migrate:redo STEP=2
To migrate to a given migration number, regardless of whether that means migrating up or down:
rake db:migrate VERSION=20100627185630
To migrate exactly one individual migration out of the sequence* (careful):
rake db:migrate:up VERSION=20100627185630
To revert exactly one individual m...
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:
Git: "Interactive reverts"
If you need to revert only parts of one or several commits the following workflow can help:
If you need to revert multiple changes, make a branch and squash it afterwards.
For every commit you need to revert, starting at the most recent, do:
git revert -n [COMMIT] #revert, but don't automatically commit
... #fix any conflicts
git reset #unstage all changes
git add -p #this will ask you for every change, whethe...
Git diff a file with another revision (or branch)
git diff commit_hash -- path/to/file
Provide any commit hashes or branch names like "master
" for commit_hash
.
Install a specific version of a gem
To install webmock 1.5.0:
sudo gem install webmock --version "=1.5.0"
or
sudo gem install webmock -v "=1.5.0"
Prevent Bundler from downloading the internet
As a user of Bundler you have spent significant time looking at this message:
Fetching source index for http://rubygems.org/
To make Bundler skip this index update and only use installed gems, you can use the --local
option:
bundle install --local
Unfortunately this does not work with bundle update
.
It is said that Bundler 1.1 will use a feature of Rubygems.org that allows partial index updates. Hopefully the wh...
Generate a Unicode nonbreaking space in Ruby
Regular spaces and non-breaking spaces are hard to distinguish for a human.
Instead of using the
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.
Defining and using sub-classes with modularity
Given this class:
class Foo
class Bar
end
end
If you want to clean up this code with the modularity gem, you might try something like this:
class Foo
does 'bar'
end
module BarTrait
as_trait do
class Bar
end
end
end
Note that this changes Bar
's full class name from Foo::Bar
to BarTrait::Bar
. If you have methods inside Foo
(or other classes), you would have to change all references accordingly, which is quite unpleasant.
You can solve it like that:
`...
Install gems for all bundled projects
This is a bash script for those of you who need to install all gems for all projects (e.g. to get started quickly on a newly installed system).
Put it into your ~/bin/
and run it from the directory that holds your projects.
Note that, like the vanilla bundle install
, this will fail whenever a new gem compiles native components and requires a missing system dependency.
Select a random table row with ActiveRecord
Use this scope:
class Stick
named_scope :shuffled, lambda {
last_record = last
{ :conditions => [ 'id >= ?', rand(last_record.id) ] } if last_record
}
end
You can now pick a random stick by saying
Stick.shuffled.first
Or, if you prefer something smaller:
class Stick
named_scope :shuffled, :order => 'RAND()'
end
Note however that you should never order by RAND()
on tables that may become large some day, as this performs horribly and can kill your database server.
Test that a CSS selector is present with Cucumber
This note describes a Cucumber step definition that lets you test whether or not a CSS selector is present on the site:
Then I should see an element "#sign_in"
But I should not see an element "#sign_out"
Here is the step definition for Capybara:
Then /^I should (not )?see an element "([^"]*)"$/ do |negate, selector|
expectation = negate ? :should_not : :should
page.send(expectation, have_css(selector))
end
Here is the step definition for Webrat:
Then /^I should (not )?see an element "([^"]*)"$/ do |negate...
Bash Cheat Sheet (standard Emacs mode)
-
Ctrl + R Search commands you entered previously. Press Ctrl + R again to search further back, Ctrl + Shift + R searches forward again.
-
Ctrl + W Deletes from the cursor position to the left.
-
Ctrl + _ Undo. Yes, this also works with a German keyboard layout.
-
Ctrl + L Clear screen.
-
Ctrl + D _Close shell. (EOT, just like in many other shells.) Note: if you dove into another shell (e.g. with
sudo su username
) you will close it and return to ...