Skip to content

Instantly share code, notes, and snippets.

View mrpunkin's full-sized avatar

Bryan Corey mrpunkin

View GitHub Profile
@mrpunkin
mrpunkin / gist:385528dbd73a50db5908
Created March 24, 2015 23:23
decorators/shopify_api/order_decorator.rb
module ShopifyAPI
module OrderDecorator
included do
before_update :remove_source_name
end
private
def remove_source_name
@mrpunkin
mrpunkin / jquery.parseq.js
Created November 13, 2014 19:44
My jQuery plugin to parse query strings, including support for arrays and hashes. Also adds String prototype function.
(function($, undef){
$.parseQuery = function(str){
var obj = {};
var soRegex = new RegExp(/\[(\S*)\]$/); // Test against sub objects / arrays
$.each(str.replace(/^\?/, "").split("&"), function(i, param){
param = param.split("=");
var k = decodeURIComponent(param[0]),
v = decodeURIComponent(param[1]);
var m = k.match(soRegex);
@mrpunkin
mrpunkin / encrypt-token.rb
Created August 22, 2014 21:10
Encryption / decryption of token. Works fine in console, fails when run through passenger and stored in DB.
def encrypt_token
aes = OpenSSL::Cipher.new('AES-256-CBC')
aes.encrypt
aes.key = Rails.application.config.shopify.secret
aes.iv = iv = aes.random_iv
encrypted = aes.update(self[:token])
encrypted << aes.final
encoded = Base64.strict_encode64(iv + encrypted)
self.send(:token=, encoded)
end
@mrpunkin
mrpunkin / trackEventAndContinue.js
Last active August 29, 2015 14:02
jQuery event handler to track a Google Analytics (analytics.js) event and continue with the element's default HTML action, such as a form submit, link follow, etc.
// Event Handler
var trackEventAndContinue;
(function($, undef){
trackEventAndContinue = function(e){
e.preventDefault();
if((this.disabled !== undef && this.disabled) || $(this).hasClass('disabled')) return false;
$(this).unbind(e);
var opts = {
'hitType': 'event',
@mrpunkin
mrpunkin / weekdays.rb
Last active August 29, 2015 13:57
A simple, light-weight script that sets up a new ActiveSupport::Duration class to handle weekdays.
module Weekdays
module NumericExtensions
def weekdays
Weekday.new(self)
end
alias :weekday :weekdays
end
module TimeExtensions
def weekday?
def halfdays
ActiveSupport::Duration.new(self*12.hours, [[:halfdays, self]])
end
FooJob = Struct.new(:shop_id) do
def perform
## code ##
end
include ShopifyJobHooks
end
FooJob = ShopifyJob.new(:shop_id) do
def perform
## code I want wrapped in superclass' session call ##
end
end
class Order < ActiveRecord::Base
def self.fulfillable_for(product_id)
select("orders.*, COUNT(up.product_id) AS unfulfillable").
joins(:order_items).
joins("LEFT JOIN order_items oi ON oi.order_id = order_items.order_id AND oi.product_id != order_items.product_id").
joins("LEFT JOIN products up ON up.product_id = oi.product_id AND up.available_at > #{ActiveRecord::Base.sanitize(Date.today)}").
where(order_items: { product_id: product_id} ).
group("order_items.order_id").
having("unfulfillable <= 0")
end
module ProductDecorator
extend ActiveSupport::Concern
def fulfillable_product
FulfillableProduct.find_or_initialize_by(product_id: id)
end
def available_at
fulfillable_product.available_at
end