Skip to content

Instantly share code, notes, and snippets.

@mp3063
mp3063 / homestead-default-php-version.txt
Created February 8, 2021 15:21
Homestead choose php version
sudo update-alternatives --config php
@mp3063
mp3063 / array_replace_keys_recursively.php
Created September 1, 2020 09:44
Replace keys with different values, $arr = ['something'=>'exm'] $set = ['something'=>'something_else'] => $arr = ['something_else'=>'exm']
function array_replace_keys_recursively(array $arr, array $set)
{
if (is_array($arr) && is_array($set)) {
$newArr = [];
foreach ($arr as $k => $v) {
$key = array_key_exists($k, $set) ? $set[$k] : $k;
$newArr[$key] = is_array($v) ? array_replace_keys_recursively($v, $set) : $v;
}
return $newArr;
@mp3063
mp3063 / git_alias.sh
Created March 10, 2020 13:58
Git alias for pretty looking logs
gloga='git log --oneline --decorate --graph --all'
@mp3063
mp3063 / getUrlParameter.js
Created January 22, 2020 08:58
Get url parameter from query string.
function getUrlParameter(name) {
name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
let regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
let results = regex.exec(location.search);
return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
};

Keybase proof

I hereby claim:

  • I am mp3063 on github.
  • I am mp3063 (https://keybase.io/mp3063) on keybase.
  • I have a public key ASBBm7w345Ese2vm170qPIf735EiFg9A6MywbptQ0uYwFQo

To claim this, I am signing this object:

@mp3063
mp3063 / _bundle_products.haml
Created August 20, 2019 13:45
Pass selected checkboxes (any value) to params in controller...
- @product.company.products.each do |product|
- if product.configurable?
- product_hash_id = {product: product.id}
.product-name
%br
=product.name
%br
-product.subproducts.each do |sub|
.ml-5
= check_box_tag "product[#{product.id}][]", sub.id
@mp3063
mp3063 / rollback_migration.sh
Created July 25, 2019 09:04
Rollback migration in Rails!
rake db:rollback STEP=1
@mp3063
mp3063 / default_scope_model_with_joins.rb
Created June 13, 2019 14:10
default scope with joins and ordering
default_scope { joins(:subproduct).order("configuration_position desc nulls last").order(sku: :asc) }
@mp3063
mp3063 / order_by_priority.rb
Created June 5, 2019 13:50
Order something by priority field integer
default_scope { order("asset_priority desc nulls last") }
@mp3063
mp3063 / csv_to_json.rb
Created May 7, 2019 11:23
Convert csv file to pretty json format and put in the file
require 'rubygems'
require 'json'
require 'csv'
def is_int(str)
# Check if a string should be an integer
return !!(str =~ /^[-+]?[1-9]([0-9]*)?$/)
end
lines = CSV.open(ARGV[0]).readlines //ARGV[0] is a path to .csv file