Compare two XML strings as hashes

Let's say you have two XML strings that are ordered differently but you don't care about the order of attributes inside containers:

a = '<?xml version="1.0" encoding="UTF-8"?><Authenticate><User>batman</User><Password>secret</Password></Authenticate>'
b = '<?xml version="1.0" encoding="UTF-8"?><Authenticate><Password>secret</Password><User>batman</User></Authenticate>'

Working with plain string comparison is not helpful, of course:

a == b
=> false

Instead, you can use the Nori gem ...

Git: Remove information on branches that were deleted on origin

When branches get deleted on origin, your local repository won't take notice of that.

You'll still have your locally cached versions of those branches (which is actually good) but git branch -a will still list them as remote branches.

You can clean up that information locally like this:

git remote prune origin

Your local copies of deleted branches are not removed by this.

The same effect is achieved by using (kudos to Julien):

git fetch --prune

You could also set that as a default.

Markdown/Commonmarker examples

This card shows you how to format a card's content using Markdown. We use the Commonmarker interpreter, so here are examples for its dialect.

Formatting

**Bold**

Bold

_Italics_

Italics

`Monospaced`

Monospaced

> Quoted text

Quoted text

Here is [a link](http://makandra.com/).

Here is a link.

![An image; this is the alt text](http:/...

ActiveRecord: Overwriting a setter causes trouble with attributes depending on each other

Undeterministically I got a nil error on saving the object caused by the random order of hash elements of the params hash.

class Feedback
  belongs_to :user

  def role=(value)
    @role = value
    self.email = find_email_by_name(user.name)
  end
end

This piece of code works well until the object params hash contains a second element when it is updated like this:

@feedback.update_attributes!(params[:feedback])

Now it is no longer ensured that user was set before name was set. If the name...

TodoMVC - A common learning application for popular JavaScript MV* frameworks

Developers these days are spoiled with choice when it comes to selecting an MV* framework for structuring and organizing JavaScript web apps. Backbone, Spine, Ember (SproutCore 2.0), JavaScriptMVC... the list of new and stable solutions goes on and on, but just how do you decide on which to use in a sea of so many options?

To help solve this problem, we created TodoMVC - a project which offers the same Todo application implemented using MV* concepts in most of the popular JavaScript MV* frameworks of today.

Solutions look and feel the same...

Creating the inverse of a Rails migration

Let's say you need to revert a migration that happened a while back. You'd create a new migration that removes what was added back then in the up path, while its down path restores the old functionality.

While you could just copy&paste the down and up parts of it to the inverse part of the new migration, you may not want to do that. Especially when the up/down paths already contained some logic (that executed update statements on the created column, for example), copying does not feel right.

Someone already added the logic how to...

RubyMine: Find and execute a menu action by its name

You are looking for a functionality in RubyMine but don't know or remember its keyboard shortcut or which menu it is located in?\
Hit Ctrl+Shift+A.

This will bring up the "Find Action" box where you can enter an action's name or category. Pick the result from the list to run it.

The list of results will also show you any assigned keyboard shortcuts.

rubymine-find-action.png

RubyMine: Exclude data and log directories from a project

RubyMine offers you to exclude directories from search, meaning faster search results and less "noise" in the list of result.

Right-click a folder in your project tree and click "Mark Directory As" → "Excluded".

Do it for your your log, data, and other directories that you don't need to access during development and whose search results are irrelevant.
They won't be deleted but simply ignored when searching across a project's files.

![](https://makandracards.com/makandra/6635-rubymine-exclude-data-and-log-directories-from-a-project/atta...

Git: Diff changes in a long line efficiently

Instead of showing you two lines for each change, Git allows you to highlight changes in a line explicitly:

git diff --word-diff some_file

Hello [-world-]{+universe+}!

(The result is usually colored nicely, the removed part being red and the added text green.)

When doing a diff on a long line, this can be very helpful but you'll still get a less-like scrolling output that can be unhandy to use.\
You maybe just want the diff put into your terminal:

PAGER='' git diff --word-diff some_file

WebMock 1.8.0 does not play nice with Curb < 0.7.16

When updating WebMock, be prepared that your specs may send real requests into the depths of the internet unless you update Curb as well.\
WebMock will not complain about those requests not being stubbed.

One of the commits that made it into 1.8.0 actually breaks Curb versions below 0.7.16 while fixing it for that version (and above, hopefully).\
WebMock's hooks for Curl::Easy are sti...

TeamViewer 7 finally works with multiple screens under Linux

TeamViewer 6 and lower had an issue where they would see a multi-monitor Linux setup as a single wall of pixels. This is fixed in version 7. The guest can now select the currently active screen from the TeamViewer menu.

will_paginate on complex scopes may be slow (workaround)

will_paginate triggers a database query to determine the total number of entries (i.e. to let you display the number of search results). When you paginate complex scope (e.g. that has many includes), this query may take several seconds to complete.

If you encounter this behavior, a solution is to calculate the total count yourself and pass it to the pagination call:

scope = User.complex_scope_full_of_includes
total_number_of_users = scope.count
@users = scope.paginate(:total_entr...

Sunspot and Solr on Tomcat: Trouble with Umlauts

We experienced problems with Sunspot and Solr on Tomcat: Umlauts (ä, ö, ü) were not correctly handled on Tomcat while everything was okay on the local development machines (your local Sunspot service you start with the sunspot:solr:run task is based on Jetty).

We use a stemmer that reduces "Sänger" to "sang" and "Sanger" to "sang" as well.
Though, results for "Sänger" where empty on Tomcat.

This is due to a UTF-8 bug in RSolr (see Github for some discussion on that).
The bug is fixed in a ...

Issues with has_select?

The handy method has_select?(field, :selected => text) does not behave as expected with Cucumber 0.10.2, Capybara 0.4.1.2 and Selenium 0.2.2. It may not recognize a select field if the selected option with the text has no value. If you don't have the possibility to upgrade these Gems, probably the best way to go is to distinguish the current Capybara driver:

Then /^"([^"]*)" should be selected for "([^"]*)"(?: within "([^\"]*)")?$/ do |value, field, selector|
  with_scope(selector) do

    # currently needed due to different behav...

Convert Virtualbox .ova Image to .ovf

The .ova file format is a tar file with a .ovf file inside.
tar xvf virtualboximage.ova

Nicer alternatives to def_delegator or def_delegators

Delegating methods to other objects is often helpful but the syntax of both def_delegators and def_delegator is a complete mess that makes your code hard to read.

Consider these classes:

class Topic < ActiveRecord::Base
  def title
    "A title"
  end
  
  def category
    "Topic category"
  end
end

class Post < ActiveRecord::Base
  belongs_to :topic
  def_delegato...

Moment.js - A lightweight javascript date library

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Run your own code before specific RSpec examples

You probably know about the possibility to tag scenarios in Cucumber to run your own piece of code before the actual scenario is run which looks like that:

@foo
Scenario: Do something
...

and you place the following snippet into support/env.rb:

Before('@foo') do
  puts "This is run every time a @foo tagged scenario is hit"
end    

You can tag RSpec examples like this:

it 'does something', :foo => true do
  ...
end

What you need is the following within the RSpec.configure do |config| block wit...

Fix YAML::Syck::DefaultKey:0x1083b59f8

When your gems complain about invalid gemspecs and illformed requirements, it is most probably an error resulting from the transition from Syck to psych. To fix this:

  1. go to your gemspec directory (e.g. /Library/Ruby/Gems/1.8/specifications/)
  2. change #<Syck::DefaultKey:0x00000100e779e8> to = (equals sign) in each file that's complaining

CSS: Change text selection color

You can change the color for text selection via CSS, using the ::selection and ::-moz-selection pseudo-elements.

Adding this to your Sass will make all text selections use a red background:

::selection
  background-color: #f00

::-moz-selection
  background-color: #f00

Unfortunately, those can't be combined into "::selection, ::-moz-selection". Doing so will have no effect.

RestClient sends XML Accept header by default

REST Client is a nice, simple HTTP client library for Ruby.

When you do a simple GET request like that:

RestClient.get 'http://example.com/'

it will result in this request beeing sent to www.example.com:

GET / HTTP/1.1
Accept: */*; q=0.5, application/xml
Accept-Encoding: gzip, deflate
Host: www.example.com

The application/xml accept header might lead to unexpected results on your server. You can force REST Client to ask the server for default text/html that way:

RestC...

Letter Opener

Preview email in the browser instead of sending it.

Auto-generate Cucumber navigation paths

Don't you just hate to write Cucumber path helpers to be able to say this?

When I go to the user form for "foo@bar.de"               # goes to edit_user_path(User.find_by_anything!('foo@bar.de'))
When I go to the form for the user "foo@bar.de"           # goes to edit_user_path(User.find_by_anything!('foo@bar.de'))
When I go to the form for the user above"                 # goes to edit_user_path(User.last)
When I go to the project page for "World Domination"      # goes to project_path(Project.find_by_anything!('World Domination')
...

High-level Javascript frameworks: Backbone vs. Ember vs. Knockout

This is a very general introduction to MV* Javascript frameworks. This card won't tell you anything new if you are already familiar with the products mentioned in the title.

As web applications move farther into the client, Javascript frameworks have sprung up that operate on a higher level of abstraction than DOM manipulation frameworks like jQuery and Prototype. Such high-level frameworks typically offer support for client-side view rendering, routing, data bindings, etc. This is useful, and when you write a moderately complex Javascript ...