Skip to content

Instantly share code, notes, and snippets.

View apauly's full-sized avatar
😎

Alex Pauly apauly

😎
View GitHub Profile
@apauly
apauly / graphiti.rb
Created August 17, 2020 14:57
Graphiti requires writable id also for relations
require 'graphiti'
class User; end
class Post; end
class UserResource < Graphiti::Resource
attribute :id, :integer_id, writable: false
has_many :posts
end
@apauly
apauly / graphiti.rb
Last active August 17, 2020 14:31
Graphiti id attribute with writable: false
require 'graphiti'
class PostResource < Graphiti::Resource
attribute :id, :integer_id, writable: false
attribute :title, :string, writable: true
end
params = {
data: {
id: 123,
@apauly
apauly / benchmark.rb
Last active January 9, 2020 13:25
Benchmark for fix in ActiveSupport
require 'json'
require 'active_support/all'
#
# Monkeypatch Hash class in order to be able to compare both version
#
class Hash
def as_json(options = nil) #:nodoc:
# create a subset of the hash by applying :only or :except
subset = if options
@apauly
apauly / action_text_initializer.rb
Last active September 4, 2020 17:15
ActionText: Use preloaded ActiveStorage attachments
Rails.application.config.after_initialize do
ActiveSupport.on_load(:action_text_content) do
class_eval do
attr_accessor :rich_text
def attachment_for_node(node, with_full_attributes: true)
attachment = ActionText::Attachment.from_node(node, rich_text&.find_preloaded_attachable(node))
with_full_attributes ? attachment.with_full_attributes : attachment
end
@apauly
apauly / test.rb
Last active June 26, 2019 14:18
Nested scopes bug report
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# gem "rails", "~> 5.1.0" # works
@apauly
apauly / preload_counts.rb
Last active July 7, 2022 00:50
Preload the count of associations similar to the familiar "preload"
##
# PreloadCounts
#
# Usage:
# collection = Post.limit(10).preload_counts(:users)
# collection.first.users.count # fires one query to fetch all counts
# collection[1].users.count # uses the cached value
# collection.last.users.count # uses the cached value
#
# Call `::PreloadCounts.enable!` inside of an initializer to enable preloading of association counts
@apauly
apauly / gist:7917906
Last active May 4, 2016 06:00
Mixin for responsive sprites with Sass
@mixin responsive-sprite($map, $icon){
$icon-file: sprite-file($map, $icon);
$icon-width: image-width($icon-file);
$icon-height: image-height($icon-file);
$sprite-file: sprite-path($map);
$sprite-width: image-width($sprite-file);
$sprite-height: image-height($sprite-file);
$space-top: floor(nth(sprite-position($map, $icon), 2));
@apauly
apauly / application.css
Last active December 20, 2015 01:08
Compass Retina "spriting": use a seperate css file with data-urls of .svg images instead of a normal "sprite"
@import "svg-sprites.css"; // client-side import; just like a large sprite image