...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...
Most browsers have built-in drag and drop support for different page elements like text and images. While this may be useful in most situations, it may become annoying in...
...functionality. This does no longer work. You may now achieve this by explicitly preventing the startdrag-event:
const noDragElement = document.querySelector('no-drag-pls') noDragElement.addEventListener('dragstart', event => event.preventDefault())
...If you are plucking from the id column in particular you can also say: User.active.ids => [1, 5, 23, 42] For a DISTINCT selection, use distinct on your scope (not the...
...resulting array). Article.distinct.pluck(:state) # SELECT DISTINCT state FROM articles => ['draft', 'published'] In Rails 3 and 4 you must use uniq instead of distinct: Article.uniq.pluck(:state) # SELECT DISTINCT state FROM articles...
When testing your command line application with Aruba, you might need to stub out other binaries you don't want to be invoked by your test. Aruba Doubles is a...
Install the gem as instructed by its README, then put this Before block somewhere into features/support: Before do ArubaDoubles::Double.setup prepend_environment_variable 'PATH', ArubaDoubles::Double.bindir + ':' end
The issue: You are using stub_const to change a constant value for your test. stub_const "SomeClass::CONST", 'test' All of a sudden, tests fail with undefined method 'some...
...method' for # . The reason When using stub_const before the Class containing the constant has been loaded, a module is automatically created with the name. Since RSpec does no autoloading...
...version understands? Did you double-check your @font-face declarations with all the hacky syntax that is required? Compare them with other declarations you find on the web.
...same name? This usually leads to trouble, but there are workarounds. Do IE's security settings prevent the download of webfonts? (see below) IE Security Settings can prevent font download...
In Rails, we usually have a mailer setup like this: class MyMailer < ActionMailer::Base def newsletter mail to: 'receiver@host.tld', from: 'sender@host.tld', subject: 'My mail' end end If you want to...
...Rails will print the mail's text representation to your log/development.log. You may also save this output as-is to an .eml file and open it with your mail client...
...use the puppetdb_query feature. This can result in more complex code but has several benefits: you can use more complex puppetdb queries to get the resources you want than...
...db', $vhost['parameters']['dbuser'], { 'password' => $vhost['parameters']['dbpassword'], 'type' => $type, 'extensions' => $vhost['parameters']['dbextensions'], 'schemas' => $vhost['parameters']['dbschemas'], 'charset' => $vhost['parameters']['dbencoding'], 'collate' => $vhost['parameters']['dbcollate'], 'host' => $host, }) }
...optimize the experience. Using capistrano-rails capistrano-rails is a Gem that adds Rails specifics to Capistrano, i.e. support for Bundler, assets, and migrations. While it is designed for Asset...
...Pipeline (Sprockets) assets, it can easily be configured for Webpacker. This brings these features to the Webpacker world: Automatic removal of expired assets Manifest backups # config/deploy.rb # No need to include...
...in "Comment" with: """ This is a long comment. With multiple lines. And paragraphs. """ The step definition is part of the spreewald gem
Some users might use Adblock Plus or similar browser plugins to reduce the number of ads displayed. If you run into an issue that your application or part of an...
...our web apps. But if your application uses iframes or is embedded in another site it's more prone to it. Blocked elements most of the time appear to the...
...not possible to use variables in media queries with plain CSS. @media (max-width: var(--some-pixel-size)) {...
...} /* Does not work */ Postcss Plugin If you're using PostCSS to postprocess...
...your CSS, you can configure postcss-custom-media and add this feature: @custom-media --small-variable-name(max-width: 968px); @media (--small-variable-name) {...
...} /* works...
...about half of our curriculum deck! 🎉 We've covered the basics of your future stack and are about to dive deeper in some more advanced topics. A few things before...
...curriculum cards every other monday Read the article "Parting advice from an older Software Engineer". Try to memorize the key takeaways of 1-5 on the list. Watch Dominik's...