Rails 5.2+ supports "verbose query logs" where it shows the source of a query in the application log. Normally, it looks like this: User Load (0.5ms) SELECT "users".* FROM...
...app/controllers/users_controller.rb:42:in `load_users' However, you may encounter ActiveRecord's LogSubscriber as the source for all/most queries which is not helpful at all: User Load (0.5ms) SELECT "users...
...links. Popular pagination libraries like will_paginate or Kaminari do this for us by simply issuing an extra query, like this: SELECT post.* FROM posts LIMIT 20 OFFSET 100;
...of the time. But rarely, you might have very complicated WHERE conditions or a subquery that takes time to run. In these cases, doing two queries is slower than it...
...Ever wanted to give an element "the container's width minus 20px on each side"? Here you go: .foo { width: calc(100% - (20px * 2)); } When using Sass, you need to...
...interpolate Sass expressions: $margin: 20px * 2 .foo width: calc(100% - #{$margin}) Supported by all modern browsers and IE9...
...all the world to admire. For private gems this is very bad. To make sure this cannot happen, rubygems 2.2+ allows you to restrict eligible push hosts: Gem::Specification.new 'my...
...gem', '1.0' do |s| # ... s.metadata['allowed_push_host'] = 'https://gems.my-company.example' end In case you already messed up, follow these instructions to get your gem removed. Of course, it's not...
...Linux and MacOS, they look horrible on Windows, a problem that gets worse with smaller font sizes. The culprit is something called font hinting: ... hinting is about... programming instructions that...
...hinting, causing horrible rendering for most fonts. Embedding autohinting information into font files A solution is to autohint fonts and replace the existing (bad) hinting with the autohinting information.
You may want this for things where Rationals are being used, like when subtracting Date objects from one another. What's happening? Converting a Rational to a String usually...
...does something like this: 1.8.7 > Rational(2, 3).to_s => "2/3" 1.9.3 > Rational(2, 3).to_s => "2/3" 2.0.0 > Rational(2, 3).to_s => "2/3" However, when you have a...
...message like this, rubygems.org might have issues with their ipv6 connectivity: $ bundle install Fetching source index from https://rubygems.org/ Retrying fetcher due to error (2/4): Bundler::HTTPError Could not fetch...
...specs from https://rubygems.org/ due to underlying error <timed out (https://rubygems.org/specs.4.8.gz)> The (a little bit dirty) possible solution If that's actually the case, then you can try...
When using Sidekiq in your application, you must write thread-safe code. This wiki page also lists gems that are known to be unsafe on threaded applications.
...gem that will also be used by a Sidekiq worker, make sure to confirm it's thread-safe...
...Content-Type on GET request (which have a blank body), an external API may still force you to send one. Angular's $http service will strip that header when the...
...is blank. [1] This is possibly a misconception of RFC2616. Here is how to send GET requests with a Content-Type header in Angular. Example Consider this request: $http({ method...
A fantastic guide for a dilemma facing any web-based product. Here’s a simple set of Yes/No questions that you can quickly answer before you add another item to...
...your product roadmap. Saying yes to a feature request – whether it’s a to an existing customer, a product enquiry, a teammate, or a manager – is immediately rewarding. It’s...
Soon after having written our shell-for script, we wanted to easily get dumps of our productions machines, too. This is how we do it: dump-for staging [-s]
...dump to your project's tmp directory and name it according to the capistrano stage you're calling for, here: staging.dump. When you pass the optional -s option, the dump...
What are the advantages of a gem like memoized over the @variable ||= syntax? Why can it be dangerous to memoize class methods? Why is it often fine to...
...memoize instance methods? Resources Speeding up Rails with Memoization 4 Simple Memoization Patterns in Ruby (And One Gem) Don't use the || operator to set defaults Exercise
What are Google’s plans for turning WebM into a genuinely open standard, one that is based on consensus like the rest of W3C’s HTML5 effort? Would Google fully...
...support such an effort? Even the WebM project’s domain is controlled by Google. Google chose to release WebM under the Creative Commons license which would theoretically allow a standards...
...Railses. Consul now uses Memoizer for this. Temporarily change the current power When you set Power.current to a power in an RSpec example, you must remember to nilify it afterwards...
...Otherwise other examples will see your global changes. A better way is to use the .with_power method to change the current power for the duration of a block:
jQuery doesn't store information about event listeners and data values with the element itself. This information is instead stored in a global, internal jQuery cache object. Every time you...
...gets deleted is when you call remove() on the element that put it there! Since cache entries also have a pointer back to the element that spawned them, it is...
We used zeroclipboard.js in some of our projects but now we switched to clipboard.js because it does not rely on flash. Flash support of the major browsers has ended.
...more advantages of clipboard.js: it consists only of a single javascript file, so it does not trigger additional requests with rails it automagically provides user feedback by selecting the text...
...Use Before and After to avoid that. Details Consider this Cucumber feature file: Feature: Something that needs to be tested Background: Given a user And I sign in Scenario: Sign...
When I sign out Then I should see "Signed out" Scenario: Something else # ... Now, assume you have these step definitions: Around do puts "** Around: before yield" yield puts "** Around...
...to those fields that is case-insensitive. The model looked like this: create_table :shop_locations do |t| t.string :street t.string :house_number t.string :zip_code t.string :city t.belongs_to...
end But how to solve the uniqueness problem? Another day, another undocumented Rails feature! This time, it’s that ActiveRecord::Base.connection.add_index supports an undocumented option to pass a...
...extremely popular amongst front-end developers the last couple of years. This isn’t surprising, as it has made it a lot easier for us to create dynamic layouts and...
UI sortable helps reordering items with drag 'n drop. It works quite fine. Proven configuration for sorting table rows When invoking the plugin, you may pass several options. This set...
...causing the thead border-bottom to grow // when the first table row is dragged tr.ui-sortable-helper:first-child + tr > td border-top: none
You can use JavaScript to get or set cookie values on the client. Using the vanilla JavaScript API In JavaScript, document.cookie is an accessor to all cookies on the current...
...site. It looks like a String, but its setter is actually more powerful. When setting cookies this way, remember to set the path=/ option. Reading cookies A result may look...
...an array, neither JavaScript/ES6+ nor libraries like LoDash offet that natively. Here is a simple function instead that modifies the input array in place. function moveArrayElement(array, element, offset) {
...It was what I needed, but you could easily adjust that. Check the linked StackOverflow post for more information and other implementations...
...Basic.encode_credentials for that, and put its result into the Authorization request header. Request specs For request specs, use the :header option. it 'requires authentication' do get '/' expect(response.status).to...
...username, password) get '/', header: { Authorization: encoded_credentials } expect(response.status).to eq(200) end Controller specs In controller specs, you can put then into request.env['Authorization']. it 'requires authentication' do
Our old solution for cronjobs, the "craken" plugin, is no longer maintained and does not work on Rails 3.2+. We will instead use the whenever gem. "Whenever" works just like...
...craken", by putting your rake tasks into the server's cron table. Everything seems to work just like we need it. Installation for new projects Add "whenever" to your Gemfile...