Git: See all unpushed commits or commits that are not in another branch
Local commits missing from a remote or upstream branch are easy to identify with git cherry -v, which lists the commits not shared by both branches.
Creating Liquid Layouts with Negative Margins
I was recently confronted with the task of creating a two-column liquid layout with a header and footer in which the content needed to come before the sidebar in the source code. I took opportunity to demonstrate an under-used aspect of CSS: negative margins. Negative margins allow us to push the content area away from the sides of the browser, leaving room for the sidebar.
Git: Define a custom merge driver
The ‘merge.*.driver` variable’s value is used to construct a command to run to merge ancestor’s version, current version and the other branches’ version. The merge driver is expected to leave the result of the merge in the file named with %A by overwriting it, and exit with zero status if it managed to merge them cleanly, or non-zero if there were conflicts.
Overriding unary operators in Ruby
This must be one of the coolest, yet quite unknown, technique in Ruby. For certain types of problems (e.g. when you have a set of rules) this gives you such an elegant way of describing the solution. There’s no meta programming or monkey patching involved, it’s short and sweet and best of all: it’s very intuitive.
Apache: Redirect all requests from one host to another
Redirecting all requests from one host to another preserves path and query string while avoiding duplicate hosts and broken links.
Word boundaries in MySQL regular expressions
MySQL lacks \b word boundaries in regular expressions, so word-start and word-end matching requires MySQL-specific markers instead.
Apache: Require username/password authentication except from a single IP, host or network
Apache Basic auth can stay enabled while a specific IP, host, or network bypasses login by combining access rules with Satisfy any.
Parametrized shared example groups in RSpec
Parameterized shared examples make reusable RSpec behavior checks accept arguments and blocks, avoiding duplicated expectations across similar specs.
Rails for Zombies
Learning Rails for the first time should be fun, and Rails for Zombies allows you to get your feet wet without having to worry about configuration. You'll watch five videos, each followed by exercises where you'll be programming Rails in your browser.
Export CSV from MySQL directly
If you need to export data from MySQL to a CSV, you can profit from really fast built-in methods.
This query writes the data to /tmp/geodb_coodinates.csv. And it's fast: Query OK, 337925 rows affected (0.86 sec)
SELECT * INTO OUTFILE '/tmp/geodb_coordinates.csv' FIELDS TERMINATED BY ',' ENCLOSED BY '"' ESCAPED BY '\\' LINES TERMINATED BY '\n' FROM geodb_coordinates;
Proper cross-browser CSS styling for buttons
Cross-browser button styling can vary with browser and OS defaults, especially for image-based designs. A small Sass reset removes inconsistent padding and borders in button elements.
Reset MySQL query cache
To clear the query cache in your MySQL database manually, e.g. for database profiling, execute the following command in your MySQL console:
RESET QUERY CACHE;
Git: Accessing lost commits
Amended, rebased, or reset commits are still reachable by SHA1 for a time; git reflog reveals recent branch positions before garbage collection removes them.
Apply a new callback to existing records
Run a newly added model callback for existing production records without replaying validations and unrelated callbacks; suitable for backfilling cached data after deployment.
Check if a field or button is disabled with Cucumber
Check whether form controls or buttons are disabled in Cucumber step definitions, using label-based assertions on Capybara or Webrat elements.
The Ruby Infinite Hash
How to create an infinitely nestable hash that always defaults to a new hash if a key does not map to a value.
Force net/http to verify SSL certificates
Ruby's net/http is setup to never verify SSL certificates by default. Most ruby libraries do the same. That means that you're not verifying the identity of the server you're communicating with and are therefore exposed to man in the middle attacks. This gem monkey-patches net/http to force certificate verification and make turning it off impossible.
ActiveRecord Table Transform
How to write to the db 27,000 times in 24 seconds instead of 9 minutes.
Test that an exception or error page is raised in Capybara
Capybara error-page assertions rely on the response status code and require @allow-rescue; Selenium @javascript scenarios do not support page.status_code.
Ruby: Checking if a class is a descendant of another class
Ruby classes can be tested for direct superclass, any ancestor, or instance compatibility with superclass, ancestors, and is_a?.
Check if two arrays contain the same elements in Ruby, RSpec or Test::Unit
Array equality without regard to order is awkward in Ruby tests; RSpec and Test::Unit provide matchers for matching elements, and plain Ruby needs custom comparison logic.
Encode or decode HTML entities
Convert accented and reserved characters to HTML entity references, or turn entity strings back into readable Unicode text, with the htmlentities gem.
Capistrano cowboy deploys
Sometimes, you just need to shoot from the hip…or deploy your local changes without committing them. Put this snippet from Jesse Newland in ~/.caprc and now you can cap cowboy deploy.
Use the contents of a WordPress database in your Rails app
These two models can be used to access the posts and associated comments of a WordPress database.