Skip to content

Instantly share code, notes, and snippets.

Building railsdevs in public.

Joe Masilotti joemasilotti

Building railsdevs in public.
View GitHub Profile
View result_type.rb
module ResultType
extend ActiveSupport::Concern
module ClassMethods
def define_type(type)
define_method "#{type}?" do
joemasilotti / cold_message.rb
Created Apr 27, 2022
Service layer extraction
View cold_message.rb
# AFTER the refactor
class ColdMessage
class BusinessBlank < StandardError; end
class MissingSubscription < StandardError; end
class ExistingConversation < StandardError
attr_reader :conversation
joemasilotti / sidekiq_policy.rb
Created Dec 22, 2021
ActiveSupport::EnvironmentInquirer example
View sidekiq_policy.rb
class SidekiqPolicy
attr_reader :user, :environment
def initialize(user, environment: Rails.env)
@user = user
@environment = environment
def visible?
user.admin? || environment.development?
joemasilotti / HTTP.swift
Last active Mar 25, 2021
Combine-powered HTTP client with success and failure JSON decoding
View HTTP.swift
enum HTTP {
struct Response<T> {
let value: T
let headers: [AnyHashable: Any]
enum HTTPError<T: LocalizedError>: LocalizedError {
case failedRequest
case invalidResponse
case invalidRequest(T)
joemasilotti /
Created Mar 4, 2021
Turbolinks + Turbo Native - Generic form handling with Stimulus

Turbolinks + Turbo Native - Generic form handling with Stimulus

These three files show how to handle forms with (legacy) Turbolinks in your Rails app with the new Turbo Native adapters.

  1. The Rails controller renders the form partial with an :unprocessable_entity status when encountering a form validation error
  2. The form is submitted via AJAX (local: false or remote: true depending on your Rails version)
  3. This is caught via the ajax:error->form#onError Stimulus action
  4. The Stimulus controller replaces the form's contents with the server-sided rendered HTML
  5. The native app is informed when the redirect occurs on a succesful submission
joemasilotti / ScriptMessageEnum.swift
Created Feb 10, 2021
Two approached to a Swift enum with associated type
View ScriptMessageEnum.swift
// Enum with manual parsing.
enum ScriptMessage {
case registerForRemoteNotifications
case showBackButton(url: URL)
case signOutCompleted
init?(body: [String: Any]) {
switch body["name"] as? String {
case "registerForRemoteNotifications":
self = .registerForRemoteNotifications
joemasilotti / TestVisitDelegate.swift
Created Dec 28, 2020
Using #function to record messages sent to Swift test doubles
View TestVisitDelegate.swift
class TestVisitDelegate: VisitDelegate {
func visitDidStart() {
func visitDidComplete() {
// MARK: Testing verification helpers.
joemasilotti / .UI tests without UI Testing
Last active Dec 6, 2020
UI tests without UI Testing experiment
View .UI tests without UI Testing

UI tests without UI Testing experiment

This gist is a small experiment to see if there's an "in-between" for testing iOS applications. More feature-level than XCTest but not as heavy handed (or slow and brittle) as UI Testing.

The general idea is to provide an API similar to UI Testing but be able to spin up any controller on the fly. By putting the controller inside of a window the test behaves a bit more like the real app.

Currently, only two methods are explored: finding labels and buttons. An obvious omission is searching for the view recursively. A perhaps less obvious omission is still being able to tap disabled buttons.

This extends on my thoughts in a recent blog, Testing the UI without UI Testing in Swift.

joemasilotti / ContentView.swift
Last active Aug 4, 2020
SwiftUI List/ForEach with bindings
View ContentView.swift
import SwiftUI
struct ContentView: View {
@ObservedObject private var signalStore = SignalStore()
var body: some View {
VStack {
List(signalStore.all) { signal in
SignalView(signal: self.signalStore.binded(signal: signal))
joemasilotti / Multiple sheets in
Last active Oct 13, 2021
Multiple sheets in SwiftUI
View Multiple sheets in