Skip to content

Instantly share code, notes, and snippets.

View afcapel's full-sized avatar

Alberto Fernández-Capel afcapel

View GitHub Profile
import { Controller } from 'stimulus'
export default class extends Controller {
connect() {
if ( typeof window.ga != 'undefined' ) {
const currentExperiments = this.experiments()
Object.keys(currentExperiments).forEach((experimentId) => {
ga('set', 'expId', experimentId)
Rails.application.eager_load!
ActiveRecord::Base.descendants.each do |model|
next unless model.table_exists?
model.columns.each do |col|
next unless col.name =~ /_?id\Z/
if col.type == :integer && col.sql_type != "bigint"
puts "#{model.table_name} #{col.name} is of type #{col.sql_type} but should be bigint"
class Invoice < ApplicationRecord
include AccountScoped
end
class InvoicesController < ApplicationController
def show
@invoice = Invoice.in_current_account.find(params[:id])
end
end
# app/models/current.rb
class Current < ActiveSupport::CurrentAttributes
attribute :account, :person
def person=(person)
super
self.account = person&.account
end
end
# app/controllers/concerns/authentication.rb
# (Authorisation is different than Authentication and so it is handled in a different concern)
module Authentication
extend ActiveSupport::Concern
included do
before_action :authenticate
helper_method :logged_in?
end
# app/models/concerns/account_scoped.rb
module AccountScoped
extend ActiveSupport::Concern
included do
belongs_to :account
before_validation :set_current_account, on: :create
before_destroy :belongs_to_current_account
# Returns whether a can be split in max_blocks with a
# max sum equal or lower to max_sum
def can_split_in_blocks?(a, max_sum, max_blocks)
n_blocks = 0
current_block_sum = 0
a.each_with_index do |elm, i|
if current_block_sum + elm <= max_sum && i > 0
# Use the current block
[1] pry(main)> rating = Rating.new
=> #<Rating id: nil, comment: nil>
[2] pry(main)> erb = ERB.allocate
=> #<ERB:0x007f841b98ba10>
[3] pry(main)> proxy = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(erb, :result)
=> #<ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy:0x3fc20dca5a08>
[5] pry(main)> erb.instance_variable_set :@src, "User.steal_all_passwords;"
@afcapel
afcapel / mod_pow.c
Last active August 29, 2015 14:14
Write a C extension in Ruby
#include "ruby.h"
#define TO_BIGNUM(x) (FIXNUM_P(x) ? rb_int2big(FIX2LONG(x)) : x)
static VALUE zero = INT2NUM(0);
static VALUE one = INT2NUM(1);
static VALUE c_mod_pow(VALUE self, VALUE exp, VALUE mod){
VALUE result = one;
require 'delegate'
class NumberDecorator < SimpleDelegator
def in_dollars
"$#{to_s}"
end
def in_euros
"€#{to_s}"