Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@skojin
skojin / controller.rb
Created September 16, 2022 13:45
batch set enabled flag from table with checkboxes
current_account.items.update_all(['enabled = (SELECT id IN (?))', Array(params[:ids])])
@skojin
skojin / if_jq.sh
Last active September 4, 2020 15:04
test input with jq, if ok then show input, otherwise exit with non 0 exit code
#!/bin/sh
# test input with jq, if ok then show input, otherwise exit with non 0 exit code
# echo '{"success": true}' | jq -e '.success | select(. == true)' # => {"success": true}
# echo '{"success": false}' | jq -e '.success | select(. == true)' # => exit code non 0
input=`cat`;
if echo "$input" | jq -e "$1" > /dev/null ; then
echo "$input"
@skojin
skojin / Dockerfile
Last active May 5, 2020 12:35
Dockerfile for http://luckyframework.org deployment
FROM crystallang/crystal:0.34.0 as builder
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential \
apt-transport-https curl ca-certificates gnupg2 apt-utils
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash - \
&& apt-get install -y nodejs
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
@skojin
skojin / strong_params_generator.rb
Created February 12, 2018 16:01
generate strong params hash rule by real request hash
def to_permit_rule(h)
scalar = []
complex = {}
h.each do |k,v|
if v.is_a? Array
complex[k.to_sym] = []
elsif v.is_a? Hash
if v.keys.all?{|vk| vk =~ /\A\d+\z/ }
complex[k.to_sym] = to_permit_rule(v.values.first)
else
@skojin
skojin / local:js
Created September 26, 2017 16:13
bookmarket that open production page on http://localhost:3000
javascript:(function()%7B(function()%7Bvar url %3D window.location.href.split('%2F')%3Burl%5B0%5D %3D 'http%3A'%3Burl%5B2%5D %3D 'localhost%3A3000'%3Bwindow.open(url.join('%2F'))%3B%7D)()%7D)()
@skojin
skojin / sql_tz.rb
Created July 23, 2017 19:14
util to map datetime column to date in current timezone
# SQL timezone time/date related utils
module SqlTz
extend self
# convert time to date with respect to timezone
# example: where("#{SqlTz.time_to_date(:created_at)} = ?", Date.today)
def date(column)
"DATE(DATE_ADD(#{column}, INTERVAL #{Time.current.utc_offset} SECOND))"
end
@skojin
skojin / sql_dump.rb
Last active July 23, 2017 12:28
export AR model to sql
# export AR model to sql
# example:
# SqlDump.new( KeyValue.where(..) ).export{ |sql| puts sql }
# SqlDump.new( KeyValue.where(..) ).export(file: "key_values_dump.sql")
class SqlDump
def initialize(scope)
@scope = scope
@count = 0
end
@skojin
skojin / keybase.md
Created July 6, 2017 15:01
keybase.md

Keybase proof

I hereby claim:

  • I am skojin on github.
  • I am skojin (https://keybase.io/skojin) on keybase.
  • I have a public key ASBIneplQGFfIiZhlB6VN72mbLa_bwt0kBG4NLr-tcJzAwo

To claim this, I am signing this object:

# Each record of this class store one extra attribute for each model
# Used to store not important data need for reports, data sync, and etc
class ExtraValue < ActiveRecord::Base
belongs_to :record, polymorphic: true
scope :type, ->(klass){ where(record_type: klass) }
scope :record, ->(r){ where(record_id: r.id, record_type: r.class) }
class << self
@skojin
skojin / crontab
Last active January 18, 2017 21:23
aws cloudwatch custom rails metrics
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyPassengerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[P]assenger RackApp" | wc -l)
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyDJWorkersCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[d]elayed_job" | wc -l)
*/5 * * * * aws cloudwatch put-metric-data --metric-name MyCometServerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps ax | grep "[b]randrep_comet_server" | wc -l)
# N of Passenger processed that older then 1 hour
*/30 * * * * aws cloudwatch put-metric-data --metric-name MyOldPassengerCount --namespace "System/Linux" --dimensions "InstanceId=$(ec2metadata --instance-id)" --unit Count --value $(ps axo etimes,args | grep "RackApp" | awk '{if ($1 >= (1 * 3600)) print $0}' | wc -l)