require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe DattsRight, ".where_dynamic_attribute" do before do reset_database @page = Page.create end it "should be aliased by where_datt" do @page.add_dynamic_attribute(:price, "float") @page.write_dynamic_attribute :price, 200.0 Page.where_dynamic_attribute(:price => 200.0).should == Page.where_datt(:price => 200.0) end it "should be aliased by where_datts" do @page.add_dynamic_attribute(:price, "float") @page.write_dynamic_attribute :price, 200.0 Page.where_dynamic_attribute(:price => 200.0).should == Page.where_datts(:price => 200.0) end it "should be aliased by where_dynamic_attributes" do @page.add_dynamic_attribute(:price, "float") @page.write_dynamic_attribute :price, 200.0 Page.where_dynamic_attribute(:price => 200.0).should == Page.where_dynamic_attributes(:price => 200.0) end it "should automatically join in the dynamic_attributes table" do @page.add_dynamic_attribute(:price, "float") @page.write_dynamic_attribute :price, 200.0 @page.add_dynamic_attribute(:farce, "boolean") @page.write_dynamic_attribute :farce, true @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.add_dynamic_attribute(:farce, "boolean") @page_2.write_dynamic_attribute :farce, true @page_2.write_dynamic_attribute :price, 1 @page_2.save @result = Page.where_dynamic_attribute(:price => 200.0, :farce => true) @result.should include(@page) @result.should_not include(@page_2) end it "should allow chaining with where scopes" do @page.add_dynamic_attribute(:price, "integer") @page.name = "aardvark" @page.write_dynamic_attribute :price, 200 @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 200 @page_2.save @results = Page.where_dynamic_attribute(:price => 200).where(:name => "aardvark") @results.should include(@page) @results.should_not include(@page_2) end it "should return an empty array if we're looking for a field that doesn't exist" do Page.where_dynamic_attribute(:bogus_field => "none").should be_empty end end