Skip to content

Instantly share code, notes, and snippets.

View aseroff's full-sized avatar

Andrew Seroff aseroff

View GitHub Profile
@aseroff
aseroff / toggleable_controller.js
Created September 12, 2023 20:50
toggle stimulus controller
import { Controller } from "@hotwired/stimulus"
export default class ToggleableController extends Controller {
static targets = [ 'toggle', 'enableSection', 'disableSection', 'hideSection' ]
toggle() {
let checked = this.toggleTarget.checked
if (this.hasEnableSectionTarget) {
this.enableSectionTarget.disabled = !checked
@aseroff
aseroff / characters.html.haml
Created June 22, 2023 17:26
text with character limit html & stimulus controller
%div{data: {controller: 'text-with-limit'}}
- limit = 1000
%label Message:
= form.text_area :custom_message, value: default_message, maxlength: limit, rows: 9, data: { 'text-with-limit-target': 'field', action: 'keyup->text-with-limit#updateCounter' }
.right
%small
Chars left:
%span{data: {'text-with-limit-target': 'counter'}}= limit - default_message.length
\/
= limit
@aseroff
aseroff / check_column_controller.js
Last active July 5, 2023 17:43
sample check all checkboxes stimulus controller with operator
import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = [ 'header', 'row', 'operator' ]
headerToggle() {
let switch_status = this.headerTarget.checked
this.rowTargets.forEach(box => {
if (!box.disabled) { box.checked = switch_status; }
})
@aseroff
aseroff / docs.rake
Created June 22, 2023 17:05
rake tasks for versioning
# frozen_string_literal: true
if Rails.env.development?
require 'rails-erd'
require 'yard'
namespace :docs do
desc 'updates documentation'
task all: :environment do
@aseroff
aseroff / hoops
Last active January 30, 2024 20:40
The stupid hoops required to install mysql2 gem with brew-installed mysql
mysql_version=$(brew list --versions mysql | tr ' ' '\n' | tail -1)
mysql_path=$(brew --cellar mysql)/$mysql_version
gem install mysql2 -- \
--with-mysql-lib=$mysql_path/lib \
--with-mysql-dir=$mysql_path \
--with-mysql-config=$mysql_path/bin/mysql_config \
--with-mysql-include=$mysql_path/include \
--with-openssl-dir=/usr/local/Cellar/openssl@3/3.1.2
@aseroff
aseroff / string.rb
Last active March 30, 2023 13:42
Convert roman numerals into arabic numerals in place
class String
def to_arabic
result = 0
str = self
roman_mapping.each_value do |roman|
while str.start_with?(roman)
result += roman_mapping.invert[roman]
str = str.slice(roman.length, str.length)
end
@aseroff
aseroff / opt5.md
Last active March 28, 2023 20:52
Creating a selectbox for a form using ancestry SECRET OPTION #5

Options 1-4 seen here

Option 5: Directly writing to the Ancestry string field

  • In the form:
    <%= f.select :ancestry, [['- New Primary Category -', '/']] + Category.all.map{ |category| [category.name, "#{category.ancestry}#{category.id}/"] } %>
@aseroff
aseroff / exercise_turbo_frame_request_conditional.rb
Last active March 22, 2023 19:43
Testing rails controllers that change behavior based on "if turbo_frame_request?"
# Based on this code: https://github.com/hotwired/turbo-rails/blob/main/app/controllers/turbo/frames/frame_request.rb
# the response of turbo_frame_request? is based on the presence of a header named "Turbo-Frame".
# Therefore, you can exercise code behind this conditional in your controller tests by specifying that header in your GET requests.
# example controller action
class DashboardController < ApplicationController
# GET dashboard
def index
if turbo_frame_request?
# frame loading behavior
@aseroff
aseroff / customizing_spina.md
Last active October 18, 2023 19:58
Customizing a SpinaCMS-powered site/blog

For a while, my org was using Wordpress to manage a public-facing website, which was reverse-proxied on my Rails application's server to share its DNS (a fairly common setup). However, the cost of supporting Wordpress and the proxy became too great to justify, and we looked into replacing Wordpress with another CMS option. I came across SpinaCMS, an open-source, Rails-powered CMS, which offered a lot of upside to proxying to a seperate server running a separate application. However, there were several gotchas that took some navigating, that I want to share in one place, considering I believe these would all be common situations to anyone trying to accomplish a similar goal. This guide will assume you have completed the basic setup of gem installation and setup rake tasks.

Using MySQL instead of Postgres

If your application is already running on MySQL, you probably don't want to go through the hurdles of moving to a Postgres database. Fortunately, unless you are using the e-commer

@aseroff
aseroff / yard_customization.md
Last active April 27, 2023 18:48
YARD customization for Rails

So you're running a Rails application and want to spruce up your YARD documentation. Here's the guide I wish I had.

Step 0: Plugin

Add yard-activerecord and yard-activesupport-concern to your Gemfile, add --plugin activerecord and --plugin activesupport-concern to your .yardopts flags, and db/schema.rb to the end of your .yardopts sources. Your models' attributes and assocations should now be included in the documentation.

Step 1: CSS/JS

To modify your YARD template, create doc-src/templates/default/fulldoc/html, then add --template-path doc-src/templates to your project's .yardopts. You can now create a doc-src/templates/default/fulldoc/html/css/common.css and the styles will be included (but overwrites need to be !important).