Skip to content

Instantly share code, notes, and snippets.

View zporter's full-sized avatar
👨‍💻

Zach Porter zporter

👨‍💻
View GitHub Profile
@zporter
zporter / chop.exs
Last active August 29, 2015 13:56
Learning Elixir: Guess a number between a given range
defmodule Chop do
def guess(actual, range = low..high) when actual >= low and actual <= high do
_guess(actual, ask(div(low + high, 2)), range)
end
defp _guess(actual, actual, _) do
IO.puts "\nYES! It is #{actual}\n"
end
@zporter
zporter / tmux.conf
Created May 8, 2014 14:14
tmux config
# Set prefix to Ctrl-a
set -g prefix C-a
bind C-a send-prefix
# Unbind previous Ctrl-b prefix
unbind C-b
# Change default delay between keystrokes
set -s escape-time 1
@zporter
zporter / .gitignore-global
Created June 13, 2014 14:04
Global Gitignore
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
@zporter
zporter / full_url_validator.rb
Created June 18, 2014 20:26
Rails: Full URL validator
class FullUrlValidator < ActiveModel::EachValidator
VALID_SCHEMES = %w(http https)
def validate_each(record, attribute, value)
unless valid_full_url?(value)
record.errors[attribute] << (options[:message] || 'is not a valid URL')
end
end
@zporter
zporter / if_multiple_pages.rb
Created October 26, 2010 17:18
Method used to determine if multiple pages are necessary
def if_multiple_pages?(collection, per_page = 10)
pages = (collection.size / (per_page || 10).to_f).ceil
yield pages if pages > 1
end
@zporter
zporter / many_to_many_self.rb
Created November 11, 2010 13:53
Rails model with a many-to-many relationship upon itself
class Product < ActiveRecord::Base
# ... other code
has_and_belongs_to_many :products,
:join_table => "related_products",
:foreign_key => "product_a_id",
:association_foreign_key => "product_b_id",
:finder_sql => 'SELECT DISTINCT products.* FROM products INNER JOIN related_products ON (#{id} = related_products.product_a_id OR #{id} = related_products.product_b_id) WHERE (products.id <> #{id})',
:insert_sql => 'INSERT INTO related_products (product_a_id, product_b_id) VALUES (#{id}, #{record.id})',
:delete_sql => 'DELETE FROM related_products WHERE (product_a_id = #{id} AND product_b_id IN (#{record.id})) OR (product_a_id = #{record.id} AND product_b_id IN (#{id}))',
@zporter
zporter / category.rb
Created November 22, 2010 20:48
options_for_select method to accommodate sub-categories
class Category < ActiveRecord::Base
has_many :categories, :order => 'order_by asc'
belongs_to :category
scope :order, order("order_by asc")
scope :main, where(:category_id => nil)
def options_for_select( selected = nil )
selected_attribute = ' selected="selected"' if self == selected
options_output = "<option value='#{self.id}'#{selected_attribute}>#{self.title}</option>"
@zporter
zporter / test_helper.rb
Created January 20, 2011 20:47
Speed up tests with the following gist
#
# Credit goes to jamis
# http://37signals.com/svn/posts/2742-the-road-to-faster-tests
#
# wipe almost all of the instance variables from the object
teardown :scrub_instance_variables
@@reserved_ivars = %w(@loaded_fixtures @test_passed @fixture_cache @method_name @_assertion_wrapped @_result)
@zporter
zporter / albums.rb
Created February 3, 2011 21:56
Factory for Album model
Factory.define :album do |album|
album.name 'Album Name'
album.description 'Album Description'
album.active true
album.association :cover_image, :factory => :image
end