FactoryGirlでActiveRecord以外のデータをつくる

例えばHashie::Mashを返したい場合はこんな感じ。

FactoryGirl.define do
  factory :post, class: Hashie::Mash do
    title "foo"
    body "bar"
  end
end

FactoryGirl.build(:post).class #=> Hashie::Mash
FactoryGirl.build(:post) #=> {"title"=>"foo", "body"=>"bar"}
FactoryGirl.build(:post, title: "baz").title #=> "baz"

ちなみにbuildじゃなくてcreateするとsave!が呼ばれるんだけどHashie::Mash#xxx!xxxフィールドが作られるという挙動なのでcreateでも動くけど無駄にsaveフィールドが作られる。

FactoryGirl.create(:post) #=> {"title"=>"foo", "body"=>"bar", "save" => {}}

ES6でN個の配列

Array.prototype.keysはIteratorを返すのでArray.fromに食わせる

Array.from(Array(5).keys()); //=> [0, 1, 2, 3, 4]

もしくは

Array.from({ length: 5 }).map((v, k) => k); //=> [0, 1, 2, 3, 4]

Array.fromの第二引数はmap的な役割があるので

Array.from({ length: 5 }, (v, k) => k); //=> [0, 1, 2, 3, 4]

こんな感じ。

[追記] 便利。

ES6でN個の配列 - hokaccha hamalog v3

これでも行ける -> [...Array(5).keys()];

2016/04/18 11:42
b.hatena.ne.jp

ShadowDOMの外から内部の要素を取得する

shadowRootでshadowRootが取れるのでそこから辿れる。

var proto = Object.create(HTMLElement.prototype);
proto.createdCallback = function() {
  var shadowRoot = this.createShadowRoot(); 
  shadowRoot.innerHTML = '<span class="foo">text</span>';
};

document.registerElement('x-element', { prototype: proto });

var el = document.createElement('x-element');

// これで取れる
console.log(el.shadowRoot.querySelector('.foo').textContent); //=> text

Shadow-Piercing descendant combinator is deprecated

最近Web Componentsのアップデート全然見てなかったけどCSSで外からShadow DOMをスタイリングするための/deep/>>>)、::shadowがdeprecatedになってた。

Shadow-Piercing descendant combinator, '/deep/' (aka '>>>') - Chrome Platform Status

gitでトラッキングされてないファイルも含めてstashする

なんとなく

$ git stash --all

ってやったら、ignoreされてるファイルも全部消えてファッってなった。

$ git stash -u
# or
$ git stash --include-untracked

が正しいっぽい。以下helpより。

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state.
If the --all option is used instead then the ignored files are stashed and cleaned in addition to the untracked files.

Polymer/platform.jsでdocument.currentScriptが動かない

Web ComponetsのPolyfillのPolymer/platform.jsでHTML import使ったときに、呼び出される側のHTMLのJSのdocumentを取得したいときに、

var doc = document.currentScript.ownerDocument;

ってやればとれるらしいんだけど、これがうまくPolyfillできなくて動かないらしい。
https://github.com/Polymer/HTMLImports/blob/a2accf9e3e15ee4584196a9aa5803cd5e7535820/src/HTMLImports.js#L196-L206

ので

var doc = document._currentScript.ownerDocument;

とする必要があるらしい。

最近のAndroid Chromeのリモートデバッグ

以前はAndroidChrome側にWebデバッグを有効にするみたいなのがあって、PC側ではADB立ち上げたりする必要があったんだけど、最近はもっと簡単になってるっぽくて、

1. AndroidのUSBデバッグを有効にする
2. USBでつなぐ
3. AndroidChromeで適当なページ開く
4. PCのChromeで「ツール」→「デバイスの検証」を開く
5. Androidで開いてるページをPC側で開いてデバッグできる

という簡単なステップで済むようになってる。

PCのChromeのバージョンは32以上が必要(現時点でのstableは31なのでCanaryとかBetaを使う必要ある)。