Skip to content
This repository
Browse code

Use the ActiveRecord object to typecast values extracted from a condi…

…tion. Solves problems with differing cache keys (published/0 vs. published/false)

Signed-off-by: Nick Kallen <nick@pivotallabs.com>
  • Loading branch information...
commit fc4fb74a50eab3f1682b54b0ab07d314db9fd4d6 1 parent 02aecfe
authored December 23, 2008 Nick Kallen committed January 08, 2009
3  db/schema.rb
... ...
@@ -1,7 +1,8 @@
1  
-ActiveRecord::Schema.define(:version => 1) do
  1
+ActiveRecord::Schema.define(:version => 2) do
2 2
   create_table "stories", :force => true do |t|
3 3
     t.string "title", "subtitle"
4 4
     t.string  "type"
  5
+    t.boolean "published"
5 6
   end
6 7
 
7 8
   create_table "characters", :force => true do |t|
4  lib/cash/query/abstract.rb
... ...
@@ -1,7 +1,7 @@
1 1
 module Cash
2 2
   module Query
3 3
     class Abstract
4  
-      delegate :with_exclusive_scope, :get, :table_name, :indices, :find_from_ids_without_cache, :cache_key, :to => :@active_record
  4
+      delegate :with_exclusive_scope, :get, :table_name, :indices, :find_from_ids_without_cache, :cache_key, :columns_hash, :to => :@active_record
5 5
 
6 6
       def self.perform(*args)
7 7
         new(*args).perform
@@ -102,7 +102,7 @@ def parse_indices_from_condition(conditions = '', *values)
102 102
         conditions.split(AND).inject([]) do |indices, condition|
103 103
           matched, table_name, column_name, sql_value = *(KEY_EQ_VALUE.match(condition))
104 104
           if matched
105  
-            value = sql_value == '?' ? values.shift : sql_value
  105
+            value = sql_value == '?' ? values.shift : columns_hash[column_name].type_cast(sql_value)
106 106
             indices << [column_name, value]
107 107
           else
108 108
             return nil
9  spec/cash/active_record_spec.rb
@@ -123,6 +123,15 @@ module Cash
123 123
             Story.find(:first, :conditions => []).should == story
124 124
           end
125 125
         end
  126
+        
  127
+        describe "#find(:first, :conditions => '...')" do
  128
+          it "uses the active record instance to typecast values extracted from the conditions" do
  129
+            story1 = Story.create! :title => 'a story', :published => true
  130
+            story2 = Story.create! :title => 'another story', :published => false
  131
+            Story.get('published/false').should == [story2.id]
  132
+            Story.find(:first, :conditions => 'published = 0').should == story2
  133
+          end
  134
+        end
126 135
       end
127 136
 
128 137
       describe '#find_by_attr' do
1  spec/spec_helper.rb
@@ -37,6 +37,7 @@
37 37
     Story.class_eval do
38 38
       index :title
39 39
       index [:id, :title]
  40
+      index :published
40 41
     end
41 42
 
42 43
     Epic = Class.new(Story)

0 notes on commit fc4fb74

Please sign in to comment.
Something went wrong with that request. Please try again.