Skip to content

Instantly share code, notes, and snippets.

Avatar
☀️

Sunny Ripert sunny

☀️
View GitHub Profile
View blog_article_seeds.rb
# 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 Sep 8, 2021
Stimulus controller to show/hide elements
View select_definitions_controller.js
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 Aug 18, 2021
Pagy Standalone to test trim + custom URLs
View pagy_standalone_app.ru
# 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
View 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 Oct 5, 2020
Show i18n keys when locale is "translate"
View i18n.rb
# config/initializers/i18n.rb
I18n::Backend::Simple.include(I18n::Backend::TranslateLocale)
@sunny
sunny / application_controller.rb
Last active Oct 6, 2020
Rails helper so that "?_locale_keys=1" in URL shows locale keys
View application_controller.rb
class ApplicationController < ActionController::Base
include I18nHelper
end
@sunny
sunny / rails_bundler_require_benchmark.rb
Created Apr 6, 2020
Show time to require each dependency in your application's Gemfile
View rails_bundler_require_benchmark.rb
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,
View actor_brule.rb
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
View snail.rb
class Snail
include Enumerable
def initialize(data)
@data = data
@direction = :right
@x = 0
@y = 0
end
@sunny
sunny / detect_unused_tables_and_columns.rb
Last active Mar 29, 2020
Detect unused tables and columns
View detect_unused_tables_and_columns.rb
# Paste this a your production console to detect which tables are empty, which
# columns are empty and which columns contain a single value.
#
# Adapted from http://blog.ianenders.com/coding/2013/07/02/detecting-unused-db-fields-in-rails.html
connection = ActiveRecord::Base.connection
connection.tables.each do |t|
puts "----> #{t}"
count_query = "SELECT COUNT(1) as count FROM #{t}"
count = connection.select_all(count_query, "Count").first['count']