Skip to content

Instantly share code, notes, and snippets.

View hartct's full-sized avatar

Chris Hart hartct

View GitHub Profile
@hartct
hartct / prorating_prices.rb
Created December 27, 2010 16:12
A couple methods to prorate prices by calculating the remaining days of the month
require 'date'
def days_remaining_in_month
day_of_month = Time.now.day
(days_in_month(Time.now.year, Time.now.month) - day_of_month)
end
def prorated_price_today(price)
day_of_month = Time.now.day
percent_of_month_remaining = BigDecimal(days_remaining_in_month.to_s) / BigDecimal(days_in_month(Time.now.year, Time.now.month).to_s)
@hartct
hartct / credit_card_validation.js
Created December 27, 2010 16:16
JavaScript function for validating subscription information - assumes certain form input IDs for name,adress, CVV, card number.
function cardval() {
// validate presence of first name, last name, billing address and CVV.
if ($("#subscription_first_name").val().length == 0 || $("#subscription_last_name").val().length == 0 || $("#subscription_address").val().length == 0 || $("#subscription_city").val().length == 0 || $("#subscription_state").val().length == 0 || $("#subscription_zipcode").val().length == 0 || $("#subscription_cvv2").val().length == 0) {
alert("All fields on the form must be filled out. Please complete the form and try again.");
return false;
}
// check CVV2 is 3 or 4 digits
var re = new RegExp(/^\d{3,4}$/);
@hartct
hartct / routes.rb
Created September 22, 2011 00:53
Problematic routes.rb with multiple devise routes for same resource
devise_for :admins, :controllers => { :registrations => ‘admin/registrations’, :sessions => ‘admins/sessions’ }
# other routes here
devise_for :admins # oops, this won't work!
@hartct
hartct / mobile.html
Created September 22, 2011 00:59
CSS links for both iPhone & iPad
<link href="/stylesheets/mobile/tablet.css?1308515841" media="only screen and (max-device-width: 1024px) and (-webkit-min-device-pixel-ratio: 1)" rel="stylesheet" type="text/css" />
<link href="/stylesheets/mobile/mobile.css?1308364516" media="only screen and (max-device-width: 480px)" rel="stylesheet" type="text/css" />
@hartct
hartct / vcal.rb
Created September 22, 2011 02:41
Custom properties for Outlook vCalendar/iCalendar
cal = Calendar.new
# normal iCalender stuff here...
cal.custom_property("METHOD", "PUBLISH")
cal.custom_property("X-MS-OLK-FORCEINSPECTOROPEN", "TRUE")
@hartct
hartct / invitation_mailer.rb
Created September 22, 2011 02:44
Attaching vCalendar/iCalendar files for Outlook invitations
attachments['meeting.ics'] = { :content_type => 'text/calendar; method=REQUEST', :content => notification.ical_text, :encoding => '8bit' }
@hartct
hartct / iui-patch.txt
Created October 30, 2011 00:45
Patch for iUI.js to correct unnecessary scrolling to the top of page
--- iui.js
+++ iui.js
@@ -677,12 +677,12 @@ function orientChangeHandler()
switch(orientation)
{
case 0:
- setOrientation(portraitVal);
+ setOrientationAndScroll(portraitVal);
break;
@hartct
hartct / foreigner_can_delete_ar_patch.rb
Created August 26, 2012 00:40
Check if a record can be deleted when using foreigner gem
module ActiveRecord
class Base
def can_delete?
this_model_name = self.class.name
associations = Kernel.const_get(this_model_name).reflect_on_all_associations
has_one_models = associations.map{ |x| x.name.to_s if x.macro == :has_one }.compact
has_one_models.each do |child|
if self.send(child).present?
return false
end
@hartct
hartct / institution_mapping.rb
Created October 25, 2012 03:55
Example of using post_save method with xml-mapping gem
class InstitutionLogin
include XML::Mapping
# added namespaces to make root element compliant with recipients's expectation
def post_save xml, options={:mapping=>:_default}
xml.root.add_attributes("xmlns"=>"http://schema.intuit.com/platform/fdatafeed/institutionlogin/v1")
xml.root.add_namespace "xsi", "http://www.w3.org/2001/XMLSchema-instance"
xml.root.add_namespace "xsd", "http://www.w3.org/2001/XMLSchema"
end
self.root_element_name "InstitutionLogin"
object_node :credentials, "credentials", :default_value => nil
@hartct
hartct / gist:5279334
Last active December 15, 2015 15:09 — forked from rafaelss/gist:3700977
1. launchctl unload -w ~/Library/LaunchAgents/org.postgresql.postgres.plist
2. mv /usr/local/var/postgres9 /usr/local/var/postgres90
3. curl https://raw.github.com/fragility/homebrew/737af01178590950749cf5e841f2d086c57c5a80/Library/Formula/postgresql.rb > /usr/local/Library/Formula/postgresql.rb
4. brew install postgresql # if this doesn't work, homebrew may have an existing installation. you can try a brew upgrade postgresql but some of the instructions that follow may need to change...
4b. at this point, check to make sure that the postgres binaries are linked and in the path correctly. try doing psql -V and confirm it shows version 9.2. If it doesn't, adjust your path so /usr/local/Cellar/postgresql/9.2.0/bin/ is in front of the offending locations with postgres binaries in them or fight with brew link to fix it.
5. initdb /usr/local/var/postgres -E utf8
6. Edit ~/Library/LaunchAgents/org.postgresql.postgres.plist file to point to your new locations, probably looking like this:
<?xml version="1.0" encodi