Skip to content

Instantly share code, notes, and snippets.

View ogirginc's full-sized avatar

Oğulcan Girginç ogirginc

View GitHub Profile
import {Controller} from "@hotwired/stimulus"
// Warns if form fields have unsaved changes before leaving the page.
// Changes are stored in Session Storage to restore un-warnable events
// like using the back button
//
// To use:
// <form data-controller="unsaved-changes">
// <input type="text" name="name" data-unsaved-changes-target="field">
export default class extends Controller {
@henrik
henrik / 0_example.erb
Last active May 25, 2025 01:17
Rails ViewComponent convenience, e.g. `c.heading("Hi")`
Before:
<%= render Admin::HeadingComponent.new("Hi") %>
After:
<%= c.heading "Hi" %>

Namespaces 101

Introduction

Ruby has recently merged namespaces as an experimental feature that is disabled by default, as of this writing.

This is a non-trivial development driven by @matz himself, and mainly implemented by @tagomoris, who just became a Ruby committer (🎉).

The feature has been cooking for a long time, with a first ticket opened a couple of years ago (#19744) and a revised one opened just last week (#21311).

# frozen_string_literal: true
require "bundler/inline"
# Inline Gemfile for dependencies
gemfile(true) do
source "https://rubygems.org"
gem "benchmark-ips"
end
@peterc
peterc / CONVENTIONS.md
Last active June 9, 2025 14:51
CONVENTIONS.md file for AI Rails 8 development
  • You MUST NOT try and generate a Rails app from scratch on your own by generating each file. For a NEW app you MUST use rails new first to generate all of the boilerplate files necessary.
  • Create an app in the current directory with rails new .
  • Use Tailwind CSS for styling. Use --css tailwind as an option on the rails new call to do this automatically.
  • Use Ruby 3.2+ and Rails 8.0+ practices.
  • Use the default Minitest approach for testing, do not use RSpec.
  • Default to using SQLite in development. rails new will do this automatically but take care if you write any custom SQL that it is SQLite compatible.
  • An app can be built with a devcontainer such as rails new myapp --devcontainer but only do this if requested directly.
  • Rails apps have a lot of directories to consider, such as app, config, db, etc.
  • Adhere to MVC conventions: singular model names (e.g., Product) map to plural tables (products); controllers are plural.
  • Guard against incapable browsers accessing controllers with `allo
class BaseClient
class APINotFound < StandardError; end
attr_reader :auth_strategy
def initialize(auth_strategy: :headers, headers: {})
@auth_strategy = auth_strategy
@headers = headers
end
# In your Gemfile
gem "localhost"

# Then, depending on your desired server
gem "falcon"
gem "puma"
@spencerldixon
spencerldixon / bookmarks_controller.rb
Created May 23, 2024 16:34
Real time search with Hotwire
def search
@pagy, @bookmarks = if params[:search].present?
pagy(@bookmarks.search(params[:search]), params: ->(params) { params.merge!({search: params[:search]}) })
else
pagy(@bookmarks)
end
end