Skip to content

Instantly share code, notes, and snippets.

@kjirou
kjirou / where_does_where_come_from.rb
Last active December 26, 2015 14:59
arel_tableはself.arel_tableと同じ。見えない self 自体も静的に束縛されて無名関数内で参照されている。JSのthisとは違う
class Entry < ActiveRecord::Base
scope :filter, -> {
entries = arel_table
# entries = self.arel_table # 同じ
# entries = Entry.arel_table # 同じ
}
end
@kjirou
kjirou / want_to_use_legacy_innerlink_in_angularjs.js
Created October 30, 2013 10:10
AngularJSでページ内リンクをしたくて http://www.benlesh.com/2013/02/angular-js-scrolling-to-element-by-id.html とか http://www.benlesh.com/2013/02/angular-js-scrolling-to-element-by-id.html を起点に調べて、色々やって結局いらなくなった設定の墓場
@kjirou
kjirou / devise_login_spec.rb
Created October 30, 2013 11:04
使わなくなった devise のログインテスト
require 'spec_helper'
describe Admin::HomeController do
describe 'devise' do
context '未ログインユーザーの場合' do
before(:each) do
@user = Fabricate :admin
end
it 'ログイン画面へリダイレクトする' do
@kjirou
kjirou / inner_pagelink.coffee
Created October 31, 2013 10:38
AngularJSでページ内リンクをする方法
@kjirou
kjirou / index.js
Last active August 29, 2015 13:57
Jade で each を使うと改行文字が消えてしまう問題
var jade = require('jade');
var templateFile = './template.text.jade';
var str = require('fs').readFileSync(templateFile, 'utf8');
var fn = jade.compile(str, {
filename: templateFile
});
var text = fn();
console.log(text);
@kjirou
kjirou / first_node_or_others.jade
Created March 30, 2014 09:18
Jadeで最初のノードかそれ以外かの判定
each v, i in ['foo', 'bar', 'baz']
div(class= i === 0 ? 'first' : 'others')
mysql> show create table aaa;
+-------+-----------------------------
| Table | Create Table
+-------+-----------------------------
| aaa | CREATE TABLE `aaa` (
`id` int(11) DEFAULT NULL, // -2147483648 から 2147483647 の範囲が格納可能
`foo` int(10) unsigned DEFAULT NULL // 0 から 4294967295 の範囲が格納可能
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+-----------------------------
1 row in set (0.00 sec)
@kjirou
kjirou / mongoose_query_sample.js
Last active August 29, 2015 13:58
mongoose のクエリ例
// Ref) http://mongoosejs.com/docs/queries.html
Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
//.skip(1) // これもある
.sort('-occupation')
.select('name occupation') // fields オプションと同じ、{name:1, occupation:1} と同じ意味
@kjirou
kjirou / ggrep.sh
Last active August 29, 2015 14:00
Macでコピペした文字列を所定位置から若干見易くgrepする
#!/bin/sh
grep -r "`pbpaste`" public/js views --exclude-dir=public/js/vendor | sed 's/^\(.\{1,120\}\).*/\1/' | sed 's/[[:cntrl:]]/ /'
@kjirou
kjirou / mongoose_doc_save.js
Last active August 29, 2015 14:00
存在した _id が設定済みの doc を save() するとエラー
// 重複エラーが出る
var sandbox = new Sandbox({
_id: ObjectId('既存の_id'),
});
sandbox.someProp = 'New value';
sandbox.save(function(err, sandbox){
console.log(err);
});
//
// -> [MongoError: E11000 duplicate key error index: tomcom.sandboxes.$_id_ dup key: { : ObjectId('既存の_id') }]