Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
Patrick Lenz authored Nick Kallen committed
View
3  db/schema.rb
@@ -1,7 +1,8 @@
-ActiveRecord::Schema.define(:version => 1) do
+ActiveRecord::Schema.define(:version => 2) do
create_table "stories", :force => true do |t|
t.string "title", "subtitle"
t.string "type"
+ t.boolean "published"
end
create_table "characters", :force => true do |t|
View
4 lib/cash/query/abstract.rb
@@ -1,7 +1,7 @@
module Cash
module Query
class Abstract
- delegate :with_exclusive_scope, :get, :table_name, :indices, :find_from_ids_without_cache, :cache_key, :to => :@active_record
+ delegate :with_exclusive_scope, :get, :table_name, :indices, :find_from_ids_without_cache, :cache_key, :columns_hash, :to => :@active_record
def self.perform(*args)
new(*args).perform
@@ -102,7 +102,7 @@ def parse_indices_from_condition(conditions = '', *values)
conditions.split(AND).inject([]) do |indices, condition|
matched, table_name, column_name, sql_value = *(KEY_EQ_VALUE.match(condition))
if matched
- value = sql_value == '?' ? values.shift : sql_value
+ value = sql_value == '?' ? values.shift : columns_hash[column_name].type_cast(sql_value)
indices << [column_name, value]
else
return nil
View
9 spec/cash/active_record_spec.rb
@@ -123,6 +123,15 @@ module Cash
Story.find(:first, :conditions => []).should == story
end
end
+
+ describe "#find(:first, :conditions => '...')" do
+ it "uses the active record instance to typecast values extracted from the conditions" do
+ story1 = Story.create! :title => 'a story', :published => true
+ story2 = Story.create! :title => 'another story', :published => false
+ Story.get('published/false').should == [story2.id]
+ Story.find(:first, :conditions => 'published = 0').should == story2
+ end
+ end
end
describe '#find_by_attr' do
View
1  spec/spec_helper.rb
@@ -37,6 +37,7 @@
Story.class_eval do
index :title
index [:id, :title]
+ index :published
end
Epic = Class.new(Story)
Please sign in to comment.
Something went wrong with that request. Please try again.