Module imports are hoisted (internally moved to the beginning of the current scope). Therefore, it doesn’t matter where you mention them in a module and the following...
Footgun example When you're not aware of import hoisting you may be surprised that your code runs in a different order than you see in the source file...
...to lock a user in devise. Using the lockable module Customizing the user account status validation when logging in. It depends on your requirements which methods works best.
...user on soft delete We recommend to use option 2 when you want to couple the lock to the model's soft delete logic. Option 1 might also work when...
...use puppet-lint to find dead code in your project: # You probably need to set some ENV Variables, see https://github.com/voxpupuli/puppet-ghostbuster#environment-variables export HIERA_YAML_PATH="/home/bob/code/puppet/hiera.yaml" export PUPPETDB_URL...
...that's not a float! This occurs because JavaScript uses double precision floats to store numbers. So according to IEEE floating point definition only numbers between...
...and 2^53 - 1 (9007199254740991) can safely be represented in JavaScript. Note that ECMAScript 6 will probably also offer Number.MAX_SAFE_INTEGER (and Number.MAX_SAFE_INTEGER) that point to those...
...not necessary to add a version constraint next to your gems in the Gemfile. Since all versions are saved in the Gemfile.lock, everyone running bundle install will get exactly the...
...You are not checking in the Gemfile.lock into the version control (not recommended) A specific gem has a bug in a more recent version (adding a comment for the reason...
...discouraged to load your JavaScript by a tag in the : The reason is that a tag will pause the DOM parser until the script has loaded and executed. This will delay the browser's first contentful paint. A much better default is to load your scripts with a tag: A deferred script has many useful properties: It does not block the browser from rendering content. Deferred scripts...
...can query the entire DOM tree with querySelector() and friends. document.readyState is 'interactive'. Deferred scripts will run before the DOMContentLoaded event. That means if your code waits to initialize until...
...posts; if you use pagination the queries will be more complicated, but the point still stands. Looks harmless enough? It is not. The problem ActiveRecord will rewrite this into a...
...query using LEFT JOINs which looks something like this: SELECT "blog_posts".*, "comments".*, "attachments".* FROM "blog_posts" LEFT OUTER JOIN "comments" ON "comments"."blog_post_id" = "blog_posts"."id"
By default, Devise sends all emails synchronously with deliver_now. To change that, Devise's readme suggests overwriting the send_devise_notification method like this: class User def send_devise...
...notification(notification, *args) devise_mailer.send(notification, self, *args).deliver_later end end However, there is one problem: When deliver_later enqueues the mail with ActiveJob, the job arguments are logged. In...
If you already selected an element and want to get its parent, you can call find(:xpath, '..') on it. To get the grand-parent element, call find(:xpath, '../..'). Example
...href]).to eq("http://twitter.com/") About XPath There is a good overview on XPath syntax on w3schools. But as XPath expressions can get quite complex and hard to understand for...
...agent manually. Example: # exec resource: exec { "update_rubygems_${user}_${version}": command => "${home}/.rbenv/shims/gem update --system ${version}", unless => "${home}/.rbenv/shims/gem -v | /bin/grep ${version}", } This does execute rbenv commands. If puppet runs...
The change_column method for rails migrations support casting with a custom SQL statement. This allows us to change a column type and keep the former content as the new...
...number, 'int USING CAST(rating AS int)' end end Warning This migration's rollback strategy will fail, if any existing entry can't be casted. This is likely to happen...
There is a practical short list for valid/invalid example email addresses - Thanks to Florian L.! The definition for valid emails (RFC 5322) can be unhandy for some reasons, though.
...provides a built-in email regex URI::MailTo::EMAIL_REGEXP. That's the best solution to work with. /\A[a-zA-Z0-9.!\#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0...
When your public-facing application has a longer downtime for server maintenance or long migrations, it's nice to setup a maintenance page to inform your users. When delivering the...
...maintenance page, be very careful to send the correct HTTP status code. Sending the wrong status code might get you kicked out of Google, or undo years of SEO work...
PostgreSQL uses the C library's locale facilities for sorting strings: First, all the letters are compared, ignoring spaces and punctuation. It sorts upper and lower case letters together. So...
...the order will be something like a A b B c C Then, spaces and punctuation are compared to break ties. Example: Ruby PostgreSQL IMAGE3.jpg image2.jpg image.jpg image3.jpg image2.jpg
If you migrate a Rails application from Sprockets to Webpack(er), you can either transpile your CoffeeScript files to JavaScript or integrate a CoffeeScript compiler to your new process. This...
...to the global namespace, define them on window directly: -class @User +class window.User Replace Sprocket's require statement with Webpacker's import statement to load dependencies. -#= require ./person +import './person...
...center an icon (or any "blockish" inline element, really) with the capital letters of surrounding text. This works well with our modern approach to SVG icons, or e.g. for custom...
...list icons. Note that it won't work with "text" icons that scale with font-size. Technical background All text has a "baseline". It is the bottom edge of most...
To work with other type of nodes (like text, comment or CDATA sections) you need to: Retrieve child nodes contents() (which behaves like children() except that it returns...
...element and returns an array of all child nodes that are text nodes: function selectTextNodes($container) { return $container.contents().filter(function() { return this.nodeType === 3; }); } Also check out this list of existing...
...instead of using the UI or creating records in the Rails console. This approach saves time and gives you useful defaults and associations right out of the box.
...yml files. You may override those application-wide error messages using model or attribute scope like this: en: activerecord: errors: messages: invalid: is invalid # used for any invalid attribute in...
Download buttons can be difficult to test, especially with Selenium. Depending on browser, user settings and response headers, one of three things can happen: The browser shows a "Save as...
...dialog. Since it is a modal dialog, we can no longer communicate with the browser through Selenium. The browser automatically downloads the file without prompting the user. For the test...
...much faster than the configured up.form.config.observeDelay. Therefore, it may happen that you already entered something into the next field before unpoly updates that field with a server response, discarding your...
The steps I wait for active ajax requests to complete (if configured) and capybara-lockstep can catch some of these cases, but not all. Both of these only wait...
When using tmux, selecting and copying multiple lines of text can be a hassle, especially when using splits (highlighting lines will cross pane borders, copying contents from the other pane...
...too) and when the user wishes to copy (thus, select) lines that have already scrolled out of the viewport in the current pane. One idea would be to enable mouse...
...ZIP archive, you basically have two options: Write a ZIP file to disk and send it as a download to the user. Generate a ZIP archive on the fly while...
...streaming it in chunks to the user. This card is about option 2, and it is actually fairly easy to set up. We are using this to generate ZIP archives...
By activating strict_loading you force developers to address n+1 queries by preloading all associations used in the index view. Using an association that is not preloaded will raise...
...an ActiveRecord::StrictLoadingViolationError. I think it's a good default to activate strict_loading in your controllers' #index actions. This way, when a change introduces an n+1 query, your...