This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def if_multiple_pages?(collection, per_page = 10) | |
| pages = (collection.size / (per_page || 10).to_f).ceil | |
| yield pages if pages > 1 | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}))', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # | |
| # 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Factory.define :album do |album| | |
| album.name 'Album Name' | |
| album.description 'Album Description' | |
| album.active true | |
| album.association :cover_image, :factory => :image | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (('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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def collection_class_name(collection, index) | |
| if index == 0 | |
| return 'first' | |
| elsif index == ( collection.size - 1 ) | |
| return 'last' | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ActionView::Base.field_error_proc = Proc.new do |html_tag, instance| | |
| if html_tag =~ /<label[^>]+/ | |
| "<div class='field_with_errors'>#{html_tag}".html_safe | |
| else | |
| "#{html_tag}</div>".html_safe | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'rack/deflater' | |
| module Rack | |
| class Deflater | |
| class GzipStream | |
| def each(&block) | |
| @writer = block | |
| gzip =::Zlib::GzipWriter.new(self) | |
| gzip.mtime = @mtime | |
| @body.each { |part| |
OlderNewer