Skip to content

Instantly share code, notes, and snippets.

Takumi Shotoku sinsoku

Block or report user

Report or block sinsoku

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View state_sample.rb
# app/models/article.rb
class Article < ApplicationRecord
has_many :article_publications
scope :draft, -> { left_joins(:article_publications).where(article_publications: { id: nil }) }
scope :published, -> { joins(:article_publications) }
end
# app/models/article_publication.rb
class ArticlePublication < ApplicationRecord
View ar_test.rb
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
View a.html.erb
<% A.foo(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<% end %>
View db.tf
resource "aws_db_instance" "db" {
count = "${terraform.workspace == "default" ? 1 : 0}"
}
resource "aws_rds_cluster" "db" {
count = "${terraform.workspace == "default" ? 0 : 1}"
}
locals {
db_endpoint = "${terraform.workspace == "default" ? aws_db_instance.db.endpoint : aws_rds_cluster.db.endpoint}"
@sinsoku
sinsoku / _how_to_use.rb
Last active May 14, 2019
`lazy_find_by` returns AR instance that executes queries when attributes is needed.
View _how_to_use.rb
## Simple UseCase
user = User.lazy_find_by(id: 1, name: "foo")
#=> #<User id: 1, name: "foo", age: nil, created_at: nil, updated_at: nil>
user.id
#=> 1
user.name
#=> "foo"
user.age
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
@sinsoku
sinsoku / bidm_interface.rb
Last active Mar 26, 2019
BiTemporal Data Modelをこんな感じで書きたいというネタ
View bidm_interface.rb
class Employee
include Foo::BitemporalModel
# デフォルトにしたいなら、自分で書きたい
# default_scope { btdm_valid }
# findを上書きしたいなら、明示的に書きたい
# alias find btdm_find
end
View keybase.md

Keybase proof

I hereby claim:

  • I am sinsoku on github.
  • I am sinsoku (https://keybase.io/sinsoku) on keybase.
  • I have a public key ASAkT9x41b7tlMeSOQGOdIGNVkyWdDjtQ9isFc1WUIUt7Qo

To claim this, I am signing this object:

@sinsoku
sinsoku / run.rb
Created Sep 20, 2017
RubyKaigi のスポンサーを取得するスクリプト
View run.rb
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'nokogiri'
require 'open-uri'
Sponsor = Struct.new("Sponsor", :name, :url, :year)
RUBYKAIGI_URL = "http://rubykaigi.org/"
sponsors = []
@sinsoku
sinsoku / catalog.md
Last active Sep 11, 2017
Rails 本に書きたい内容の目次(仮)
View catalog.md
  1. まえがき
  2. Rails プロジェクトの始め方
  3. クラスの探索順序と重複定義
  4. DB 制約とバリデーション
  5. 安定したステージング環境
  6. モデルの多重コールバック問題
  7. ポリモーフィック関連の使い所と注意点
  8. Rails で大量のレコードを扱う方法
  9. RESTful なコントローラー
  10. current_user の汚染
@sinsoku
sinsoku / nested_hash_buildable.rb
Last active Aug 14, 2017
I want to build a nested hash more easily.
View nested_hash_buildable.rb
# frozen_string_literal: true
module NestedHashBuildable
# Builds a nested hash
#
# NestedHashBuildable.build(:a, :b, c: obj)
# #=> { a: { b: { c: obj } } }
def self.build(*keys, obj)
keys.reverse.inject(obj) { |a, i| { i => a } }
end
You can’t perform that action at this time.