I improved the compiler()
function:
Somewhat regularly, you will need to filter a list down to some items and then map them to another value.
You can of course chain map
and compact
, or select
/filter
and map
, but Ruby 2.7 introduced a method for this exact purpose:
filter_map
Archive
.
So instead of
>> [1, 2, 3, 4, 5, 6].map { |i| i * 2 if i.even? }.compact
=> [4, 8, 12]
or
>> [1, 2, 3, 4, 5, 6].select(&:even?).map { |i| i * 2 }
=> [4, 8, 12]
you can just do
>> [1,...
I recently wanted to add a model for address information but also wanted to add a unique index 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 :shop
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 string argument as the v...
Sass lets you easily specify multiple selectors at once like this:
.some-block
&.has-hover,
&:hover
outline: 1px solid red
This will add a red outline on either real hover or when the has-hover
class is present. However, adding a comment will void the definition of that line:
.some-block
&.has-hover, // From hoverable.js <-- DON'T
&:hover
outline: 1px solid red
... will simply drop the &.has-hover
part in
ruby-sass
Archive
(deprecated). [sassc](https://rubygems.org/g...
Spreewald
Archive
comes with a selector_for
helper that matches an English term like the user's profile
into a CSS selector. This is useful for steps that refer to a particular section of the page, like the following:
Then I should see "Bruce" within the user's profile
^^^^^^^^^^^^^^^^^^
If you're too lazy to manually translate English to a CSS selector by adding a line to features/env/selectors.rb
, we already have an [auto-mapper to translate English into ...
Sometimes huge refactorings or refactoring of core concepts of your application are necessary for being able to meet new requirements or to keep your application maintainable on the long run. Here are some thoughts about how to approach such challenges.
Try to break your refactoring down in different parts. Try to make tests green for each part of your refactoring as soon as possible and only move to the next big part if your tests are fixed. It's not a good idea to work for weeks or months and wait for ALL puzzle pieces to...
Rack::SteadyETag
Archive
is a Rack middleware that generates the same default
ETag
Archive
for responses that only differ in CSRF tokens or CSP nonces.
byebug
being in the user bundle.Cache-Control
header that causes permanent redirects to not be cached.Imagine these models and associations:
class Deck < ApplicationRecord
has_many :cards
end
class Card < ApplicationRecord
belongs_to :deck, optional: true
end
Now you want to find all Decks without any Card or all Cards without a Deck.
Rails 6.1 introduced a handy method ActiveRecord#missing Archive to find records without given associations.
Deck.where.missing(:cards)
SELECT "decks".*
FROM "dec...
When changing code in mailers, updating the corresponding mailer preview is easily forgotten.
Mailer previews can be tested like other code as well and I sometimes add the following test to test suites:
# Make sure to require the previews
Dir[Rails.root.join('test/mailers/previews/*.rb')].sort.each { |file| require(file) }
ActionMailer::Preview.all.index_with(&:emails).each do |preview, mails|
mails.each do |mail|
it "#{preview}##{mail} works" do
expect { preview.call(mail) }.not_to raise_error
...
This is a final maintenance release before Unpoly's next major feature drop in a few weeks.
This is also the last release with support for Internet Explorer 11. Future releases will support Chrome, Firefox, Edge and the last two majors of Safari.
The polling Archive implementation was rewritten to fix many issues and edge cases:
[up-poll]
Archive
reloading very fast when the server responds with an
X-Up-Target: :none
Archive
header ...When redirecting you should take care to use the right HTTP status code.
When redirecting from a controller Archive , the default status code is 302 Moved Temporarily:
redirect_to pos...
If you have a JS fiddle, you can open it in fullscreen by appending /show
to the URL.
Example: https://jsfiddle.net/b275g910/3
=> https://jsfiddle.net/b275g910/3/show
Browsers support different types of redirects.
Be very careful with these status codes:
301 Moved Permanently
308 Permanent Redirect
Most browsers seem to cache these redirects forever, unless you set different Cache-Control
headers. If you don't have any cache control headers, you can never change them without forcing users to empty their cache.
Note
When starting a project we always make a good estimate of all known requirements, and plan budgets and available developers accordingly.
Requirements change. Budgets usually don't.
To make sure a project stays on track, we update our estimates once a month and compare them to the remaining budget. If this doesn't match any more, we have to act.
To update an estimate, do the following:
Adding a gem means you take over the liability towards the external code.
Based on " To gem, or not to gem Archive ":
Recent IRB versions include a multi-line autocomplete which may be helpful to novice users but can be distracting.
Cycling through options works by pressing the Tab key (as usual), and for some methods you also get some kind of documentation, though the quality of results is usually not on par with your IDE of choice.
I have found that it also slows down my IRB in some cases, or that pressing the Backspace key does not always reliably remove characters, which I find more annoying than useful.
You may disable multi-line autocomplete by