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 / 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
/* hoverの系の順序 */
a:link { color: #999ea1 }
a:visited { color: #999ea1 }
a:hover { color: #999ea1 }
a:active { color: #999ea0 }
// String {{{
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/, "");
}
# coding: utf-8
require 'date'
#
# カレンダークラス
#
# 実装(予定)機能
# (1)コンソールへの出力 ->○
# (2)テーブル形式での出力 ->○
# (3)休日を考慮
@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