Skip to content

Instantly share code, notes, and snippets.

View spencerldixon's full-sized avatar
👾
soju.so

Spencer Dixon spencerldixon

👾
soju.so
View GitHub Profile
@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
@spencerldixon
spencerldixon / deploy.yml
Created May 23, 2024 13:29
Kamal Boilerplate
service: wordsweeper
image: spencerldixon/wordsweeper
servers:
web:
hosts:
- 78.47.88.46
labels:
traefik.http.routers.domain.rule: Host(`wordsweeper.fun`, `www.wordsweeper.fun`)
traefik.http.routers.domain.entrypoints: websecure
@spencerldixon
spencerldixon / select_async_base_controller.js
Created June 8, 2023 16:47
Parent / Child slim-select async Stimulus controllers
import { Controller } from "@hotwired/stimulus";
import SlimSelect from 'slim-select';
// A generic base controller for doing async API searching via slim select with baked in rate limiting
// You'll can extend this controller and define two methods, query(searchTerm) for your GraphQL query,
// and formatData(data) to format your data for presentation
export default class extends Controller {
static targets = ["selector"]
static values = {
@spencerldixon
spencerldixon / conditionally_merge_date_select_params.rb
Last active April 1, 2021 10:47
Overrides the year field of a date_select if it is not set
def update_params
params.require(:thing).permit(:date).tap do |param|
param["date(1i)"] = Date.current.year if param["date(1i)"] == "1"
# Can merge in other things here
end
end
@spencerldixon
spencerldixon / zip_streaming.rb
Created September 25, 2020 11:40
Azure Downloads and Zip Streaming
# Service object azure_api.rb for interfacing with azure blob storage. Handles getting a list of files as well as
# getting files by top_level_directory/prefix and multiple prefixes. Also caches requests to ensure a fast response.
require 'azure/storage/blob'
CACHE_LENGTH = 3.hours
PREFIX_DELIMITER = ','
class AzureApi
def self.get_files_for(directory, prefix=nil)
@spencerldixon
spencerldixon / scopes.rb
Last active September 25, 2020 12:01
Useful Scopes
# Recent things with optional period parameter
scope :recent, ->(period = 30.days) { where("created_at >= ?", period.ago.beginning_of_day) }
# With Active::Text :notes field
scope :with_notes, -> { left_joins(:rich_text_notes).where('action_text_rich_texts.id is NULL') }
# Between two optional dates (uses keyword arguments for optional params)
scope :between, ->(from: 30.days.ago, to: Date.current) { where(created_at: from.beginning_of_day..to.end_of_day) }
@spencerldixon
spencerldixon / groups_spec.rb
Created August 25, 2020 14:45
RSpec feature tests for simple CRUDing of a resource
require 'rails_helper'
RSpec.feature "Groups", type: :feature do
let(:user) { FactoryBot.create(:user) }
let(:admin) { FactoryBot.create(:user, :with_admin) }
let(:group) { FactoryBot.create(:group) }
let(:groups) { FactoryBot.create_list(:group, 2) }
let(:new_group) { FactoryBot.build(:group) }
scenario 'require authentication' do
@spencerldixon
spencerldixon / pdf.js
Created November 8, 2019 13:33
html2pdf with Puppeteer
'use strict';
const puppeteer = require('puppeteer');
const fs = require('fs'); //Filesystem
let content = fs.readFileSync(process.argv[2], "utf-8");
const createPdf = async() => {
let browser;
try {
@spencerldixon
spencerldixon / user_registration_spec.rb
Last active August 28, 2020 12:01
Capybara feature specs for a simple Devise installation
require 'rails_helper'
RSpec.feature 'User Registration', type: :feature do
let(:new_user) { FactoryBot.build(:user) }
let(:user) { FactoryBot.create(:user) }
scenario 'User can sign up' do
visit root_path
first(:link, 'Sign up').click
@spencerldixon
spencerldixon / rails_helper.rb
Created July 19, 2019 10:47
RSpec Boilerplate
config.include FactoryBot::Syntax::Methods
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::ControllerHelpers, type: :view
config.include Devise::Test::IntegrationHelpers, type: :feature