0BSD License

Copyright (C) 2024 by Max Chernyak

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.

THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


Date
August 11, 2024

Links to this post
Good shell script template The article Shell Script Best Practices (also saved in my History Book app) has a great template/explanation on how to write shell scripts. Code Dynamic Module Snippet for Ruby If you like making dynamic modules (the kind where you can do include MyModule.with_argument('foo')), then these 6 lines of code make writing them A way to enforce contracts in Portrayal rubygem portrayal code, portrayal gem New way (based on version 0.9.0): https://gist.github.com/maxim/4dc1765d9f81ac849325d166d14d2dfa Blog post: Camelize JSON keys in Rails Works well with portrayal, and narrative Rails app design. Add to your lib/camelize_json_keys.rb, then require and use as needed. Code snippets in Find out RAM timings (like CAS) and manufacturer on Ubuntu I honestly don’t know how this works, but got it from this SO answer. (The only one that worked for me.) P.S. Probably the same on Debian. Code Compiling Go binary for Linux when on a Mac with Apple Silicon Code snippets in this post are covered by 0BSD License Helper for creating table and model in Rails tests Here’s a simple method that you can throw into your test_helper.rb (or wherever helpers go in your test framework) that works correctly as of Rails Assertion assert_enqueued_email_with with matcher support I made a PR into Rails to add matcher support for assert_enqueued_email_with. Until that PR is merged, here’s a custom assertion Custom Puma executable for a Ruby app Sometimes I don’t like having to create required files and directories. This executable can serve your app via puma without having to create GemPatch: monkeypatch rubygems responsibly If you must extend or change a gem’s functionality, it’s a good idea to make sure that your change won’t break with the next update. There isn’t Install command line tools on MacOS Code snippets in this post are covered by 0BSD License Preferences for Rubocop ruby formatting Canonical place for my up-to-date rubocop settings now lives in narrative Rails template repo. Code snippets in this post are covered by 0BSD License Print Rails db schema or structure These simple bash functions let you conveniently see schemas of your Rails models by printing them from schema.rb or structure.sql files. schema Procfile.dev docker postgres Start postgres in docker as a process in Procfile.dev Code snippets in this post are covered by 0BSD License Rclone tips and tricks On Linux (Ubuntu in my case), if Radarr/Sonarr apps are controlled by docker compose, hooked into an Rclone mount, and started with systemd units, Ruby barewords are insidious The problem with ruby barewords is that you cannot tell by looking at them what’s going on in them. They hide whether something returns a stored SaveIf concern for Rails optimistic locking without lock_version I wrote a proposal for Rails to implement an optimistic locking feature that doesn’t require lock_version column. There are some use cases where Serve static files in Rails This shows a way to mount a directory from which Rails will serve static files. By default, path/ will serve index.html if present. Code snippets in How my LaunchD demon was setup I switched to crontab now, and it seems to work much better, but before I was trying to run periodic job with this daemon setup. From Support rails generator testing in Rspec This goes into spec/support/generator_specs_support.rb: In spec/rails_helper.rb add the following: Specs should go into Underscore ruby strings Code snippets in this post are covered by 0BSD License Camelize (camel case) ruby string First letter: upper First letter: lower See also: Underscore ruby strings Code snippets in this post are covered by 0BSD License Iterate over sorted enumerables preserving order Better optimized, no indexes Code snippets in this post are covered by 0BSD License Working with systemd units Add new systemd service (will symlink your file): Remove existing systemd service (will remove symlinked files): Reload after changing your Inspect and install a deb package Check meta info about a package: List files the package will create: Extract deb package into a local folder: Install the package with dependencies: Transferring files over ssh with scp Upload: Download: Note: add flag -C to compress files. Code snippets in this post are covered by 0BSD License Time Boolean If you like storing timestamps for your booleans, throw this snippet into your ApplicationRecord for a quick win. Let’s say your user can be Pretty Print Authorize.net Response in Rails Assuming a response object from https://github.com/AuthorizeNet/sdk-ruby. This will turn it into a Ruby hash: Code snippets in this post are covered Enums in Ruby There are many ideas out there on how to represent enum values in Ruby. They seemed fun, but overall either too complex or missing something. Here’s SQL data streaming If you ever need to provide a download stream based on data from Postgres SQL, there’s a great low level/optimized example on how to do it with