Skip to content

Commit

Permalink
Updated finders to support passing a conditions array which combined …
Browse files Browse the repository at this point in the history
…a typical SQL-like conditions string and a finders Hash.

git-svn-id: svn+ssh://rubyforge.org/var/svn/arext/trunk@12 5128a5ed-121c-0410-8d5c-98af306bc9be
  • Loading branch information
zachdennis committed Oct 2, 2006
1 parent 4b9c6a6 commit 2159f4c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions base-extensions/lib/active_record_base/finders.rb
Expand Up @@ -43,8 +43,16 @@ def attribute_condition( argument, negate=false )
alias :sanitize_sql_orig :sanitize_sql
def sanitize_sql( arg )
arg = sanitize_sql_from_hash( arg ) if arg.is_a?( Hash )
arg = sanitize_sql_from_array_and_hash( arg ) if arg.size == 2 and arg.first.is_a?( String ) and arg.last.is_a?( Hash )
sanitize_sql_orig( arg )
end

def sanitize_sql_from_array_and_hash( arr )
arr2 = sanitize_sql_from_hash( arr.last )
conditions = [ arr.first << " AND (#{arr2.first})" ]
conditions.push( *arr2[1..-1] )
conditions
end

# TODO Refactor sanitize_sql_from_hash and break up into smaller more manageable methods
def sanitize_sql_from_hash( hsh ) # :nodoc:
Expand Down
13 changes: 13 additions & 0 deletions base-extensions/tests/test_active_record_base_finders.rb
Expand Up @@ -111,4 +111,17 @@ def test_find_not_matching_regex
developers = Developer.find( :all, :conditions=>{ :name_not=>/9999/ } )
assert_equal( Developer.count, developers.size )
end

def test_find_with_string_and_hash
developers = Developer.find( :all,
:conditions=>[ "name = 'Zach Dennis'", { :id=>1 } ] )
assert_equal( 1, developers.size )
end

def test_find_with_string_and_hash_where_none_match
developers = Developer.find( :all,
:conditions=>[ "id = 1", { :id=>2 } ] )
assert_equal( 0, developers.size )
end

end

0 comments on commit 2159f4c

Please sign in to comment.