When an Rspec example fails, I usually investigate by running that example again using
rspec <file:line>. However, this does not work with shared examples, since Rspec doesn't know in which context the shared example should be run.
But there is a different way: You can run the shared example using the
--example option. It takes a string value and runs all scenarios containing that substring in their full description.
This allows you to run a single uniquely named example, all examples with
similar names, all the examples in a u…
This is a small example on how you can check if your Postgres index can be used by a specific query in you Rails application. For more complex execution plans it might still be a good idea to use the same path of proof.
puts User.order(:last_name, :created_at).to_sql # => SELECT "users".* FROM "users" ORDER BY "users"."last_name" ASC, "users"."created_at" ASC
add_index :users, [:last_name, :created_at]
Web applications can be used by multiple users at the same time. A typical application server like Passenger has multiple worker processes for a single app. In a distributed deployment setup like we use at makandra you will even have multiple application servers, each with their own worker pool.
This means that your code needs to deal with concurrent data access. The two main tools we use to cope with concurrency are database transactions and distributed locks. These two are not interchangeable. You ca…
From time to time we update the underlying
rubocop version in our
makandra-rubocop gem. This often results in breaking changes in the cops configuration.
The upgrade process is described in the Changelog. Normally you will only see the new cops that were added by the
rubocop upstream. They will result in new offenses in your project and need to be fixed.
For this release we want to encourage you to enable the Rails cops, too. This is how you co…
In Rspec 3 the DSL like
describe is exposed globally by default. Therefore it is not necessary to write
However, there is a config option to disable this beavior, which also disables the old
RSpec.configure do |config| config.disable_monkey_patching! end
In order to still use
Rspec, you can set
expose_dsl_globally = true after
RSpec.configure do |con…
All columns of a model's database table are automagically available through accessors on the Active Record object.
When you need to specialize this behavior, you may override the default accessors (using the same name as the attribute) and simply call the original implementation with a modified value. Example:
class Poet < ApplicationRecord def name=(value) super(value.strip) end end
Note that you can also avoid the original setter and directly read/write from/to the instance's attribute storage. However th…
When installing gems, a lot of time is spent building locally installed documentation that you probably never use.
We recommend you disable documentation generation for
gem install by default.
Note that Bundler won't install documentation, so this advice applies only when installing gems manually.
If you don't already have it, create a
~/.gemrc file. The gemrc is a Yaml file, so add the following line to add default switches to the
(If you do n…
When a Rails controller action should handle both HTML and JSON responses, do not use
request.xhr? to decide that. Use
I've too often seen code like this:
def show # ... if request.xhr? render json: @user.as_json else # renders default HTML view end end
This is just plain wrong. Web browsers often fetch JSON via XHR, but they (should) also send the correct
Accept HTTP header to tell the server the data they expect to receive.
If you say
request.xhr? as a means for "…
You login to a Linux server with a performance issue: what do you check in the first minute?
uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 sar -n TCP,ETCP 1 top
When MySQL refuses to use your index, you can do a number of things wrong. One of them might be conditions with improper data types.
For example, let's assume you have a
users table with an
varchar) which is indexed.
MySQL will use the index when your query is well-formed:
mysql> EXPLAIN SELECT * FROM users WHERE email = 'firstname.lastname@example.org'; +----+-------------+-------+-------+----------------------+----------------------+---------+-------+------+-------+ | id | select_type | table | type | poss...
For rastered image formats like JPG or PNG, each pixel is basically drawn on a fixed size canvas. To display such an image in a different size (say: 1.5 times larger than original), the renderer (your Browser / GPU / Monitor) needs to interpolate the color values of missing pixels. The image will appear slightly blurred.
This is different for vector graphics like the SVG (Scalable Vector Graphics) format. You can imagine SVG files as XML file contai…
So I had the issue that User input (coming from many different sources and users) often contains the same long word. Maybe that's a super german thing to have lots of long words I have to deal with. This long word needs to be fully visible on small screens but none of the automated word-break solutions offered by css (https://justmarkup.com/articles/2015-07-31-dealing-with-long-words-in-css/) is clever or supported enough to be a good solution. So I…
You can use the Youtube API to get data from youtube. Here are some resources and some things that are good to know.
You need to create an API key and each request will cost a certain amount of Request-Points (depending on the kind of query).
It may help to understand youtube's domain model before starting.
It's best to just try out your requests in the browser to see what you will get.
A request can look like this (note that you have to replace the playlis…
A nice trick that lets you define "partials" in HTML without any additional rendering technology on the server or client.
There is a kind of features in web applications that hinder automated integration tests. Examples include cookie consent banners or form captchas. Clearly, these should be disabled so you do not have to explicitly deal with them in each and every test (like, every test starting with accepting the cookies notice). On the other hand, they must be tested as well.
A good feature disabling solution should therefore meet these requirements:
The feature is generally disabled in tests. A test does not need to do anything manually.
It is *…