Today I Learned

A Hashrocket project

6 posts by micahwoods

Remove whitespace changes, then `git add -p`

git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p

git diff -w --no-color creates a diff

git apply --cached --ignore-whitespace applies the diff ignoring whitepace, and indexes it

git checkout — . removes the unindexed “whitespace” changes

git reset resets the index to just he non-whitespace cahnges

git add -p adds the non-whitespace changes in patch mode

We did this with it alias gwap=“git diff -w --no-color | git apply --cached --ignore-whitespace && git checkout -- . && git reset && git add -p”

Pause Ember acceptance tests

While your acceptance tests are running you can use pauseTest() to pause the test suite and interact with your app. NOTE: this does not halt execution like debugger, it only pauses the test suite.

test("my acceptance test", function(assert) {
  visit('/');
  pauseTest();  // returns a promise that will never resolve
  andThen(function(){
    // make assertions
  });
});

ActiveRecord subselects

So you want to find all the rocketeers who wrote blog posts in a date range.

Blog::Post.where(published_at: 15.years.ago..4.years.ago).includes(:rocketeer).map(&:rocketeer)
  # Blog::Post Load (0.6ms)  SELECT "blog_posts".* FROM "blog_posts"
  #   WHERE ("blog_posts"."published_at" BETWEEN '2000-06-12 14:40:06.429288' AND '2011-06-12 14:40:06.429498')
  # Rocketeer Load (0.7ms)  SELECT "rocketeers".* FROM "rocketeers"
  #   WHERE "rocketeers"."id" IN (12, 13, 14, 16)  ORDER BY "rocketeers"."name"

But you want to do it in one query!

Rocketeer.where(
  id: Blog::Post.where(published_at: 15.years.ago..4.years.ago).select(:rocketeer_id)
)
  # Rocketeer Load (0.9ms)  SELECT "rocketeers".* FROM "rocketeers"
  #   WHERE "rocketeers"."id" IN (
  #     SELECT "blog_posts"."rocketeer_id" FROM "blog_posts"
  #       WHERE ("blog_posts"."published_at" BETWEEN '2000-06-12 14:42:20.005077' AND '2011-06-12 14:42:20.005317'))  ORDER BY "rocketeers"."name"