View

Find the innermost DOM element that contains a given string

Let's say you want to find the element with the text hello in the following DOM tree:

<html>
  <body>
    <article>
      <strong>hello</strong>
      <strong>world</strong>
    </article>
  </body>
</html>

You might think of using jQuery's :contains selector:

$(":contains('hello')")

Unfortunately that returns a lot more elements than you expect:

[ <html>...<html>,
  <body>...</body>,
  <article>...</article>,
  <strong>hello</strong> ]

The reason for this is that…

Repeats

How to: Use git bisect to find bugs and regressions

Git allows you to do a binary search across commits to hunt down the commit that introduced a bug.

Given you are currently on your branch's HEAD that is not working as expected, an example workflow could be:

git bisect start # Start bisecting
git bisect bad # Tag the revision you are currently on (HEAD) as bad. You could also pass a commit's SHA1 like below:
git bisect good abcdef12345678 # Give the SHA1 of any commit that was working as it should
# shorthand:
git bisect start HEAD abcdef12345678

Git will fetch a commi…

External contentRepeats

Create and send any HTTP request using the Postman request builder

Talking with APIs makes more fun using Postman. Otherwise you might want to read: How to send HTTP requests using cURL

Postman ist available as Chrome or Mac App.

How does it helped me?
- Editing multiline JSON bodies is much more comfortable than in the terminal
- Saving named request in a collection
- Syntax highlighting when writing JSON bodies
- History with all my request
- Multiple environments

External content

Google Analytics Debugger

When you're facing a somewhat complex Google Analytics setup and want to find out what's happening, you can use this Chrome extension.

It's much simpler than other fancy plugins (like "Google Tag Assistant") and does just one job and does it well.

To see what's happening, you need to open your developer console.

Repeats

Rails asset pipeline: Why things break in production and what precompilation does to your assets

When you work with a Rails 3.1+ application, you will be working with the asset pipeline. The asset pipeline is awesome until you deploy. Then it will be less so if you haven't done everything as the pipeline expected it from you.

The problem

When using the asset pipeline your assets (images, javascripts, stylesheets, fonts) live in folders inside app:

app/assets/fonts
app/assets/images
app/assets/javascripts
app/assets/stylesheets

With the asset p…

One-liner syntax in RSpec's should-based and expect-based syntaxes

RSpec supports a one-liner syntax for setting an expectation on the subject:

describe Array do
  describe "when first created" do
    it { should be_empty }
  end
end

The example description "it should be empty" will be defined automatically.

With RSpec 3's expect-based syntax you use it_is_expected instead:

describe Array do
  describe "when first created" do
    it { is_expected.to be_empty }
  end
end

How to reverse the order of HTML elements with CSS

Imagine you have a list you want to render inline on large screens, but stacked on small screens.

<ul>
  <li>high</li>
  <li>medium</li>
  <li>low</li>
</ul>
ul { white-space: nowrap } /* optional: keep items in one line no matter the available width */
li { display: inline-block }

@media (max-width: 600px) {
  li { display: block }
}

Now imagine you want the rightmost item to be the topmost item on small screens. You'll need to invert the order of list items, but only for large screens. Here are some approaches to do so:…

Rails route namespacing (in different flavors)

TL;DR

  scope module: 'module', path: 'path', as: 'as' do
    resources :examples, only: :index
  end

=> path helpers: as_examples_path and as_examples_url
=> URL: /path/examples
=> Controller: Module::ExamplesController
=> Views path: app/views/module/examples/

Without affecting controllers or route helpers

When you want to namespace some paths but leave the corresponding controllers and url helpers unaffected, you can use the scope method like follows:

scope path: 'proxy' do
  resources :properties
end

-…

Google Hangouts: How to increase the size of a shared screen (horrible solution!)

When another caller is sharing their screen, Hangouts will draw large black margins around their picture, even when you go Full screen. This makes the text very hard to read, even when your screen is the same size as theirs.

The "solution" is to hammer CTRL+Minus multiple times and ZOOM OUT (!!!) until you hit 25%. This way the Hangouts controls become so small that there is more space for the actual screen.

I'm so sorry!

Repeats

Always, always declare your associations with symbols

Never ever declare your associations with a string, especially when doing metaprogramming. A common mistake is something like

# WRONG
class Page < ActiveRecord::Base
  %w[main sub].each do |type|
    belongs_to "#{type}_title"
  end
end

Always append a to_sym, otherwise you'll have all kinds [of fun](/makandra/11601-unsa…

External content

djberg96/sys-filesystem: A Ruby library for getting filesystem information

Uses FFI and works all relevant operating systems.

If you'd try to do it yourself, you'd have to use FFI which is a bit awkward (see the gem's code), or call commands like df -B1 and search the strings for your relevant data.
That gem takes the pain away and works nicely.

Repeats

Testing ActiveRecord validations with RSpec

Validations should be covered by a model's spec.

Recipe for testing any validation

In general any validation test for an attribute :attribute_being_tested looks like this:

  1. Make a model instance (named record below)
  2. Run validations by saying record.valid?
  3. Check if record.errors[:attribute_being_tested] contains the expected validation error
  4. Put the attribute into a valid state
  5. Run validations again by saying record.valid?
  6. Check if record.errors[:attribute_being_tested] does *…
Repeats

ActiveRecord scopes must be loaded before using RSpec's "=~" matcher

To test whether two arrays have the same elements regardless of order, RSpec gives you the =~ matcher:

actual_array.should =~ expected_array

If either side is an ActiveRecord scope rather than an array, you should call to_a on it first, since =~ does not play nice with scopes:

actual_scope.to_a.should =~ expected_scope.to_a

netstat: Sum open connections by IP (and sort it)

The following sums up all connections (ESTABLISHED, TIME_WAIT, FIN_WAIT, etc.) and sorts it:

netstat -n | awk ' $5 ~ /^[0-9]/ {print $5}' | cut -d: -f1 | sort | uniq -c | sort -n

Works on FreeBSD and Linux.

Repeats

Git: How to configure git to push only your current branch

You can change which branches will be pushed when saying git push. Our recommendation is to set it to current.

From the git-config documentation:

push.default

Defines the action git push should take if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line. Possible values are:

  • nothing - do not push anything.
  • matching - push all matching branches. All bra…
Repeats

Testing ActiveRecord callbacks with RSpec

Our preferred way of testing ActiveRecord is to simply create/update/destroy the record and then check if the expected behavior has happened.

We used to bend over backwards to avoid touching the database for this. For this we used a lot of stubbing and tricks like it_should_run_callbacks.

Today we would rather make a few database queries than have a fragile test full of stubs.

Example

Let's say your User model creates a first Project on cr…

Repeats

RSpec 3 argument constraints use weak equality

If you expect method calls in RSpec 3, be aware that the argument matchers use very liberal equality rules (more like === instead of ==).

For example:

expect(subject).to receive(:foo).with(MyClass)

subject.foo(MyClass)      # satisfies the expectation
subject.foo(MyClass.new)  # also satisfies the expectation

expect(subject).to receive(:bar).with(/regex/)

subject.bar(/regex/)      # satisfies the expectation
subject.bar('regex')      # also satisfies the expectation

This is usually not an issue, except when your method argu…

View
3107 cards