Skip to content

Instantly share code, notes, and snippets.

View article_channel.rb
class ArticleChannel < ApplicationCable::Channel
def subscribed
@article = Article.find(params[:article_id])
@editor_id = params[:editor_id]
# Write it only if the editor is truly nil in a single query to avoid race conditions
Article.where(id: @article.id, editor: nil).update(editor: @editor_id)
stream_for @article
View Bug test
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
View bash
Version4::Foo.last.bars.first.class
=> Version4::Bar
Version5::Foo.last.bars.first.class
=> Version4::Bar
View gist:b9cd530973b2f8a422c174986280b7a2
# The models
class TextMessage < ApplicationRecord
belongs_to :account, inverse_of: :text_messages, optional: true
belongs_to :user, inverse_of: :text_messages, optional: true
has_many :phones, ->(text_message) { joins(name: :products).where("products.account_id = ?", text_message.account_id) }, foreign_key: :number, primary_key: :client_phone_number
has_many :names, through: :phones, source: :name
class Phone < ApplicationRecord
belongs_to :name, inverse_of: :phones
View keybase.md

Keybase proof

I hereby claim:

  • I am glennfu on github.
  • I am glennfu (https://keybase.io/glennfu) on keybase.
  • I have a public key ASAVcYf3fpA81yrW6C_8vxGA37B59iFDv2SgKUvAKlXbZQo

To claim this, I am signing this object:

@glennfu
glennfu / geokit_commands.rb
Last active Jul 13, 2016
Ways to get data out of geokit calls to Google
View geokit_commands.rb
def parse_stuff
geoloc = Geokit::Geocoders::GoogleGeocoder3.geocode address.gsub("\n", ", ")
placemark = JSON.parse(geoloc.to_json)
# Sometimes the most accurate entry doesn't have a city, but a less accurate version does
if city.blank? && region.present? && geoloc.respond_to?(:all)
placemark["city"] = geoloc.all.detect(&:city).try(&:city)
end
end
@glennfu
glennfu / geokit.rb
Last active Jul 13, 2016
I used this to help me parse some locations where the default Geokit wasn't enough
View geokit.rb
require 'geokit'
module Geokit
module IpGeocodeLookup
# Overriding this to only store for 2 hours instead of 30 days
def store_ip_location
return if params[:lat] && params[:lng]
unless session[:geo_location]
@glennfu
glennfu / turbolinks.js
Created Jun 24, 2016
turbolinks.js to go along with glennfu/turbolinks-rails
View turbolinks.js
View app_models_inventory_item.rb
class InventoryItem < ActiveRecord::Base
def self.update_kit_ids
search = NetSuite::Records::InventoryItem.search({
criteria: {
basic: [
{
field: 'type',
operator: 'anyOf',
type: 'SearchEnumMultiSelectField',
View turbolinks_prefetch.coffee