Skip to content

Instantly share code, notes, and snippets.

View mrpunkin's full-sized avatar

Bryan Corey mrpunkin

View GitHub Profile
@mrpunkin
mrpunkin / exception_notifier.rb
Created September 5, 2013 21:17
Extension for ExceptionNotifier class to ignore "invalid byte sequence" errors.
class ExceptionNotifier
def call(env)
@app.call(env)
rescue Exception => exception
options = (env['exception_notifier.options'] ||= {})
options.reverse_merge!(@options)
unless Array.wrap(options[:ignore_exceptions]).include?(exception.class) or /invalid byte sequence/ =~ exception.message
Notifier.exception_notification(env, exception).deliver
env['exception_notifier.delivered'] = true
@mrpunkin
mrpunkin / routes.rb
Created July 15, 2013 22:05
Preferred method for named routes in a single, non-resource controller?
## Option A
scope "search", :controller => :search do
root :to => 'search#index', :as => 'search'
get :users, :as => "search_users"
get :photos, :as => "search_photos"
end
## Option B
@mrpunkin
mrpunkin / gist:5700075
Created June 3, 2013 18:08
Determine object from restful / polymorphic routes, taking into account the possibility that to_param has been overridden for the given object's class.
cls = controller_name.classify.constantize
paramtest = cls.new
paramtest.attributes.map{|name,val| paramtest[name] = name }
to_param = paramtest.to_param
@object = cls.send("find_by_#{to_param}", params[:id])
@mrpunkin
mrpunkin / Client.php
Last active December 21, 2016 14:27
Fix to re-process header if initial header is 100 Continue in Amazon MWS Fulfillment Outbound Shipment PHP client library
<?php
...
//// Starting @ line 635 of FBAOutboundServiceMWS/Client.php
list($other, $responseBody) = explode("\r\n\r\n", $response, 2);
$other = preg_split("/\r\n|\n|\r/", $other);
list($protocol, $code, $text) = explode(' ', trim(array_shift($other)), 3);
// Re-check for new header after continue header.
arels = [Vote, Comment, Love].collect{|u|
t = u.arel_table
u.select([
t[:created_at].maximum.as("ts"),
t[:photo_id].as("what_id"),
"'Photo' AS what_type"
]).group(t[:photo_id]).arel
}
union = Arel::Nodes::Union.new(arels[0], arels[1])
SELECT photos.id FROM photos
LEFT OUTER JOIN votes ON votes.photo_id = photos.id
LEFT OUTER JOIN flags ON flags.photo_id = photos.id AND flags.type="Love"
LEFT OUTER JOIN comments ON comments.photo_id = photos.id
WHERE photos.user_id = 38218
GROUP BY photos.id;
class Challenge < ActiveRecord::Base
has_many :challenge_photos, :dependent=>:destroy
has_many :photos, :through=>:challenge_photo
end
validate :group_is_real
def group_is_real
errors.add(:group, "must be an already existing group") if group_id and !group
end
@mrpunkin
mrpunkin / gist:4502921
Created January 10, 2013 15:37
Recently moved signup / login to our homepage. Any reason not to do this?
root :to => 'application#index', :as => :home
root :to => 'application#index', :as => :new_user, :via=>:get, :defaults=>{:anchor=>"signup"}
root :to => 'application#index', :as => :login_users, :via=>:get
TIMESTAMPDIFF(HOUR, '2012-08-30 22:13:31', "2012-12-19 19:04:37") -- Gives me 2660
time_to_sec(timediff("2012-12-19 19:04:37", '2012-08-30 22:13:31'))/3600 -- Gives me 838.9997