Skip to content

Instantly share code, notes, and snippets.

ruby 3.1.4p223 (2023-03-30 revision 957bb7cb81) [x86_64-darwin21]
Warming up --------------------------------------
       concatenation   449.658k i/100ms
          array join   249.847k i/100ms
Calculating -------------------------------------
       concatenation      4.375M (± 2.5%) i/s -     22.033M in   5.039031s
          array join      2.508M (± 4.3%) i/s -     12.742M in   5.090572s

Comparison:
@nicolas-brousse
nicolas-brousse / form--base_component.rb
Created December 2, 2020 15:05
[WIP] Share of a ViewComponent::FormBuilder
# frozen_string_literal: true
module Form
class BaseComponent < ApplicationComponent
class << self
attr_accessor :default_options
end
include Components::Validations
// Loads stimulus controllers from ../components/ folder
//
// Normal output:
// button/button_controller.js => data-controller="button--button"
// form/text_field/text_field_controller.js => data-controller="form--text-field--text-field"
//
// Expected output:
// button/button_controller.js => data-controller="button"
// form/text_field/text_field_controller.js => data-controller="form--text-field"

Description

My issue was about ordering and pagination. I was ordering a query on a column (of_the_moment_weight) that could have multi times the same value. Tehcnically, all entries could have the same value.

The result is that some entry appear on multiple page. But it shouldn't be the case.

Solution

Use two columns for ordering.

@nicolas-brousse
nicolas-brousse / 01_README.md
Last active January 13, 2023 23:04
Rails health check

HealthCheck

Install

Add the following line to your Gemfile

gem "server_health_check-rails"
@nicolas-brousse
nicolas-brousse / 01_README.md
Last active February 15, 2019 13:12
[WIP] A simple decorator for Rails

Usage

🚀 I create the dekorator gem if this interrest you.

Get started

Copy ApplicationDecorator inside app/decorators/application_decorator.rb and DecoratorsHelper inside decorators_helper.rb.

Then you could create and use your own decorators by create new class that extend from ApplicationDecorator.

@nicolas-brousse
nicolas-brousse / list_dependencies.rb
Last active October 25, 2018 18:38
Get dependencies list of a Ruby project by reading its Gemfile and Gemfile.lock
#!/usr/bin/env ruby
# frozen_string_literal: true
# USAGE:
# $ ./list_dependencies.rb
# $ ./list_dependencies.rb /path/to/project
#
# @see: https://stackoverflow.com/questions/44681231/how-to-list-ruby-production-only-dependencies-using-gemfile-lock-and-lockfilepar
@nicolas-brousse
nicolas-brousse / file_size_validator.rb
Created September 12, 2018 11:11
Rails ActiveStorage FileSizeValidator
# frozen_string_literal: true
module ActiveStorage
module Validations
class FileSizeValidator < ActiveModel::EachValidator
include ActiveSupport::NumberHelper
MESSAGES = { minimum: :file_size_too_short, maximum: :file_size_too_long }.freeze
CHECKS = { minimum: :>=, maximum: :<= }.freeze
class Mailcatcher < Formula
desc "Catches mail and serves it through a dream."
homepage "https://github.com/sj26/mailcatcher"
url "https://github.com/sj26/mailcatcher/archive/v0.6.5.zip"
sha256 "393303738ec6c7e0254b22b3f6b8f7988363791890516f820e90372afef4d65c"
version "0.6.5"
def install
ENV["GEM_HOME"] = libexec
system "gem", "build", "mailcatcher.gemspec"

Usage

$ ruby fake_smtp_server
require "net/smtp"