This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Invoice < ApplicationRecord | |
include AccountScoped | |
end | |
class InvoicesController < ApplicationController | |
def show | |
@invoice = Invoice.in_current_account.find(params[:id]) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/current.rb | |
class Current < ActiveSupport::CurrentAttributes | |
attribute :account, :person | |
def person=(person) | |
super | |
self.account = person&.account | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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;" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'delegate' | |
class NumberDecorator < SimpleDelegator | |
def in_dollars | |
"$#{to_s}" | |
end | |
def in_euros | |
"€#{to_s}" |