Skip to content

Instantly share code, notes, and snippets.

View sunny's full-sized avatar
☀️

Sunny Ripert sunny

☀️
View GitHub Profile
const htmlFetch = async (url, { headers, body, ...options } = {}) => {
const token = document.querySelector('meta[name="csrf-token"]').content;
options = {
headers: new Headers({
Accept: "text/html",
"Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest",
"X-CSRF-Token": token,
...headers,
# frozen_string_literal: true
class Blog::ArticlePlanter < Planter
def check(slug_en:)
Blog::Article.where(slug_en: slug_en).any?
end
def create(params)
FactoryBot.create(:blog_article, params)
end
@sunny
sunny / select_definitions_controller.js
Created September 8, 2021 22:07
Stimulus controller to show/hide elements
import { Controller } from "stimulus";
// Show/hide elements based on the value of a select element.
//
// Example:
// <div data-controller="select-definitions">
// <select
// data-select-definitions-target="select"
// data-action="change->select-definitions#change"
// >
@sunny
sunny / pagy_standalone_app.ru
Last active August 18, 2021 18:27
Pagy Standalone to test trim + custom URLs
# frozen_string_literal: true
# Self-contained, standalone Sinatra app usable to play with pagy
# and/or easily reproduce any pagy issue.
# Copy this file in your own machine and
# ensure rack is installed (or `gem install rack`)
# USAGE:
# rackup -o 0.0.0.0 -p 8080 pagy_standalone_app.ru
@sunny
sunny / keybase.md
Created November 9, 2020 08:26
keybase.md

Keybase proof

I hereby claim:

  • I am sunny on github.
  • I am sunfox42 (https://keybase.io/sunfox42) on keybase.
  • I have a public key ASDTUFctmlgvsQC0k66qtvmcGhmcO_CtzpmW0D-cdx-xSAo

To claim this, I am signing this object:

@sunny
sunny / i18n.rb
Created October 5, 2020 08:07
Show i18n keys when locale is "translate"
# config/initializers/i18n.rb
I18n::Backend::Simple.include(I18n::Backend::TranslateLocale)
@sunny
sunny / application_controller.rb
Last active October 6, 2020 07:57
Rails helper so that "?_locale_keys=1" in URL shows locale keys
class ApplicationController < ActionController::Base
include I18nHelper
end
@sunny
sunny / rails_bundler_require_benchmark.rb
Created April 6, 2020 08:32
Show time to require each dependency in your application's Gemfile
require "benchmark"
require "rubygems"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __dir__)
require "bundler/setup"
# https://github.com/rubygems/rubygems/blob/a753447de8fb92dad30e927656984aa2b7765f3d/bundler/lib/bundler/runtime.rb#L46
REQUIRE_ERRORS = [
/^no such file to load -- (.+)$/i,
/^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
class ComputePrice < Actor
play ComputePricePerDay,
if: -> input { input.selected_at < Time.utc(2020, 9, 1) }
play ComputePricePerMinute,
if: -> input { Time.utc(2020, 9, 1) <= input.selected_at }
play ApplyDiscount,
if: -> input { Time.utc(2020, 6, 1) <= input.selected_at }
end
class ComputePricePerDay < Actor
class Snail
include Enumerable
def initialize(data)
@data = data
@direction = :right
@x = 0
@y = 0
end