Skip to content

Instantly share code, notes, and snippets.

View mediafinger's full-sized avatar
🏠
In Köln

Andreas Finger mediafinger

🏠
In Köln
View GitHub Profile
@mediafinger
mediafinger / single_page_rails_app.ru
Created April 25, 2024 17:15
Single Page Rails App example / template
# filename: single_page_rails_app.ru
# RUN via: `rackup single_page_rails_app.ru`
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rack", "2.2.9"
gem "rails", "~> 7.1"
@mediafinger
mediafinger / add_roles_to_users.rb
Last active November 18, 2022 13:43
Example code to allow instances of the `User` model to have multiple `roles`
# Example code to allow instances of the `User` model to have multiple `roles`:
# Migration -------------------
class AddRolesToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :roles, :text, array: true, default: [], null: false
end
end
@mediafinger
mediafinger / geojson.markdown
Created November 12, 2022 15:20
GitHub flavored Markdown with geojson data renders maps

GeoJson Markdown

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "id": 1,
 "properties": {

Draft

Ich schreibe mal runter, wie ich mir den Prozess vorstelle, ohne mir die SLOC API Docs anzuschauen:

  • wir starten den SyncSlocSensorsJob
  • der nimmt sich alle Sensoren die in den letzten X Stunden nicht gesynct wurden last_synced_at
  • bei den am längsten nicht gesyncten anfangend, ruft er die SLOC API in Batches of 10 ab
  • bei erfolgreicher Response
    • wird last_synced_at in den 10 Sensoren updated nachdem
  • die Werte in der DB gespeichert wurden
@mediafinger
mediafinger / index.markdown
Created November 7, 2022 21:47
@mediafinger@ruby.social / Mastodon verification
@mediafinger
mediafinger / Packwerk.markdown
Last active October 25, 2022 23:30
Modularizing a Rails Monolith with Packwerk - collection of ideas and resources

Packwerk

Is a Ruby gem to help separate concerns inside a Rails monolith.
https://github.com/Shopify/packwerk

Treat data modeling for packages like data modeling for network-isolated services:
hold foreign IDs referencing models across package boundaries, but do not hold strictly enforced foreign database keys and do not use ORM associations that would hide the separation of concerns.

Following the upper advise would be quite a change for our models. But less dependent: :destroy | :nullify | nil decisions could actually make our lifes easier and our data more consistent (or just more fault tolerant). And not to forget that Packwerk allows to start small and increase its checks file by file.

@mediafinger
mediafinger / refactoring_example.markdown
Last active October 25, 2022 07:23
Refactoring example of a Rails ActionController action with strong parameters and a helper method

Refactoring Example

I’ve found some confusing code in a project and would like to explain how and why I refactored it. As this is a small, very focused refactoring one can do in a "drive-by" manner while implementing another small change in this method, I think it serves well as an example.

Before

The first thing that confused me was that our frontend is not sending the parameters we explicitly permit in the route_event_params method (see below), instead it is sending *_uuid parameters...

And yes, this project unfortunately has id as primary_key and later added uuid which is the one known to the frontends.

@mediafinger
mediafinger / fancy_uuids.markdown
Created October 15, 2022 12:55
Fancy UUIDs - sortable UUID v4 compatible UUIDs with encoded timestamp and model name

Fancy UUIDs

UUID v4 UUIDs have the big drawback of being non-sortable. Fancy UUIDs encode the timestamp (just one digit short of microseconds) on the first bits and are therefore sortable.

They also encode a shortname of the generating model class, to make them globally identifiable.

Compatibility

Fancy UUIDs are just UUID v4 UUIDs - PostgreSQL and any other tool will be able to treat them like any other UUID v4 UUIDs.

@mediafinger
mediafinger / execute_sql_service.md
Last active October 5, 2022 10:02
With the update to Rails 7.0 `update_colum` is no longer allowed, when a model is in the `readonly?` state. While refactoring this to use SQL queries, I ran into the issue that my code works in the rails console, but not in my rspec tests. I suspect checking out a `connection` from `ActiveRecord::Base.connection_pool` is causing this.

Ok, I've refactored the service to not use the connection_pool, but to use a new connection instead.

And this also satisfies the rspec tests. I still don't understand what the issue was, but this is a working solution,
and as far as I see it also prevents us from blocking more and more connections, but instead closing them reliably.

This is the new code:

class ExecuteSqlService
 class &lt;&lt; self
@mediafinger
mediafinger / updating_ruby_on_rails_apps.markdown
Created September 23, 2022 16:43
Updating Ruby on Rails apps

How to Update a Ruby on Rails app

To safely update the Ruby and Rails version, your app should have a proper test suite you can rely on. But even with a great test suite, I always recommend running a rake task, starting the rails console and starting the rails server and using your app in the browser, before opening a PR and asking for reviews.

Ruby updates

Do it incrementally, don't skip any minor versions.

Which means, if your app is still on Ruby 2.7.x, then first update to the latest 2.7.X version, before updating to the latest 3.0.Y version and only when this has been done successfully, upgrade to the latest 3.1.Z version.