Skip to content

Instantly share code, notes, and snippets.

View zporter's full-sized avatar
👨‍💻

Zach Porter zporter

👨‍💻
View GitHub Profile
@zporter
zporter / gist:1153918
Created August 18, 2011 11:53
RVM 1.7 Notes
NOTE: For all installations, as of 1.7, RVM no longer autoloads .rvmrc files. In order to return this
functionality, you MUST add 'export rvm_project_rvmrc=1' to your $HOME/.rvmrc file.
This causes RVM to override 'cd' which, while toggleable even < 1.7, is currently defaulted to 'off'.
This knob returns the previous behaviour to active which causes per-project .rvmrc files to be loaded
once again.
Example: echo 'export rvm_project_rvmrc=1' >> $HOME/.rvmrc && rvm reload
@zporter
zporter / collection_class_name.rb
Created July 20, 2011 18:13
HTML Helper method to return appropriate class name based on iteration in the each loop
def collection_class_name(collection, index)
if index == 0
return 'first'
elsif index == ( collection.size - 1 )
return 'last'
end
end
@zporter
zporter / pw_gen.rb
Created June 7, 2011 19:47
Nice little hash / password generator for Ruby 1.9
(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a).sample(30).join #=> "yh8D5NrognVw16GldXPcCxJ24ALU3W"
# with special characters
(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a + ['!','@','#','$','%','^','&','*']).sample(30).join #=> "Nc&inVolamjMLWfsE#k6$D^p2!b@XC"
@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
@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 / 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 / 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 / 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