Skip to content

Instantly share code, notes, and snippets.

View rubysolo's full-sized avatar

Solomon White rubysolo

View GitHub Profile
class Time
def is_after?(timestamp)
self.to_i - self.at_beginning_of_day.to_i > timestamp
end
def is_after_430_pm?
# 4:30 == 16:30
# 16*3600 (sec/hr)
#+30*60 (sec/min)
#=59400
SELECT u.id, u.first_name, u.last_name, u.email, so.shipto_postal_code, so.placed_at
FROM users u LEFT JOIN sales_orders so
ON so.user_id = u.id
AND so.state != 'pending'
WHERE u.contact_me = 1
ORDER BY u.id, so.placed_at DESC
----------------------------------
SELECT u.id, u.first_name, u.last_name, u.email, so1.shipto_postal_code, so1.placed_at
def check_for_bid_items
return if user.bids.empty?
@bids_and_items = user.bids.inject({}) do |hsh, bid|
items_for_bid = hsh[bid] || []
items_for_bid += line_items.select{|li| li.product.bids.include?(bid) }
hsh.update(bid => items_for_bid) unless items_for_bid.empty?
end
end
return if user.bids.empty?
@bids_and_items = user.bids.inject({}) do |hsh, bid|
items_for_bid = hsh[bid] || []
items_for_bid += line_items.select{|li| li.product.bids.include?(bid) }
hsh = hsh.update(bid => items_for_bid) unless items_for_bid.empty?
hsh
end
# send an email with items from each bid..
# hack searchlogic to work with date_select / datetime_select in search form
module Searchlogic
class Search
# Accepts a hash of conditions.
def conditions=(values)
values.each do |condition, value|
if condition =~ /(.*)\(1i\)$/
date_scope_name = $1
date_parts = (1..6).to_a.map do |idx|
values.delete("#{ date_scope_name }(#{ idx }i)")
class Discount
# name
# code
# effective_at
# ends_at
# type # (DollarOff, PercentOff, FreeShipping..)
# promotion?
# customer_exclusive?
# amount
# minimum_total_requirement
def add_discount(promo_code)
discount = Discount.find_by_code(promo_code)
if discount && discount.applicable?(self)
if discount.customer_exclusive?
discount_assignment = discount.assignments.find_by_user_id(user.id)
discount_assignment.update_attributes(:sales_order_id => self.id)
else
discount_assignments.create(:discount => discount)
end
# USAGE: Hash.from_xml:(YOUR_XML_STRING)
require 'nokogiri'
# modified from http://stackoverflow.com/questions/1230741/convert-a-nokogiri-document-to-a-ruby-hash/1231297#1231297
class Hash
class << self
def from_xml(xml_io)
begin
result = Nokogiri::XML(xml_io)
return { result.root.name.to_sym => xml_node_to_hash(result.root)}
@rubysolo
rubysolo / empty
Created June 17, 2011 17:34 — forked from banister/empty
pry(main):4> blame_for(Pry.instance_method(:repl)).display
John Mair def repl(target=TOPLEVEL_BINDING)
John Mair target = Pry.binding_for(target)
John Mair target_self = target.eval('self')
John Mair
John Mair repl_prologue(target)
Mon ouïe
John Mair # cannot rely on nesting.level as
John Mair # nesting.level changes with new sessions
John Mair nesting_level = nesting.size
@rubysolo
rubysolo / app_resizer.rb
Created June 24, 2011 16:48
A Mac OS application resizer script for different desktop resolutions.
require "rubygems"
require "appscript"
class AppResizer
include Appscript
# Full screen bounds for all modes.
HOME_FULL_SCREEN = [0, 0, 2560, 1440]
LAPTOP_FULL_SCREEN = [0, 0, 1440, 900]