Angular directives: How to find out if you have transcluding content

When you have an Angular directive that transcludes content, you might want to do something in case there is no content inside your element, like showing some default content.

Unfortunately, you can not do something like <span ng-transclude>default goes here</span>. Angular will always empty that element's text, even if there is nothing to transclude.

But you can use your directive's link function. Here's some CoffeeScript for you:

    @app.directive 'myStuff', [->
      restrict: 'E'
      transclude: true
      
      te...

Iterating over a Ruby Hash while tracking the loop index

You know each_with_index from arrays:

    ['hello', 'universe'].each_with_index do |value, index|
      puts "#{index}: #{value}"
    end
    # 0: hello
    # 1: universe

This also works on hashes. However, mind the required syntax:

    { hello: 'universe', foo: 'bar' }.each_with_index do |(key, value), index|
      puts "#{index}: #{key} => #{value}"
    end
    # 0: hello => universe
    # 1: foo => bar

The reason is that each_with_index yields 2 elements to the block, and you need to deconstruct the first el...

Cucumber: Skipping steps in a scenario outline, based on the current example

In Cucumber, scenario outlines help avoiding tests that are basically the same, except for a few variables (such as different inputs). So far, nothing new.

The problem

Now what if your test should (or should not) do something, like filling in a field only for some tests?

    Scenario Outline: ...
      When I open the form
        And I fill in "Name" with "<name>" # <= we want to do this only occasionally
      Then everybody should be happy
      
    Examples:
      | name  |
      | Alice |
      | Bob   |

You could o...

Fixing Ruby debugger: *** Unknown command: "something". Try "help".

So you have placed a breakpoint somewhere and now want to dig around, but not even inspecting variables is working:

(rdb:3) @order_item
*** Unknown command: "@order_item".  Try "help".

The reason is, you must tell the debugger to evaluate your expression. One workaround is to call irb to open an irb session at your breakpoint. Resume by sending Ctrl+D twice or by returning to the outer irb with "exit" and then continuing with "c".

However, the native debugger command for your issue is eval (or its shorter alias `e...

Manually purge binary logs on MariaDB

Open a MySQL root shell and use this command:

PURGE BINARY LOGS BEFORE DATE(NOW() - INTERVAL 3 DAY) + INTERVAL 0 SECOND;

RSpec 3 no longer chooses a spec's type based on its directory

While RSpec 1 and 2 decided that specs inside spec/model are model specs, and those inside spec/features are feature specs (and so on), RSpec 3 will no longer do that by default.

This will result in errors such as missing routing helpers, etc.

There are 2 ways to fix this:

  • Explicitly set the type on each spec. For example:

    describe '...', type: 'feature' do
      # ...
    end
    
  • Add this to your spec_helper.rb (inside the RSpec.configure block) to restore the old behavior:

    ...

Git: Listing branches with their latest author

Run this command to list the authors of the most recent commit of each branch:

git for-each-ref --format='%(committerdate) %09 %(authorname) %09 %(refname)' | sort -k5n -k2M -k3n -k4n

Credits go to DarVar on SO.

Test if all your favicons exist

When you don't only have a favicon.ico in your project but also PNGs of different sizes and backgrounds, you should test if all those files are actually reachable.

Here are a few selectors to get you started:

    'link[rel~="icon"]' # regular ones, matches "shortcut icon" and "icon"
    'link[rel="apple-touch-icon"]' # iOS
    'meta[content][name="msapplication-TileImage"]' # IE11
    'meta[content][name^="msapplication-square"]' # IE11

A s...

Spreewald 1.1.0 released

Spreewald 1.1.0 drops the be_true and be_false matchers in order to be RSpec 3 and Ruby 2 compatible. For backward compatibility, these matchers are replaced with == true and == false.

Note the slightly more changed semantics of the update.

jQuery and cross domain AJAX requests

When making cross-domain AJAX requests with jQuery (using CORS or xdomain or similar), you will run into issues with HTTP headers:

  • jQuery will not set the X-Requested-With header. On your server, requests will not look like AJAX requests (request.xhr? will be false).
  • jquery-ujs will not set CSRF headers.

This is by design and improves secu...

Howto respond html or json in the same controller action with Rails 2

Code snippet tested with Rails 2.3

  def index
    # ...
    if request.xhr?
      html = render_to_string(:partial => "list", :layout => false)
      respond_to do |format|
        format.html { render :text => html }
        format.json { render :json => {:html => html, ... } }
      end
    end
  end

Note: Perhaps you ran into ActionView::MissingTemplate error and this card might help. If you call render_to_string within the format.json block, Rails will only look for an index.json template, but not for an `index.erb...

Ruby lets you redefine backticks

This actually works:

class Klass

  def initialize
    `hi world`
  end

  def `(message)
    puts "Called with backticks: #{message}"
  end
  
end

Klass.new # Prints "Called with backticks: hi world"

Hat tip to @jcoglan.

Spreewald 1.0.0 released

Spreewald now has a spreewald binary that lists all available steps, optionally filtering them. Example:

$> spreewald

# All Spreewald steps
Given I am on ...
... long list

$> spreewald check

# All Spreewald steps containing 'check'
When I check "..."
When I uncheck "..."
Then the "..." checkbox( within ...)? should be checked
Then the "..." checkbox( within ...)? should not be checked
Then the radio button "..." should( not)? be (checked|selected)