Skip to content

Instantly share code, notes, and snippets.

View masasakano's full-sized avatar

Masa Sakano masasakano

View GitHub Profile
@masasakano
masasakano / inconsistency_in_was_found_error.rb
Last active May 1, 2025 21:25
Ruby Module to add constructor of *_(found?|created?) and their setter methods. Test file for Rails, but others can be included in any Ruby scripts.
module ModuleWasFounds
class InconsistencyInWasFoundError < StandardError # ModuleWasFounds::InconsistencyInWasFoundError
## Error in inconsistency in ModuleWasFound#was_found? and ModuleWasFound#was_created?
end
end
@masasakano
masasakano / rails_get_caller_info_message.rb
Created April 23, 2025 20:59
Returns the caller information String in Rails testing; in default it returns the last caller in *_test.rb so it is useful in assertions in library/module files
# Returns the String for the caller information
#
# @example To display the location of the last caller in the *_test.rb file.
# assert abc, sprintf("(%s): abc=%s", _get_caller_info_message, abc.inspect) # defined in test_helper.rb
#
# @example displays the exact line where an error occurs
# assert false, _get_caller_info_message(bind_offset: -1, prefix: true)+" Error occurs exactly at this line."
#
# @param bind_offset: [Integer, NilClass] offset for caller_locations (used for displaying the caller routine). In default (=nil), the last location in *_test.rb (such as, inside a block). If this is 0, it is useful in the case where this method is called in a test library method that is called from an original test routine. Therefore, specify "-1" to get the information of the caller itself (Second example above).
# @param fmt: [String] sprintf format. It must contain %s (for path) and %d (or %s) (for line number) in this order.
@masasakano
masasakano / test_w3c_validate_helper.rb
Created May 27, 2024 13:42
Ruby-on-Rails helper to use a local VNU server to validate HTML with W3C validator
# -*- coding: utf-8 -*-
require 'w3c_validators'
# Module to deal with W3C Validator
#
# == Usage
#
# Put this file in one of your library-load paths in the test environment.
# Then, in /test/test_helper.rb
@masasakano
masasakano / How_to_set_up_path-based_Ruby-on-Rails_I18n_environment.md
Created May 18, 2024 12:31
How to set up path-based Ruby-on-Rails I18n environment

Goals

  • Languages are :en and :ja and :fr, where :en is the default.
  • It is path-based, like /ja/articles is for Index of Article model.
  • Allow access without the locale parameter in the path like http://example.com/articles
    • in which case the locale parameter I18n.locale should be set according to the browser's request, falling back to :en

Install i18n default translations

@masasakano
masasakano / may_ignore_autocomplete_errors_for_hidden.rb
Last active November 2, 2022 22:56
Rails (test-)helper to circumvent W3C validation errors for button_to
# coding: utf-8
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
require 'w3c_validators' # Key module. Write in Gemfile: gem 'w3c_validators'
# meant to be included in /test/test_helper.rb
class ActiveSupport::TestCase
# Botch fix of W3C validation errors for HTMLs generated by button_to
@masasakano
masasakano / rails6to7esbuild.md
Created October 17, 2022 00:25
How to upgrade/migrate from Rails 6.1 to 7.0 with bootstrap

Upgrade/Migrate from Rails 6.1 to 7 with bootstrap

Background

It is a major version up, in particular:

  • Rails 6 almost dropped Sprockets in favour of Webpack
  • Rails 7 almost dropped Webpack in favour of jsbundling-rails + import maps (or esbuild) + Sprockets.

The backgrond is

@masasakano
masasakano / tee_io.rb
Created September 3, 2022 18:42
Flexible Ruby framework to temporarily suppress the output of an IO, typically STDOUT or STDERR, while the IO remains available for reading
require "tempfile"
# TeeIO to "tee" (or splice) an IO
#
# Basically this overwrites IO#write .
#
# Note this does not capture IO#syswrite
#
# The main part but {TeeIO.suppress_io} is taken from
# {https://stackoverflow.com/a/9439298/3577922}
@masasakano
masasakano / ruby_minitest_unittest_tempfile.rb
Created August 26, 2022 13:35
Ruby minitest/unit-test template for using Tempfile
gem "minitest"
require 'minitest/autorun'
require 'tempfile'
# In general, each test method should generate its own temporary files.
# With the following code, the temporary files will be automatically deleted
# at the end of the processing, even if their IOs have been alraedy closed
# and only the filename (path) were utilised during processing.
class TestUnitMyLibrary < MiniTest::Test
@masasakano
masasakano / ruby_optparse_template.rb
Created August 21, 2022 22:03
Ruby optparse extensive sample code
require 'optparse'
require 'optparse/time' # if Time is used.
#require 'ostruct'
require 'pp'
# This example file for using Ruby optparse is based on the original example
# code in Github, specifically, commit number f2b8318, or roughly for Ruby 3.1.2.
# https://github.com/ruby/optparse
# I have added several sections and modified some parts, as well as annotated
# the code extensively with my comments.
@masasakano
masasakano / country_town_dynamic_dropdown.html
Last active July 6, 2021 15:08
Dynamic dropdown menu for country-town. Town menu hidden first. It'll appear, but including optgroup country name.
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<a href="https://jsfiddle.net/masa_alpin/z1sjekvt/35/">jsfiddle</a>
<!-- country-town working with town hidden mostly4 -->
<!-- country-town dynamic dropdown menu. Town menu hidden first. It'll appear, but including optgroup country name, with tidied-up code. -->
<form action="/persons" accept-charset="UTF-8" method="post">