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 / 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 / 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 / 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 / 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 / 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 / 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 / spec__models__concerns__durable_record_spec.rb
Last active November 20, 2018 02:12
Practical (or too dirty?) way of testing something
require 'spec_helper'
shared_examples_for 'durable_record' do
let(:duration) { nil }
let(:end_date) { nil }
let(:start_date) { nil }
let(:instance) { described_class.new }
let(:attributes) do
{ duration: duration, end_date: end_date, start_date: start_date }
end
var modalQuarteirao = $('#modal-quarteirao')
, botaoQuarteiraoCancelar = modalQuarteirao.find('.cancelar');
new ProtetorDeAlteracoes(modalQuarteirao, botaoQuarteiraoCancelar);
@leandro
leandro / controller.rb
Last active March 19, 2019 14:45
Alterar nome do Admin mesmo com a senha em branco.
# frozen_string_literal: true
# Main Backoffice
class Backoffice::AdminsController < BackofficeController
before_action :set_admin, only: %i[edit update destroy]
after_action :verify_authorized, only: :new
after_action :verify_policy_scoped, only: :index
def index
@admins = policy_scope(Admin)
def create
@block_edit = false
debit_params = BankDebit.new(permitted_params[:bank_debit])
.attributes.symbolize_keys.merge(user_id: current_user.id)
Bank.transaction do
current_user.up_score!(1.3) if current_user.Potencial?
create_plot_debits!(debit_params)
create_debits!(debit_params)
redirect_to cash_path, notice: 'Lançamento realizado com sucesso'