Sept. 26, 2021, noon

PinkLetter - Tricks of the Week

PinkLetter (odone.io)

Welcome to my PinkLetter. A short, weekly, technology-agnostic, and pink newsletter where we cultivate timeless skills about web development.

My Ramblings This Week

What cool tricks have you learned this week?

I got a couple for you.

Authorization based on controller name and action:

  def authorize!
    return if {
      "root#index" => ->(_) { true },
      "users/sessions#show" => ->(current_user) { current_user.present? },
      "users#index" => ->(current_user) { current_user.present? && current_user.admin? },
      # ...
    }
      .fetch("#{controller_path}##{action_name}")
      .call(current_user)

    raise NotAuthorized.new("#{current_user.inspect} -> #{controller_path}##{action_name}")
  end

Update the CSRF token when a response returns one (that could have changed). Notice that some frameworks (like Elm) use XMLHttpRequest behind the curtains, so this works for them too:

(function() {
  var origOpen = XMLHttpRequest.prototype.open;
  XMLHttpRequest.prototype.open = function() {
    this.addEventListener('load', function() {
      const token = this.getResponseHeader('X-CSRF-TOKEN');
      if (token) {
        document.head.querySelector('meta[name="csrf-token"]').setAttribute('content', token);
      }
    });
    origOpen.apply(this, arguments);
  };
})();

Display a five-star rating in pure css with style="--rating: 2.3;"

Five-star rating input.

Reply to this email and share some tricks with me πŸ™ If you got anything about controlling blood sugars even better cause I’ve been all over the place this week!

Elsewhere on the Web

Little Things I Like to Do with Git by Harry Roberts

I thought I would note down some useful little Git snippets that I use the most frequently.


Forget a server β€” bring your static website to life with AWS S3, Lambda, and API Gateway by Jull.io

An entire backend server for something as simple as a contact form seems quite overkill, and, well β€” it is.


Shaping Values with Types by Josh Clayton

While there are only 110,000 valid four- and five-digit employee IDs, our data model for an employee ID uses the underlying type of String, which can represent an infinite number of values. Our data model does not reflect reality. By reducing the number of possible values captured in a type, it’s less likely that an incorrect value sneaks in.

You just read issue #66 of PinkLetter (odone.io). You can also browse the full archives of this newsletter.

Powered by Buttondown, the easiest way to start and grow your newsletter.