Skip to content

Instantly share code, notes, and snippets.

View chrisbloom7's full-sized avatar
💭
I may be slow to respond.

Chris Bloom chrisbloom7

💭
I may be slow to respond.
View GitHub Profile
@chrisbloom7
chrisbloom7 / slack_ruby_client_websockets.rake
Last active August 21, 2023 08:33
Basic implementation of a websocket connector for Slack using slack-ruby-client and async-websockets gems
# frozen_string_literal: true
# Basic implementation of a websocket connector for Slack using the slack-ruby-client gem
#
# Run this rake task in a virtual container that is set to automatically restart on failure; this
# will help deal with disconnects from Slack since socket URLs are temporary.
# https://api.slack.com/apis/connections/socket-implement#disconnect
#
# This rake task can be called in multiple virtual containers to help with resilliancy and rolling restarts
# https://api.slack.com/apis/connections/socket-implement#connections
@chrisbloom7
chrisbloom7 / longest_common_substring.php
Created June 12, 2011 03:27
Find the longest common substring in an array of strings (PHP)
<?php
function longest_common_substring($words)
{
$words = array_map('strtolower', array_map('trim', $words));
$sort_by_strlen = create_function('$a, $b', 'if (strlen($a) == strlen($b)) { return strcmp($a, $b); } return (strlen($a) < strlen($b)) ? -1 : 1;');
usort($words, $sort_by_strlen);
// We have to assume that each string has something in common with the first
// string (post sort), we just need to figure out what the longest common
// string is. If any string DOES NOT have something in common with the first
// string, return false.
@chrisbloom7
chrisbloom7 / README.md
Created June 6, 2011 07:16
A cheap knock off of the default validates_length_of validator, but checks the filesize of a Carrierwave attachment

Note that this validation runs both after the file is uploaded and after CarrierWave has processed the image. If your base uploader includes a filter to resize the image then the validation will be run against the resized image, not the original one that was uploaded. If this causes a problem for you, then you should avoid using a resizing filter on the base uploader and put any specific size requirements in a version instead.

So instead of this:

require 'carrierwave/processing/mini_magick'

@chrisbloom7
chrisbloom7 / random_password.rb
Created April 24, 2012 05:47
Generate a random password containing at least one number and one special character
# Generate a password that is 8 - 20 characters in length, and which contains at least one number and one special character
# Requires Ruby >= 1.9
def random_password
specials = ((32..47).to_a + (58..64).to_a + (91..96).to_a + (123..126).to_a).pack('U*').chars.to_a
numbers = (0..9).to_a
alpha = ('a'..'z').to_a + ('A'..'Z').to_a
%w{i I l L 1 O o 0}.each{ |ambiguous_character|
alpha.delete ambiguous_character
}
characters = (alpha + specials + numbers)
@chrisbloom7
chrisbloom7 / mod_xsendfile_example.php
Created March 4, 2011 04:27
Example of using mod_xsendfile to send a file as a download.
<?php
/**
* In your Apache conf file, set the following to enable XSendfile:
*
<Directory "/">
# This should be handled by the XSendFile module, but a bug in 0.11.1 prevented it from being set properly.
EnableSendfile on
XSendFile on
@chrisbloom7
chrisbloom7 / README.md
Last active October 20, 2021 22:10
Simple rails-ish app that demonstrates bug in paper_trail gem when using serialized fields that contain unicode characters

I've found a bug that is specific to the following scenario:

  • Using MySQL
  • Using a longblob column type
  • Using a serialized attribute
  • Using paper_trail gem
  • Serialized field contains unicode character

Under those conditions, after saving changes to the serialized field, record#changed? still reports true, and record#changes contains an entry for the serialized field where both the before and after elements are identical. Calling record#reload clears the changes and loads the record with the changed value. If paper_trail is removed from the scenario, the ActiveModel attribute mutation tracking works as expected.

test result reserved method call type error reported
Error :render_to_body called directly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :render_to_body called indirectly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :instance_variable_get called directly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :instance_variable_get called indirectly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :process called directly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :process called indirectly ArgumentError: wrong number of arguments (given 1, expected 0)
Error :logger called indirectly SystemStackError: stack level too deep
Error :logger called directly SystemStackError: stack level too deep
Error :send_action called indirectly ArgumentError: wrong number of arguments (given 1, expected 0)
@chrisbloom7
chrisbloom7 / the_hardest_thing_for_me_to_grok_in_ruby.rb
Created March 11, 2010 16:09
The hardest thing for me to grok in Ruby
colors = ["red"] #=> ["red"]
colors_2 = colors #=> ["red"]
colors_2 << "blue" #=> ["red", "blue"]
colors #=> ["red", "blue"]
def add_white (original_colors)
original_colors << "white"
end
add_white colors #=> ["red", "blue", "white"]
@chrisbloom7
chrisbloom7 / singleton_test_isolation.rb
Created October 2, 2019 02:57
Using an anonymous subclass to test singleton objects in isolation
class MySingletonClass
include Singleton
attr_reader :client
def initialize
@client = Service::We::Only::Ever::Need::One::Connection::To.new(SERVICE_URL)
end
end
@chrisbloom7
chrisbloom7 / change.rb
Last active September 10, 2019 11:40
Dynamic enum field values in nested association forms in Rails Admin
# app/models/change.rb
class Change < ActiveRecord::Base
belongs_to :changeset, inverse_of: :changed_fields
validates :changeset, presence: true, associated: true
validates :field, presence: true, uniqueness: {scope: :changeset_id}, inclusion: { in: :field_enum }
def field_enum
if changeset.try(:model).present?
changeset.model_attributes
else