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 v3b.hatena.ne.jp
- [es2015]
- [js]
これでも行ける -> [...Array(5).keys()];
2016/04/18 11:42
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;
とする必要があるらしい。