Skip to content

Instantly share code, notes, and snippets.

View jackrg's full-sized avatar

Jack Royal-Gordon jackrg

  • Groundbreaking Software
  • Irvine, CA
View GitHub Profile
@jackrg
jackrg / dates_international.rb
Created June 13, 2012 23:38
Ruby International Date Parser
# -*- coding: utf-8 -*-
#
# Purpose:
# Extend the date parsing capabilities of Ruby to work with dates with international month names.
#
# Usage:
#
# Date.parse_international(date_string)
# DateTime.parse_international(date_string)
# date_string.to_international_date
class SalesReportWorker < ApplicationController
include Sidekiq::Worker
sidekiq_options backtrace: true, retry: false
self.view_paths = "app/views" # Where to look for view templates
def perform(request_ident, params)
@_response = ActionDispatch::Response.new # Needed to avoid "ActionController::RackDelegation#content_type
# delegated to @_response.content_type, but @_response is nil" error
...
@jackrg
jackrg / active_record.rb
Created May 16, 2014 18:14
Add bulk import functionality to Rails Active Record (add this file to config/initializers, call <model>.import!(array-of-record-hashes))
class ActiveRecord::Base
def self.import!(record_list)
raise ArgumentError "record_list not an Array of Hashes" unless record_list.is_a?(Array) && record_list.all? {|rec| rec.is_a? Hash }
return record_list if record_list.empty?
(1..record_list.count).step(1000).each do |start|
key_list, value_list = convert_record_list(record_list[start-1..start+999])
sql = "INSERT INTO #{self.table_name} (#{key_list.join(", ")}) VALUES #{value_list.map {|rec| "(#{rec.join(", ")})" }.join(" ,")}"
self.connection.insert_sql(sql)