Skip to content

Instantly share code, notes, and snippets.

@Startouf
Startouf / google_analytics_turbolinks5.js
Created August 20, 2016 22:09
Google Analytics for Turbolinks 5 (& Rails 5)
// Google Analytics code to work with Turbolniks 5
this.GoogleAnalytics = (function() {
function GoogleAnalytics() {}
GoogleAnalytics.load = function() {
var firstScript, ga;
window._gaq = [];
window._gaq.push(["_setAccount", GoogleAnalytics.analyticsId()]);
window._gaq.push(['_setAllowAnchor', true]);
@Startouf
Startouf / algolia_search_for_jsonapi_and_mongoid.rb
Created June 22, 2017 15:04
Search adapters for Jsonapi-suite
# Background : jsonapi-powered search controller
class MySearchController < JsonapiPoweredController
jsonapi resource: ::Public::Search::ProfessionalResource
def index
render_jsonapi(search_scope)
end
def search_scope
Searcher::SearchScopeProxy.new(
collection: Professional,
@Startouf
Startouf / Share rspec example for a sideposting concern
Created July 23, 2017 13:55
Refactor jsonapi-suite sideposting into shared examples for RSpec
# spec/requests/api/create_post_spec.rb
require 'rails_helper'
require 'requests/api/concerns/commentable_sidepost_spec.rb'
describe API::V1::PostsController, type: :request do
let(:user) { create(:user) }
let(:author) { user }
let(:post_attributes) do {
title: Faker::Lorem::Sentence.new,
author_id: author.id.to_s
@Startouf
Startouf / gist:937492b1ad86aa035b9660a519802f18
Last active September 8, 2017 00:16
Conditional rendering of relationship for tricky situation
```ruby
class User
embeds_many :attacker_performance
embeds_many :defender_performance
has_many :matches_as_attacker
has_many :matches_as_defender
end
class AttackerPerformance
embedded_in :user
@Startouf
Startouf / sideposting_educations_with_schools_jsonapi_request_payload.json
Last active November 30, 2017 22:48
Sideposting with json:api, with `method` allowing to desambiguate operations
{
"data": {
"type": "profile",
"relationships": {
"educations": {
"data": [
{
"type": "profile/education",
"temp-id": 0,
"method": "create"
@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
@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 / 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 / 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 / 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'),