Skip to content

Instantly share code, notes, and snippets.

View leandro's full-sized avatar
🏠
Working from home

Leandro Camargo leandro

🏠
Working from home
View GitHub Profile
@leandro
leandro / brazilian_cardinal_currency.rb
Created May 31, 2017 13:54
Brazilian cardinality in ruby
require 'brazilian_cardinal_number'
class BrazilianCardinalCurrency
class << self
def cardinal(value)
return 'grátis' if value.zero?
abs_value = value.abs
cents = abs_value.to_s.split('.')[1].to_s[0, 2].to_i
integer = abs_value.to_i
@leandro
leandro / last.fm-free-tracks.js
Last active November 11, 2016 18:16
It lists the free tracks available in Last.fm (expected to run inside webtask.io)
var jsdom = require("jsdom");
var _ = require("lodash");
var WebCrawler = {};
var Storage = {};
!function (crawler) {
var getFreeDownloadableTracks = function(page, finishCallback) {
jsdom.env({
url: "http://www.last.fm/music/+free-music-downloads?page=" + page,
@leandro
leandro / myURL.js
Last active September 8, 2016 14:49
Lea Verou's proposal regarding URL class in https://leaverou.github.io/jsux/
!function() {
var myURL = function(str) {
this.str = str;
this.toString = function() { return str; };
}
function createFromString(urlString) {
return new myURL(urlString);
}
@leandro
leandro / checkout-redirect-patch.diff
Created August 24, 2016 00:03
Redirect to Cart page when shipping info is not filled yet
diff --git a/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php b/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php
index 5563396..681a859 100644
--- a/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php
+++ b/wordpress/wp-content/plugins/woocommerce/includes/class-wc-cart.php
@@ -105,6 +105,7 @@ class WC_Cart {
* Constructor for the cart class. Loads options and hooks in the init method.
*/
public function __construct() {
+ add_action( 'wp_head', array( $this, 'force_shipping_to_be_set_on_checkout' ) );
add_action( 'wp_loaded', array( $this, 'init' ) ); // Get cart after WP and plugins are loaded.
@leandro
leandro / gist:14d5477b2cfb15b22a7fedaa90201c69
Last active October 5, 2017 00:55 — forked from elebescond/gist:1433137
Generate a .pot template from a wordpress theme/codebase and update .po files
find . -iname "*.php" > /tmp/my_php_files.txt
# new template
xgettext --from-code=utf-8 -d my_dir -f /tmp/my_php_files.txt -o languages/my_theme.pot
# update template
xgettext --from-code=utf-8 -d my_dir -j -f /tmp/my_php_files.txt -o languages/my_theme.pot
# To update the already translated .po files and add new strings to them, run:
@leandro
leandro / data.rb
Last active June 24, 2016 04:51
One user per week
class Data < ActiveRecord::Base
belongs_to :user
before_create :user_allowed_to_create?
private
def user_allowed_to_create?
offset = Date.today.cweek % User.count
User.offset(offset).limit(1).pluck(:id).first == user_id
end
@leandro
leandro / dig_and_import.js
Created November 26, 2015 16:48
Simple port of Ruby 2.3' Hash#dig into Javascript's Underscore lib
(function() {
'use strict';
_.mixin({
// Similar to `_.dig` but uses `window` as the starting point object.
import: function(namespace, initialValue) {
return _.dig(window, namespace, true, initialValue);
},
// Fetch or create the nested objects referenced in @namespace@ (String or Array)
@leandro
leandro / run_with_timeout.rb
Last active September 9, 2015 16:39
Right way to make process calls and use Timeout.timeout (example).
def run_with_timeout(cmd, limit)
Open3.popen3(cmd) do |_, stdout, stderr, wait_thr|
output, pid = [], wait_thr.pid
begin
Timeout.timeout(limit) do
output = [stdout.read, stderr.read]
Process.wait(pid)
end
rescue Errno::ECHILD
rescue Timeout::Error
@leandro
leandro / compass-custom
Last active August 29, 2015 14:22
Making a compass executable that runs specific versions of compass
#!/usr/bin/env ruby_executable_hooks
#
# This file was generated by RubyGems.
#
# The application 'compass' is installed as part of a gem, and
# this file is here to facilitate running it.
#
require 'rubygems'
@leandro
leandro / config__routes.rb
Created May 20, 2015 23:50
Reproducing issue #20204 in rails/rails (4.2.1)
Rails.application.routes.draw do
resources :fruits do
mount TestEngine::Engine => '/test', as: :test_1
end
resources :vegetables do
mount TestEngine::Engine => '/test', as: :test_2
end
end