Download buttons can be difficult to test, especially with Selenium. Depending on browser, user settings and response headers, one of three things can happen:
When delivering non-public uploaded files (images, documents etc), one has to decide whether and how to do authorization. The usual approaches are:
send_file
with a regular controller. This is secure, but potentially slow, especially for large collections of images.When going with the "unguessable URL" approach, it is possible to somewhat increase security by using expiring URLs. The idea is to encode the expi...
Along with a number of other cool new features and performance improvements, Ruby 2.6 added function composition to the Proc and Method classes. Today we’ll take a look at how this allows us to use some functional programming goodness in our Ruby code.
Elastic antwortet per Default auf Port 9200
http GET :9200
{
"name": "ntK2ZrY",
"cluster_name": "elasticsearch",
"cluster_uuid": "Bbc-ix5bQZij5vfFU29-Cw",
"version": {
"number": "6.7.1",
"build_flavor": "...
Tod is a gem for working with daytimes.
Thus SQL has a time
datatype for storing time of day in the format hh:mm:ss
, neither Ruby nor Rails themselves offer an elegant way to deal with day times.
Time
and DateTime
both handle daytime values AND calendar date, using them to only store the time of day will end in inconsistent and thus confusing data, e. g. Time.new
will initialize with the current Time in your Timezone, DateTime.new
initializes at January 1, at an undefined year, without a timezone o...
When doing some meta-programming magic and you want to do something for all attributes of a class, you may need to access connection
or some of its methods (e.g. columns
) during class definition.
While everything will be fine while you are working on a project that is in active development, the application will fail to boot when the database is missing or has no tables. This means that Raketasks like db:create
or db:migrate
fail on a freshly cloned project.
The reason is your environment.rb
which is loaded for Raketasks and calls...
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 preview your mail in the browser, you can use the Action Mailer Preview. To inspect the mail directly in your email client, just create an .eml
file and open it with your client:
mail = MyMailer.newsletter
Fil...
Rails' ActiveSupport::TimeWithZone
objects have both a timezone code and offset, e.g. Thu, 28 Mar 2019 16:00:00 CET +01:00
. Ruby's stdlib TZInfo
also has time zones, but with different identifiers.
Unfortunately, not all timezone codes can be used to parse strings or to move time objects into another time zone.
Some timezone codes like CET
are supported by ActiveSupport extensions like String#in_time_zone
, while many codes will actually not work:
>> '2019-03-01 12:00'.in_time_zone('PST')
ArgumentError (Invalid Timezone: PST)
...
Fixed bugs where calling valid?
would change the record's state
.
When you have a powerful machine with many CPU cores, you might run into an error like
ERR DB index is out of range (Redis::CommandError)
This is because Redis defaults to at most 16 databases (0 to 15) and running tests in parallel might exceed that (your tests might run on databases 1..n
or 2..(n+1)
).
You can increase that limit:
Get number of CPUs of your machine.
nproc --all
Open up Redis configuration file.
sudo vim /etc/redis/redis.conf
Find databases
row and increase it, e.g. set to CPU cou...
Rails' params
hash contains any request parameters (URL parameters or request payload) as well as routing parameters like :controller
, :action
, or :id
.
To access only URL parameters, use request.query_parameters
. Routing params are available through request.path_parameters
.
# On /users?query=Bob&page=2
>> request.params
=> {"page"=>"2", "query"=>"Bob", "controller"=>"users", "action"=>"index"}
>> request.query_parameters
=> {"page"=>"2", "query"=>"Bob"}
>> request.path_parameters
=> {:controller=>"users", :action=>"i...
process
definitions are only applied to the original filebefore
/after
) are applied to original file and each version by itself#version_name
: version uploaders return the version name, whereas the original uploader instance returns nil
For searching in large database tables we usually use PostgreSQL's fulltext search capabilities.
While this works reasonably well for content primarily consisting of prose, it is not necessarily a good solution for all use cases. The main issue is that it is only possible to search for prefixes of text tokens, which can potentially be unexpected for users.
One example are dates:
If you index the text 2019-01-23 15:16
, PostgreSQL will create the following tokens: 2019, -01, -23, 15 16
. A user searching for 01-23
wi...
From: Sven
To: unpoly@googlegroups.com
Subject: performance on smartphones and tablets
Hello
I just used your framework in one project and must say,
I am really pleased with it -- but only on a desktop computer.
Have you benchm...
To remove all tables from a database (but keep the database itself), you have two options.
You will need to re-create the schema and its permissions. This is usually good enough for development machines only.
DROP SCHEMA public CASCADE;
CREATE SCHEMA public;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO public;
Applications usually use the "public" schema. You may encounter other schema names when working with a (legacy) application's database.
Note that f...
When projects run for many years, they require special regular maintenance to stay fresh. This kind of maintenance is usually not necessary for small or quick projects, but required to keep long-running projects from getting stale.
You should be able to fit this into a regular development block.
As time goes by, libraries outdate. Check your software components and decide if any of it needs an update. Your main components (e.g. Ruby, Rails, Unpoly) should always be reasonably up to d...
We figured out, that ActiveJob Inline might lead to autoloading problems in development. The result was an exception when running an import script, which delivers async mails.
A copy of XXX has been removed from the module tree but is still active! (ArgumentError)
Our fix was to use .deliver_now
and not .deliver_later
(this is not a general fix, but it was okey for us). Below there are some debug hints which helped us to locate the problem:
There are two ways to lock a user in devise.
It depends on your requirements which methods works best.
We recommend to use option 2 when you want to couple the lock to the m...
When you are using lambdas in RSpec to assert certain changes of a call, you know this syntax:
expect { playlist.destroy }.to change { Playlist.count }.by(-1)
While you can define multiple assertions through multiple specs, you may not want to do so, e.g. for performance or for the sake of mental overhead.
RSpec allows chaining expectations simply by using and
.
expect { playlist.destroy }
.to change { Playlist.count }.by(-1)
.and change { Video.count }.by(0)
...
Font Awesome 5 is a comprehensive solution for vector icons on your website.
Originally, Font Awesome came as an icon font (plus stylesheets), but recently it can also be used as a pure JavaScript solution (which will render icons as inline <svg>
tags), or even as SVG sprites.
All solutions have their pros and cons:
Icon font:
Javascript + inline SVG:
Phusion Passenger changed the way how it gets restarted several times. Through the project's history, these all were valid:
touch tmp/restart.txt
sudo passenger-config restart-app /path/to/app
passenger-config restart-app /path/to/app
You should not need to know which one to use. Instead, the capistrano-passenger gem will choose the appropriate restart mechanism automatically based on your installed the passenger version.
Add to your Gemfile
:
gem 'capistr...
In this example we assume that not only the storage gem changes but also the file structure on disc.
Part A: Create a commit which includes a script that allows you to copy the existing file to the new file structure.
Part B: Create a commit which removes all paperclip logic and replace it with the same code you used in the first commit
Here are some implementation details you might want to reuse:
Masonry is a famous library to dynamically arrange a grid of items that have different aspect ratio, like horizontal and vertical images.
Colcade is an alternative masonry-layouting library, developed by the same developer, but with a more modern approach.
It is said to have better performance while being smaller and having no dependencies. It automagically detects jQuery and defines a jQuery initializer, if present.
However, it offers [a few less features](https:...
Geordi's cucumber
command has a --rerun
option that reruns failing tests the given number of times. Usage:
geordi cucumber path/to/features --rerun=2
geordi cucumber path/to/features -r2
Cucumber will save a file tmp/parallel_cucumber_failures.log
containing the filenames and line number of the failed scenarios after a full test run. Normally you can say cucumber -p rerun
(rerun is a profile defined by default in config/cucumber.yml
) to rerun all failed scenarios.
Here are a few al...