Skip to content

Instantly share code, notes, and snippets.

@Startouf
Startouf / script.js
Last active October 22, 2022 08:53
Modern UTM & Referral Cookie Replicator
<script id="gtm-modern-cookie-replicator">
/**
* Modern UTM & Referral Cookie Replicator
*
* Inspired from the UTMZ cookie replicator,
* whose goal was to makes a generally faithful representation
* of the old __utmz cookie from Classic Analytics. ;
*
* the "Modern" cookie replicator
* - stores and decodes the cookie as/from JSON format
@Startouf
Startouf / readme.md
Last active August 31, 2022 08:53
Update CRD for helm traefik migration from 2.4 to 2.5
{
"type": "home",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "*Don't Slack Evil Performance*"
},
"accessory": {
@Startouf
Startouf / specification_pattern.rb
Created March 20, 2019 15:16
Specification pattern implementation for ActiveJob
class SpecificationJob < ApplicationJob
include JobSpecifications
around_perform do |_, block|
init_specifications # init a bunch of instance variables to remember status of specifications
block.call # calls the #perform method of jobs
run_registered_specifications # Actually run the specs
if specifications_all_passed?
execute_if_specifications_all_passed
else
@Startouf
Startouf / sideloading_in_memory.rb
Created October 25, 2018 22:13
jsonapi_suite sideloading of in-memory associations
# In-memory associations as jsonapi_suite sideloads
# Using the trick of memoization, assuming the parent known about the children
#
# @example
# The parent could have a `field type_string, type: String`
# and a method #type that calls and memoizes `self.type_string.camelize.constantize`
#
class Parent
def association_in_memory
@Startouf
Startouf / in_memory_iterable_adapter.rb
Created October 25, 2018 20:58
jsonapi_suite for In memory Iterables adapter
module Jsonapi
module Adapters
# Adapter for resources already loaded in memory
#
# The scope is meant to be something like an Array or Iterable of objects
#
# @example Time Series
#
# scope = [
# OpenStruct.new(at: Time.now.noon, name: 'I ate'),
@Startouf
Startouf / code.rb
Last active April 30, 2018 16:43
Small gist illustrating using has_one instead of belongs_to to take advantage of parent passing
class Moderation
belongs_to :content
end
class Content
has_many :moderations
end
class ModerationResource < ApplicationResource
type :'moderation'
@Startouf
Startouf / rollbar_notifier.rb
Created April 5, 2018 09:37
Rollbar notifier for Exception notifier
# Cf
# https://github.com/rollbar/rollbar-gem/issues/711
# https://github.com/smartinez87/exception_notification/issues/413
module ExceptionNotifier
class RollbarNotifier < ExceptionNotifier::BaseNotifier
def call(exception, options = {})
@env = options[:env]
@data = (@env && @env['exception_notifier.exception_data'] || {}).merge(options[:data] || {})
@current_user_data = (@env && @env['exception_notifier.current_user.exception_data'] || {}).merge(options[:current_user] || {})
@Startouf
Startouf / mongoid_cacheable.rb
Created March 21, 2018 20:31
Mongoid Database cacheable
# Concern to include in mongoid models that need database cache
#
#
module MongoidCacheable
extend ActiveSupport::Concern
class_methods do
# Create database fields to cache an attribute
# => Cache field, (Optional Cached_at DateTime field)
#
@Startouf
Startouf / gist:91d3b5eab004f04dfbb0d068b311ff3e
Created February 9, 2018 17:29
Mongoid adapter for jsonapi_suite
# lib/jsonapi/adapters/transactionless_mongoid_adapter.rb
module Jsonapi
module Adapters
# Mongoid transactionless adapter
# See https://github.com/jsonapi-suite/jsonapi_compliable/blob/master/lib/jsonapi_compliable/adapters/abstract.rb
#
# @author [Cyril]
#
class TransactionlessMongoidAdapter < JsonapiCompliable::Adapters::Abstract
def sideloading_module