Skip to content

Instantly share code, notes, and snippets.

View bencrouse's full-sized avatar

Ben Crouse bencrouse

View GitHub Profile
@bencrouse
bencrouse / gateway.rb
Last active June 16, 2020 12:03
Custom shipping gateway
module ActiveShipping
class MyBuild < ActiveShipping::Fedex
def find_rates(*args)
results = super
# some code to customize the results
return results
end
end
end
@bencrouse
bencrouse / brands.rb
Last active May 29, 2020 12:07
Brand aggregation
module Workarea
module Search
class Brands
include ProductDisplayRules
def all_results
buckets = Array.wrap(response.dig('aggregations', 'all_brands', 'buckets'))
buckets.map { |b| b['key'] }
end
@bencrouse
bencrouse / current.rb
Created October 19, 2016 14:09
Mongoid Criteria merging fixes
_key == '$in' ? new_val & old_val : old_val | new_val
@bencrouse
bencrouse / gist:6767822
Created September 30, 2013 18:14
Delete multiple keys from Redis
redis-cli keys 'query*' | awk '{ gsub("\"", "*", $1); print "\"" $1 "\"" }' | xargs redis-cli DEL
@bencrouse
bencrouse / paranoia_probs.rb
Created August 2, 2012 16:47
Mongoid 3.0 Paranoia Problems
1.9.3p194 :006 > Catalog::Product.last
=> #<Catalog::Product _id: PROD9990094, _type: nil, deleted_at: nil, version: 1, created_at: 2012-07-31 20:43:50 UTC, updated_at: 2012-07-31 20:43:50 UTC, slug: "test-product-211", display_starts_at: 2012-07-31 20:43:50 UTC, display_ends_at: nil, purchase_starts_at: 2012-07-31 20:43:50 UTC, purchase_ends_at: nil, site_id: 1, name: "Test Product 211", sale_starts_at: nil, sale_ends_at: nil, display_price_range: false, packaged_product_ids: []>
1.9.3p194 :007 > Catalog::Product.last.destroy
=> 0
1.9.3p194 :008 > Catalog::Product.last
=> #<Catalog::Product _id: PROD9990094, _type: nil, deleted_at: 2012-08-02 16:46:50 UTC, version: 1, created_at: 2012-07-31 20:43:50 UTC, updated_at: 2012-07-31 20:43:50 UTC, slug: "test-product-211", display_starts_at: 2012-07-31 20:43:50 UTC, display_ends_at: nil, purchase_starts_at: 2012-07-31 20:43:50 UTC, purchase_ends_at: nil, site_id: 1, name: "Test Product 211", sale_starts_at: nil, sale_ends_at: nil, display_price_range: false, p
@bencrouse
bencrouse / rails_rescues.rb
Created July 9, 2012 20:58
ActionDispatch::ShowExceptions.rescue_responses
{"ActionController::RoutingError"=>:not_found,
"AbstractController::ActionNotFound"=>:not_found,
"ActionController::MethodNotAllowed"=>:method_not_allowed,
"ActionController::NotImplemented"=>:not_implemented,
"ActionController::InvalidAuthenticityToken"=>:unprocessable_entity,
"ActiveRecord::RecordNotFound"=>:not_found,
"ActiveRecord::StaleObjectError"=>:conflict,
"ActiveRecord::RecordInvalid"=>:unprocessable_entity,
"ActiveRecord::RecordNotSaved"=>:unprocessable_entity
}
@bencrouse
bencrouse / tire.rb
Created June 4, 2012 16:49
putting data into an index
data = { id: 'id', name: 'something' }
index = Tire::Index.new('index_name')
index.store(data)
index.refresh #(optionally)
@bencrouse
bencrouse / .gitconfig
Created January 19, 2012 21:40
gitconfig
[user]
name = bcrouse
email = bcrouse@weblinc.com
[core]
[color]
ui = true
[merge]
tool = opendiff
[core]
editor = /usr/bin/vim
class AddItemToCartCommand
include Command
validate :sku, presence: true
validate :quantity, presence: true, numericality: { greater_than_or_equal_to: 1 }
def cart
Cart.from_session(params[:session_id])
end
@bencrouse
bencrouse / capybara.rb
Created December 15, 2011 16:35
Region Select
select 'Pennsylvania', from: 'shipping_address[region]'