Skip to content

Instantly share code, notes, and snippets.

@goooooouwa
goooooouwa / fixtures.rake
Last active June 25, 2018 22:06 — forked from ZachBeta/fixtures.rake
fix: no method "each" for string type
#put in lib/tasks/fixtures.rake
namespace :db do
namespace :fixtures do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :dump => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_migrations"]
ActiveRecord::Base.establish_connection(:development)
(ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|
@goooooouwa
goooooouwa / replace_review_and_commit.sh
Created July 25, 2014 06:15
little bash script to replace all occurrences of the first command line argument in the given directories. It will display the diff and then ask to commit.
#!/bin/bash
eval 'find app/views app/controllers app/models -type f -exec sed -i "s/ $1[^s\"]/ ConstrBase::&/g" {} \;'
eval 'find app/views app/controllers app/models -type f -exec sed -i "s/ConstrBase:: $1/ConstrBase::$1/g" {} \;'
git diff
echo -n "Here be dragons. Continue?"
read REPLY
if [[ "$REPLY" =~ ^[Yy]$ ]]
then
# in Vim
:args ~/src/myproject/**/*.ttl | argdo execute "normal gg=G" | update
@goooooouwa
goooooouwa / daily-bash-script-samples.sh
Last active August 25, 2023 06:21
daily bash script samples
#!/bin/bash
# For loop to exeute some task multiple times
for i in Item1 Item2 ItemN; do
bash ~/task.sh ${i}
done
for i in {1..10}
do
wget https://static.generated.photos/vue-static/human-generator/poses/female/00$i.png
@goooooouwa
goooooouwa / example usage of optional type.swift
Last active August 29, 2015 14:05
example usage of optional type in swift
var exampleDictionary = ["foo":1,"bar":2]
// an optional type means its value can either be some certain type or nothing at all( nil)
var optionalTypeVariable: Int? = exampleDictionary["someUncertainValue"]
// calling optionalTypeVariable will either return an integer or a nil, so you should normally check it’s value before actually using it
if optionalTypeVariable == nil {
println("The value of optionalTypeVariable is nil")
} else {
// calling optionalTypeVariable! will always return an integer, if optionalTypeVariable is in fact nil, then an assertion will be triggered.
@goooooouwa
goooooouwa / where condition syntax for join table example.rb
Created August 19, 2014 07:16
where condition syntax for join table example
# syntax: ModelName.joins(:association_name).where({joining_table_name: {column_name:"column value"}})
ContactField.joins(:contact_field_type).where({constr_base_contact_field_types: {name:"Email"}})
@goooooouwa
goooooouwa / show or hide line number.vim
Created September 4, 2014 03:09
command to show/hide line number in vim
# in normal mode
con
curl -i -F name=test -F [email protected] http://example.org/upload
@goooooouwa
goooooouwa / remove_trailing_whitespaces.rb
Last active August 29, 2015 14:08
remove trailing whitespaces. works for strings like "Window Furnishings " and "Bollards ".
ConstrBase::Trade.find_each do |trade|
if matches = /^((\S+\s+)+(\S*))\s*$/.match(trade.name)
trade.name = matches[1]
trade.save
end
end
@goooooouwa
goooooouwa / active_record_elasticsearch_associations_senarios.txt
Last active March 13, 2019 08:29
work with ActiveRecord associations in Elasticsearch
if model A has one or many model B( equals model B belongs to model A), to update index of A when B gets updated, just write in B:
belongs_to A, touch: true
if model A belongs to model B( equals model B has one or many model A), to update index of A when B gets updated, just write in B:
has_many :As
after_update { self.As.each(&:touch) }
if model A has and belongs to many model B, to update index of A when one of Bs gets updated, just write in A
has_and_belongs_to_many :Bs, after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]