Skip to content

Instantly share code, notes, and snippets.

#! /usr/bin/env ruby
exec("LANG=ja_JP.sjis grep -rn `echo #{ARGV[0]} | nkf -s` #{ARGV[1]} | nkf -w")
# frozen_string_literal: true
module Refinements
module AssociationCollectionExtension
refine ActiveRecord::Associations::CollectionProxy do
def find_or_build_by(arguments)
find_by(arguments) || build(arguments)
end
def detect_or_build_by(arguments)
self.detect do|instance|
@YoshitsuguFujii
YoshitsuguFujii / multiselect
Created June 1, 2015 11:25
複数選択失敗作
class MultiSelectWithoutCtrl
constructor: (_this, options)->
@id = null
@name = options["name"]
@select = _this
@checkboxes = $("<div class='hidden_multi_select'></div>")
@select.after(@checkboxes)
@selected()
selected: ->
@select.on "click" , (event) =>
@YoshitsuguFujii
YoshitsuguFujii / real_delete_discreetly.rb
Last active August 29, 2015 14:14
real delete when no relation (dependent on acts_as_pranoid)
# lib/active_record/real_delete_discreetly.rb
module RealDeleteDiscreetly
extend ActiveSupport::Concern
def delete_discreetly
relation_exist = false
self.class.confirm_relations.each do |confirm_relation|
if destroyed_by_association
if destroyed_by_association.active_record == confirm_relation.to_s.classify.constantize
relation_exist = false
# http://stackoverflow.com/questions/6317705/rackrequest-how-do-i-get-all-headers
require 'rack'
app = Proc.new do |env|
headers = env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|pair| [pair[0].sub(/^HTTP_/, ''), pair[1]]}
.collect {|pair| pair.join(": ") << "<br>"}
.sort
[200, {'Content-Type' => 'text/html'}, headers]
end
@YoshitsuguFujii
YoshitsuguFujii / console class
Last active August 29, 2015 14:08
console汎用クラス
class Console
# Class Methods
class << self
def gets_from_stdin(message = nil)
print "#{message}: " unless message.nil?
str = STDIN.gets.chomp
end
def get_y_or_n_from_stdin(message)
bol = ""
# coding: utf-8
require "aws-sdk"
S3_SAVE_DIR = "s3/"
ALL_FILE_DOWNLOAD = "全てのファイルを落とす"
s3 = AWS::S3.new(
access_key_id: "YOUR AWS ACCESS KEY"
secret_access_key: "YOUR AWS SECRET"
@YoshitsuguFujii
YoshitsuguFujii / hankaku_kana_checker.rb
Created January 30, 2014 02:48
半角カナファイルの抽出
#!/usr/bin/env ruby
require 'find'
except_ext = %w(.log .yml .jpg .JPG .jpeg .png .pid .ttf .woff .md .json .ico .eot .gif) # 除外するファイル拡張子を設定
except_pattern = %w(.git test .DS_Store migrate hankaku_kana_checker RackMultipart sprockets letter_opener underscore cache) # 除外するパスに含まれる文字列を指定
# チェックする半角カナの配列
han_kana = %w{ア イ ウ エ オ カ キ ク ケ コ サ シ ス セ ソ タ チ ツ テ ト ナ ニ ヌ ネ ノ ハ ヒ フ ヘ ホ マ ミ ム メ モ ヤ ユ ヨ ラ リ ル レ ロ ワ ヲ ン ァ ィ ゥ ェ ォ ャ ュ ョ ッ ゙ ゚ 、 。 ー 「 」}
@YoshitsuguFujii
YoshitsuguFujii / extend_jquery.js.coffee
Last active December 27, 2015 11:09
extend_jquery.js.coffee
$ ->
jQuery.extend jQuery.fn,
toggle_value: ->
obj = jQuery(this)
obj.val ((if obj.val() is "true" then "false" else "true"))
exists: ->
@length > 0
enable: ->
$(this).removeAttr "disabled"
disabled: ->
module ExtendBuiltInDataType
class ::Hash
def merge_apend_value(hash) #{{{
self.update(hash) do |key, self_val, other_val|
Array.wrap(self_val).push(other_val)
end
end #}}}
end
end