Skip to content

Instantly share code, notes, and snippets.

View dtaniwaki's full-sized avatar

Daisuke Taniwaki dtaniwaki

View GitHub Profile
/*
* Wrap file input like this.
* <span class="file-wrapper">
* <span class="class-for-your-custom-button">Choose a Photo</span>
* <input type="file" name="photo" id="photo" />
* </span>
*/
.file-wrapper {
cursor: pointer;
overflow: hidden;
$(function(){
$(document).on('change', '[data-checkbox-all]', function(){
var $self = $(this);
var target = $self.data('checkbox-all');
$('[data-checkbox=' + target + '],[data-checkbox-all=' + target + ']').prop('checked', $self.prop('checked'));
});
$(document).on('change', '[data-checkbox]', function(){
var $self = $(this);
var target = $self.data('checkbox');
var $checkboxes = $('[data-checkbox=' + target + ']');
class MultiIO
def initialize(*targets)
@targets = targets.map{ |t|t.is_a?(String) ? open(t, 'a') : t }
end
def write(*args)
@targets.each{ |t| t.write(*args) }
end
def close
@dtaniwaki
dtaniwaki / smart_assets_precompile.rb
Last active August 29, 2015 13:57
Skip assets:precompile only if the assets are updated.
def do_if_updated(tag, *sources, &block)
to = (latest_revision rescue nil)
from = (current_revision rescue nil)
doit = if !from || !to || !fetch(:smart_assets_precompile, true)
true
else
run_locally("#{source.local.log(from, to)} #{sources.join(' ')} | wc -l").to_i > 0
end
if doit
block.call
require 'net/http'
class UriValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
raise(ArgumentError, "A regular expression must be supplied as the :format option of the options hash") unless options[:format].nil? or options[:format].is_a?(Regexp)
#configuration = { message: "is invalid or not responding", format: URI::regexp(%w(http https)) }
configuration = { check_response: true, format: /(\A\z)|(\A(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?([\/].*)?\z)/ix}
configuration.update(options)
if value =~ configuration[:format]
if configuration[:check_response]
begin # check header response
require 'mail'
class EmailValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
begin
m = Mail::Address.new(value)
# We must check that value contains a domain and that value is an email address
r = m.domain && m.address == value
t = m.__send__(:tree)
# We need to dig into treetop
# A valid domain must have dot_atom_text elements size > 1
class TimeValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
flg = true
if de = options[:before]
de = Time.now if de == :now
flg &&= value <= de
end
if de = options[:after]
de = Time.now if de == :now
flg &&= de <= value
class ByteSizeValidator < ActiveModel::Validations::LengthValidator
def validate_each(record, attribute, value)
value_length = value.respond_to?(:bytesize) ? value.bytesize : value.to_s.bytesize
CHECKS.each do |key, validity_check|
next unless check_value = options[key]
next if value_length.send(validity_check, check_value)
errors_options = options.except(*RESERVED_OPTIONS)
errors_options[:count] = check_value
if Settings.mailer && Settings.mailer.interceptors
class SandboxEmailInterceptor
def self.delivering_email(message)
whitelist = [Settings.mailer.whitelist].flatten.compact
blacklist = [Settings.mailer.blacklist].flatten.compact
allowed = [message.to].flatten.all?{|t| whitelist.any?{|r| r.is_a?(Regexp) ? r =~ t : r == t } }
prohibited = [message.to].flatten.all?{|t| blacklist.any?{|r| r.is_a?(Regexp) ? r =~ t : r == t } }
if prohibited || !allowed
Rails.logger.debug "Intercept email for #{message.to.inspect}"
message.to = [Settings.mailer.interceptors].flatten
@dtaniwaki
dtaniwaki / file0.scala
Created December 12, 2015 10:59
Akkaによるbatch request作り方 ref: http://qiita.com/dtaniwaki/items/fc11b503ce0e27e52516
case class BatchTriggerTick()