Skip to content

Instantly share code, notes, and snippets.

Cyril Duchon-Doris Startouf

Block or report user

Report or block Startouf

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@Startouf
Startouf / specification_pattern.rb
Created Mar 20, 2019
Specification pattern implementation for ActiveJob
View specification_pattern.rb
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 Oct 25, 2018
jsonapi_suite sideloading of in-memory associations
View sideloading_in_memory.rb
# 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 Oct 25, 2018
jsonapi_suite for In memory Iterables adapter
View in_memory_iterable_adapter.rb
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 Apr 30, 2018
Small gist illustrating using has_one instead of belongs_to to take advantage of parent passing
View code.rb
class Moderation
belongs_to :content
end
class Content
has_many :moderations
end
class ModerationResource < ApplicationResource
type :'moderation'
@Startouf
Startouf / rollbar_notifier.rb
Created Apr 5, 2018
Rollbar notifier for Exception notifier
View rollbar_notifier.rb
# 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 Mar 21, 2018
Mongoid Database cacheable
View mongoid_cacheable.rb
# 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 Feb 9, 2018
Mongoid adapter for jsonapi_suite
View gist:91d3b5eab004f04dfbb0d068b311ff3e
# 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 / sideposting_educations_with_schools_jsonapi_request_payload.json
Last active Nov 30, 2017
Sideposting with json:api, with `method` allowing to desambiguate operations
View sideposting_educations_with_schools_jsonapi_request_payload.json
{
"data": {
"type": "profile",
"relationships": {
"educations": {
"data": [
{
"type": "profile/education",
"temp-id": 0,
"method": "create"
@Startouf
Startouf / gist:937492b1ad86aa035b9660a519802f18
Last active Sep 8, 2017
Conditional rendering of relationship for tricky situation
View gist:937492b1ad86aa035b9660a519802f18
```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 / Share rspec example for a sideposting concern
Created Jul 23, 2017
Refactor jsonapi-suite sideposting into shared examples for RSpec
View Share rspec example for a sideposting concern
# 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
You can’t perform that action at this time.