Skip to content

Instantly share code, notes, and snippets.

View jeremywrowe's full-sized avatar
❤️
Coding

Jeremy W. Rowe jeremywrowe

❤️
Coding
View GitHub Profile
@jeremywrowe
jeremywrowe / rant.md
Last active December 21, 2015 16:48
Overuse of convenience methods

I have been doing a lot of about convience methods in Ruby lately. I have, noticed others that over use the convience of creating a class with instance level methods and wrapping all of it in a class level method for convience. (this is not new to me - just to be clear. Rather something I am reflecting on)

consider:

class FunkyBusiness
  attr_reader :initial_state

  def initialize(initial_state)
    @initial_state = initial_state
@jeremywrowe
jeremywrowe / bypass_auth.rb
Created September 2, 2013 23:57
Getting current user in tests is slow and painful.. but you can do something like this to speed up your test suite. Writing a blog about it sometime in the future.
def bypass_current_user(user, &block)
raise "Block must be given.." unless block_given?
$current_user = user
begin
ApplicationController.class_exec do
alias original_current_user current_user
def current_user
$current_user
end
yield
@jeremywrowe
jeremywrowe / single-on-off-components.js
Created November 16, 2013 14:55
This is an example of limiting selection of a set of on/off components on Chargify hosted pages to select only a single component. This script can be utilized by adding it to your "Custom Javascript" on the "Hosted Pages Settings" found under the "Settings" tab. Example Video: http://cl.ly/2237413N071y/on-off-components-single-selection.mov
$(function() {
var updatingCheckboxes = false,
$components = $('.component-checkbox');
$components.change(function(e) {
if(updatingCheckboxes) { return; }
var $el = $(this),
checked = $el.attr("checked");
if(checked) {
e.preventDefault();
updatingCheckboxes = true;
@jeremywrowe
jeremywrowe / require_billing_zip.js
Created November 21, 2013 14:44
This is an example of requiring a non required field on Chargify hosted pages through the use of our custom javascript. (this example demonstrates required billing zip with all other billing fields not required. This script can be utilized by adding it to your "Custom Javascript" on the "Hosted Pages Settings" found under the "Settings" tab. Exa…
$(function() {
var $form = $("form#hosted-payment-form"),
$submit = $form.find("input[type='submit']"),
$label = $form.find("label[for='subscription_payment_profile_attributes_billing_zip']"),
submitText = $submit.val();
// Mark billing zip as required
$label.text("*" + $label.text());
@jeremywrowe
jeremywrowe / saving_private_ruby.rb
Created January 6, 2014 14:13
This is a display of taking advantage of ruby 2.1 returning a symbol representation of methods when created and passing it to private as a single argument.
class Test
# :( wish this worked
private def self.bar
puts "self bar"
end
# would be nice to not have to do this
class << self
private def nook
puts "self nook"
@jeremywrowe
jeremywrowe / refinement.rb
Last active January 2, 2016 09:29
Example of ruby 2.1 refinements and their glory. Use with caution -- They are kind of code stinkies.
module Refinement
refine String do
def reverse
self[0..1]
end
end
end
class Boom
require 'active_support/core_ext/string/inquiry.rb'
def speak_like_a(val)
is = val.inquiry
if is.chicken?
puts "bagaaawk"
elsif is.pig?
puts "oink"
else
puts "sorry don't know what tongue that speak in"
@jeremywrowe
jeremywrowe / Rakefile
Created February 16, 2014 03:06
Jekyll Rake task for making a new post
desc 'creates a new post prefixed with the current date'
task :post, :name, :title, :categories do |task, args|
File.open(File.join('_posts', Time.now.strftime("%Y-%m-%d-#{args[:name].split(' ').join('-')}.markdown")), 'w') do |file|
file << <<-DOC
---
layout: post
title: "#{args[:title].downcase}"
date: #{Time.now.strftime("%Y-%m-%d-%H:%M:%S")}
categories: #{args[:categories].downcase}
---
@jeremywrowe
jeremywrowe / class_level_privates.rb
Created February 22, 2014 01:33
I see the top implementation of class level private methods a lot. Please understand that the private means nothing in that case. Do you know why? :)
# The wrong way to make a class level method private
class Foo
def self.bar
p "bar"
end
private
def self.baz
@jeremywrowe
jeremywrowe / clogs.zsh.sh
Created March 16, 2014 22:57
ZSH function to truncate Rails logs
function clogs() {
if [[ -d log ]]; then
echo 'cleaning logs'
: > log/*.log
else
echo 'logs dir not found'
fi
}