Skip to content

Instantly share code, notes, and snippets.

View vojtad's full-sized avatar

Vojta Drbohlav vojtad

  • SlidesLive
  • Czech Republic
View GitHub Profile
# frozen_string_literal: true
class CreateSignedCloudfrontUrl < Service
attr_reader :url, :expires_at
def initialize(url:, expires_in:)
@url = url
@expires_at = expires_in.from_now.to_i
end
@vojtad
vojtad / aws_s3_multipart_helper.js
Last active April 26, 2020 06:32
Helper to enable client-side signing for AWS S3 uploading for Uppy with AWS S3 Multipart. Set callbacks for AWS S3 Multipart plugin to methods from this heper (https://uppy.io/docs/aws-s3-multipart/#createMultipartUpload-file).
import AWS from 'aws-sdk';
export default class AwsS3MultipartHelper {
constructor(options) {
this.options = {
bucket: options.bucket,
acl: options.acl || 'public-read',
expires: options.expires || 5 * 60,
};
@vojtad
vojtad / action_view_partial_renderer_collection_caching.rb
Last active March 22, 2019 16:33
Monkey patch ActionView::CollectionCaching#cache_collection_render included in ActionView::PartialRenderer. This helps prevent N+1 for uncached entries when rendering cached collection.
module MonkeyPatches
#
# Monkey patch ActionView::CollectionCaching#cache_collection_render included in ActionView::PartialRenderer.
# This helps prevent N+1 for uncached entries when rendering cached collection.
# This patch allows us to specify modify_uncached_collection Proc. The Proc is called with collection of uncached entries as argument. Its result should be Array. It is used as new collection for rendering.
#
# Setup:
#
# Add this line to monkey_patches.rb initializer.
# ActionView::PartialRenderer.prepend(MonkeyPatches::ActionViewPartialRendererCollectionCaching)
@vojtad
vojtad / bench-rails-module-exists.md
Last active December 6, 2018 20:35
Benchmark: two ways of checking if module exists

Benchmarking two ways of checking if module is defined using its name in string.

begin
  'ModuleName'.constantize
rescue NameError
  # ignored
end
Object.const_defined?('ModuleName')
@vojtad
vojtad / bench-ruby-concat.md
Last active December 10, 2018 19:00
Benchmark: ruby string concatenation

Runtime

@a = 'A', @b = 'B'
Warming up --------------------------------------
s = s + @a; s = s + @b
                        49.000  i/100ms
     s += @a; s+= @b    51.000  i/100ms
s = "#{s}#{@a}"; s = "#{s}#{@b}"
                        50.000  i/100ms
s.concat(@a).concat(@b)
@vojtad
vojtad / benchmarks.md
Created November 21, 2018 00:00
Rails JSON rendering benchmarks

Rendered JSON is ~250 KB in size.

Runtime benchmarks

Warming up --------------------------------------
   render json: data    20.000  i/100ms
render json: data.to_json
                        27.000  i/100ms
render json: ActiveSupport::JSON.encode(data)
                        26.000  i/100ms