This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
# Converts a one dimensional array into a table with the given number of columns. Optionally transposes | |
# the result. Suited for output to an HTML table you want sorted vertically instead of horizontally. | |
# Pads correctly, i.e., each row/column has at most one empty cell. num_columns is the desired number | |
# of columns in the result, regardless if the table is transposed. | |
def tabulate(num_columns, transpose=true) | |
if transpose | |
num_rows = num_columns | |
num_columns = ((self.size - 1) / num_columns) + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Architecture: Rails 3.2, Solr 1.4 with sunspot_solr gem, Apache HTTP server as reverse proxy to | |
Rails app, Jquery UI | |
Note: I don't care about the distinction between autocomplete and autosuggest. I am implementing what some | |
people call autosuggest, and I'm calling it autocomplete. | |
Given the above existing architecture, I want to add an autocomplete field into my app for a single | |
attribute on a single model. It needs to be fast and secure (duh). The search target field could have | |
white space and the search input should allow it, e.g., if I search with "iker's gui" it should return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pduey: a couple of public and private functions to add or remove a query string parameter + value pair. | |
// | |
// I like this implementation because it transforms the query string into a coherent hash with unique keys | |
// and an Array for the values, so it handles repeated query string parameter names, which are meant to be | |
// interpreted as Arrays, yo. The "private" functions could, of course, be handy for other uses. | |
// | |
// The function names include "search" because they are derived from window.location.search. | |
// Ideally, I'd attach these to the window object, as in, window.location.prototype. | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I needed to overwrite (not override) the Savon::Client.request method in my development | |
# environment only, and only for 1 type of request that happens to hit a host I can access | |
# only in production. For all other requests, it should invoke the original Savon::Client.request. | |
# I found it a little tricky since it takes optional args and a block. Here's what worked, which | |
# I learned from these two stackoverflow answers | |
# http://stackoverflow.com/questions/4470108/when-monkey-patching-a-method-can-you-call-the-overridden-method-from-the-new-i | |
# http://stackoverflow.com/questions/89650/how-do-you-pass-arguments-to-define-method | |
# It's maybe similar to how rspec implements the stub method? | |
Savon::Client.class_eval do |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Save as file, e.g., luhn_validator.rb, make executable, then: ./luhn_validator.rb 'number' | |
DIGIT_REDUX = [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] # simple lookup instead of doubling and summing if > 9 | |
def valid_luhn_number?(number) | |
digits = number.to_s.split(//).map{|d| d.to_i} | |
digits.reverse!.each_index{|i| digits[i] = DIGIT_REDUX[digits[i]] if i.odd?}.reduce(:+) % 10 == 0 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# In order to use Rails Active Storage Direct Upload to a cloud service, e.g., using Rails | |
# Action Text rich text editor image upload button, the cloud service will need CORS | |
# configured. The GCS CORS doc at https://cloud.google.com/storage/docs/configuring-cors | |
# describes 3 ways of doing it. I wanted to use the Ruby code method, since I already had | |
# GCS configured for my app and could avoid configuring some other utility. The key was | |
# getting a handle on a client from my rails app. So, assuming you have already uploaded | |
# something via your app (not via direct upload): | |
client = ActiveStorage::Blob.last.service.send :client | |
bucket = client.buckets.first | |
bucket.update do |bucket| |