In order to have monitoring for Sidekiq (like queue sizes, last run of Sidekiq) your application should have a monitoring route which returns a json looking like this: { "sidekiq": {
...queue_sizes": { "dead": 0, "retries": 0, "monitoring": 0, "low_priority": 0, "mails": 0, "default": 0, "elasticsearch": 0, "high_priority": 0, "file_upload": 0, "scheduled": 0 }, "active_workers": 0 }, "timestamps": {
Bundler allows you to specify the name of the Gemfile you want to bundle with the BUNDLE_GEMFILE environment variable. BUNDLE_GEMFILE=Gemfile.rails.7.2 bundle By default, bundler will look...
...have multiple Gemfiles in your project, which cannot all be named Gemfile. Let's say for example, you maintain a gem and want to run automated tests against multiple rails...
When you want to filter records in a model where a string column roughly matches a given term, you can use PostgreSQL’s trigram similarity search. Writing a fuzzy query...
User.where("similarity(name, ?) > 0.3", "John") This finds all users where the name is similar to "John" with a similarity score above 0.3. You can tune the threshold:
Plot graphs in Ruby WebGraphviz renders in your browser via JavaScript (to store the rendered graph, extract the SVG using your browser's DOM inspector) GraphViz with DOT: Online...
...graphviz.christine.website/ or offline https://makandracards.com/makandra/1589-auto-generate-state_machine-graphs-as-png-images Balsamiq Draw.io Excalidraw Asciiflow Google Presentation Egon.io: Domain storytelling with replay functionality (good to visualize and present flows) Gnuplot Ruby Bindings: Especially for more...
Note: You won't need this for single lines of text. In this case it is better to just use the text-overflow property: Use CSS "text-overflow" to truncate...
...use -webkit-line-clamp in your CSS/SASS to natively render an ellipsis (...) after a specific amount of lines for a multi-line text in your HTML. Earlier, it was necessary...
to create a Gallery that has a name and has_many :images, which in turn have a caption to offer the user a single form to create...
...with any number of images immediate uploads with a progress bar per image a snappy UI Enter jQuery File Upload. It's a mature library that can do the job...
When you need to see the content of a page (i.e. not all the HTML but the relevant text body) you can do pp (html_content) pp will format the...
...html String human readable pretty printed where html_content can be replaced by one of the following commands: Rails body or response.body Capybara: page.driver.html.content page.body Webrat: Nokogiri::HTML(response.body).content...
Rails comes with a Rake task notes that shows code comments that start with "TODO", "FIXME", or "OPTIMIZE". While it's generally not good practice to leave them in your...
...not yet available. To keep track of them, run rake notes. Its output looks something like this: $ rake notes app/controllers/frontend/media_documents_controller.rb: * [ 6] [TODO] should be part of a publication workflow app/helpers/frontend/slider_helper.rb...
A recent patch level Ruby update caused troubles to some of us as applications started to complain about incompatible gem versions. I'll try to explain how the faulty state...
...care of a few things: The new Ruby version is installed The Bundler version stated in the Gemfile.lock is installed Geordi is installed (for database dumps) The gems of the...
When giving a presentation where you do some coding, the font size you usually use is probably a bit too small and makes code hard to read for users on...
...smaller screens or low-bandwidth connections when the image quality is lower. Here are two solutions. Presentation Mode RubyMine offers a "Presentation Mode" which you can use. Simply navigate to...
When you need to store structured data (like Ruby hashes) in a single database column with ActiveRecord, a simple way is to use PostgreSQL's jsonb columns. ActiveRecord will automatically...
...serialize and deserialize your Hash to and from JSON, and you can index JSON paths for fast reads. As an alternative, ActiveRecord::Store offers a way to store hashes in...
...at the same time. When assets did not change, we do not want to spend time compiling them. Here is our solution for all that. Its concept should work for...
...all test suites. Copy the following to config/initializers/webpacker_compile_once.rb. It will patch Webpacker, but only for the test environment: # Avoid hardcoded asset hosts in webpack, otherwise all chunks would be loaded...
...are usually believed to have worse performance than those defined via def. Hence, developers sometimes prefer using class_eval to define methods using def, like this: class_eval "def #{attribute...
...for_realsies?; do_things; end" You can benchmark methods defined like this and will see that those defined via def actually do perform better. Basically, it ranks like this:
When internationalizing your Rails app, you'll be replacing strings like 'Please enter your name' with t('.name_prompt'). You will be adding keys to your config/locales/*.yml files over...
...the right place is a challenging task. The gem i18n-tasks has you covered. See its README for a list of things it will do for you. Note
...a new callback to your model that (e.g.) caches some data when it is saved. Now you need to run that callback for the 10000 existing records in the production...
Write a clever migration, possibly by embedding the model into the migration script. Open the Rails console after deployment and re-save every single record. You should probably...
tl;dr: Ruby's Bundler environment is passed on to system calls, which may not be what you may want as it changes gem and binary lookup. Use Bundler.with_original...
...env to restore the environment's state before Bundler was launched. Do this whenever you want to execute shell commands inside other bundles. Example outline Consider this setup: my_project/Gemfile...
Boot partitions from installations prior to the 16.04 era are terribly small. When you install updates and encounter errors due to a full /boot partition, consider risizing it.
...can't do the steps described below, ask someone experienced to help you out. This has worked 100% so far. 1 out of 1 tries. ;) Scenario A: There is unassigned...
When you do a bitwise copy using the dd tool you will not see any output until it completes or an error occurs. However, you can send a command signal...
...to the process to have it show its progress so far. From another terminal, simply call (be root or use sudo): pkill -USR1 dd This makes dd write something like...
...listen to other events. Quirks The "default behavior" in this case is not to show an alert, and preventing this default means the alert is being shown. In ancient browsers...
...customize the alert text, but this is no longer possible. You might want to set event.returnValue = true, as a truthy returnValue was necessary to trigger the alert in Chrome...
If you want to build a small CLI application, that supports more advanced inputs than gets, I recommend using the cli-ui gem. It's a small dependency-free library...
...that provides basic building blocks, like an interactive prompt: require "cli/ui" CLI::UI::StdoutRouter.enable puts CLI::UI.fmt "a small {{red:demo}}" # supports h, j, k, l, arrows and even filtering...
In Rails 3.1+, instead of defining a separate up and down method you can define a single method change: class AddComparisonFieldsToReport < ActiveRecord::Migration def change add_column :reports, :compare, :boolean...
...update "UPDATE reports SET compare = #{quoted_false}" add_column :reports, :compare_start_date, :date add_column :reports, :compare_end_date, :date end end Migrating up works as expected:
Sometimes we write plain SQL queries in migrations so we don't have to mock ActiveRecord classes. These two migrations do the same: class Migration1 < ActiveRecord::Migration[5.2]
...Migration2 < ActiveRecord::Migration[5.2] def up add_column :users, :trashed, :boolean update("UPDATE users SET trashed = #{quoted_false}") end end The plain SQL migration is less code, but has a...
Let's say we have posts with an attribute title that is mandatory. Our example feature request is to tag these posts with a limited number of tags. The following...
...In most cases you want to use Option 4 with assignable values. The basic setup for all options looks like this: config/routes.rb Rails.application.routes.draw do root "posts#index" resources :posts, except...
Let's say you have a form that you render a few times but you would like to customize your submit section each time. You can achieve this by rendering...
...form partial as layout and passing in a block. Your template or partial then serves as the surrounding layout of the block that you pass in. You can then yield...