Skip to content

Instantly share code, notes, and snippets.

View somazx's full-sized avatar
🏠
Working from home

Andy Koch somazx

🏠
Working from home
  • British Columbia, Canada
View GitHub Profile
@somazx
somazx / Gemfile
Last active February 1, 2017 20:03
Using Amazon Elasticsearch securely (signed requests) with Rails & searchkick gem on Heroku.
gem 'elasticsearch', '>= 1.0.15'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'patron'
gem 'faraday_middleware-aws-signers-v4'
gem 'searchkick'
@somazx
somazx / nested, serialized dataobjects.md
Last active May 3, 2022 14:25
Working with nested, serialized dataobjects in Rails

Rails and serialized nested DataObjects

I wanted to store arbitrary nested data structures in a jsonb column and be able to retrieve and work with that data seemlessly within a Rails application. There are several tutorials covering different aspects of this, but none felt complete to me.

For this example we'll be working with a User model that can define surveys. Each survey consists of n multiple choices questions, and each question can have between two and ten answer options.

Requirements:

In this case we use Postgresql's JSONB support to store the data, but you could also use other forms of serializing such as a text column and Rails' default YAML serializing. JSONB is chosen in this case because it leaves open the possiblity of querying the data with SQL. Also, apparently serializing to JSON is much faster.[1]

Keybase proof

I hereby claim:

  • I am somazx on github.
  • I am somazx (https://keybase.io/somazx) on keybase.
  • I have a public key ASC6zNYYHXFpwjIR-jMU4mCTT90NtXeNrzhD-k2mU-vd4go

To claim this, I am signing this object:

@somazx
somazx / modern_rails.md
Last active December 31, 2018 00:21
Improved Rails

Pain Points in Rails

Rails has several pain-points for more complex web applicatoins that frameworks like Hanami and Trailblazer attempt to address. By addressing theses issues your business logic is better isolated from the framework details/implementation, clearer responsibilities, easier reasoning about, cleaner code, creates opportunity for easy further abstraction when necessary, allows for simpler testing, etc.

Identified Pain Points and potential solutions

Better file structure/autoloading that allows grouping of domain concerns under a single folder structure

Solutions: use custom autoload_paths/eager_load_paths in an initializer?

# config/application.rb
config.autoload_paths = Dir['app/domains/*/*/']
@somazx
somazx / scraper.js
Last active April 15, 2023 16:45
Humble Bundle Unredeemed Keys Scraper
/*
Purpose: easily obtain a list of your humble bundle games that haven't been redeemed.
Instructions:
1) Log into Humble Bundle as usual.
2) Navigate to https://www.humblebundle.com/home/keys
3) Open browser console and run the following script in the console
Script will dump to console the scraped list of all your unredeemed game keys.
<template>
<v-list-tile-action v-if="email || phone || phone2">
<v-menu>
<template v-slot:activator="{ on }">
<v-btn flat v-on="on">
<v-icon color="blue">fa-comment-dots</v-icon>
</v-btn>
</template>
<v-list>
@somazx
somazx / maxlength_controller.js
Created March 29, 2023 13:42
Max Length for Input - Stimulus Controller
import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["counter", "input"];
connect() {
this.updateCounter();
}
trackLength() {
@somazx
somazx / orderable_controller.js
Created March 29, 2023 13:46
Orderable: Drag & Drop Ordering - Stimulus Controller
import { Controller } from "stimulus";
// Handles ordering elements on the page using
// buttons than can move the elements up or down.
//
// Usage:
// 1. Add data-controller orderable on a parent node.
// 2. (Optional) For persisting order to input fields:
// - add data-orderable-target="orderInput"
// 3. Wire up buttons or links to call up/down actions.