Skip to content

Instantly share code, notes, and snippets.

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
# 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"
Version4::Foo.last.bars.first.class
=> Version4::Bar
Version5::Foo.last.bars.first.class
=> Version4::Bar
# 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

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 July 13, 2016 15:59
Ways to get data out of geokit calls to Google
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 July 13, 2016 15:54
I used this to help me parse some locations where the default Geokit wasn't enough
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 June 24, 2016 17:13
turbolinks.js to go along with glennfu/turbolinks-rails
class InventoryItem < ActiveRecord::Base
def self.update_kit_ids
search = NetSuite::Records::InventoryItem.search({
criteria: {
basic: [
{
field: 'type',
operator: 'anyOf',
type: 'SearchEnumMultiSelectField',