Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
FILE HOSTS PREMIUM ACCOUNT
ALL FILE HOST PREMIUM ACCOUNTS
Zynga Slingo Trainer v5.12
iTunes Gift Card Generator V3.1 2012
Diablo 3 GOLD Coins FREE
Working PS3 Jailbreak 3.65 And 3.66
ExtaBit Premium Accounts and Cookies
Steam Wallet Hack - Money Adder & Hack v3
Empires & Allies Hack Cheat Trainer v5.4.1
Eve Onnline 60 Days Time Card Generator v2
Popular
XBOX POINTS GENERATOR - MICROSOFT POINTS GENERATOR v1.2012
11 may 2012 premium uploading accounts 100% working
Free Microsoft Points
Free Microsoft Points - Microsoft Points Generator - Xbox Live Codes 2012
Car Town Free Blue Points Hack
Free CarTown Blue Points Generator and CarTown Templates
Better way to get content via jQuery $.get()
Free Microsoft Points
Simple Days Purger
Sharecash Downloader Bypass Surveys New 05/2012
Pastable version of
DRYing/shortening form processing
<pre class='prettyprint' language='ruby'>def self.filter_price(direction, price) return nil if direction.blank? or price.blank? return {:error => 'Invalid argument: direction'} unless ['at most', 'at least'].include? direction return {:error => 'Invalid argument: price'} unless price.to_s =~ /\A[0-9]+\Z/ case direction when 'at most' then return {:sql => 'price <= :price', :price => price.to_s} when 'at least' then return {:sql => 'price >= :price', :price => price.to_s} else raise 'Unknown error' end end def self.filter_bedrooms(bedrooms) return nil if bedrooms.blank? return {:error => 'Invalid argument: bedrooms'} unless bedrooms.to_s =~ /\A[0-9]+\Z/ {:sql => 'bedrooms = :bedrooms', :bedrooms => bedrooms.to_s} end # # ...Snip. There are several more methods like the ones above... # def self.filtered_properties(params) conditions_string = '' conditions_hash = {} # Stores the conditions for filtering out properties. condition_errors = [] # Stores errors related to a filter request. # Process the "price" and "price_direction" params. filtered = filter_price params[:price_direction], params[:price] if filtered.blank? # Do nothing. elsif filtered[:error] condition_errors << 'Please provide a number for the price. Eg: 1500' else conditions_string << filtered[:sql] conditions_hash[:price] = filtered[:price] end # Process the "bedrooms" param. filtered = filter_bedrooms params[:bedrooms] if filtered.blank? # Do nothing. elsif filtered[:error] condition_errors << 'Please provide a number for the bedrooms. Eg: 2' else conditions_string << ' AND ' << filtered[:sql] conditions_hash[:bedrooms] = filtered[:bedrooms] end # Process the "bathrooms" param. filtered = filter_bathrooms params[:bathrooms] if filtered.blank? # Do nothing. puts '> bathrooms is blank' elsif filtered[:error] condition_errors << 'Please provide a number for the bathrooms. Eg: 2' else conditions_string << ' AND ' << filtered[:sql] conditions_hash[:bathrooms] = filtered[:bathrooms] end # ...snip... return condition_errors unless condition_errors.empty? self.find :all, :conditions => [conditions_string, conditions_hash] end # End #filtered_properties</pre> <a href="http://www.refactormycode.com/codes/558-drying-shortening-form-processing" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>