require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') describe DattsRight, "on dynamic find_by_dynamic_attribute methods" do before do reset_database @page = Page.create end it "should be aliased by find_by_datt" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 400 @page.save Page.find_by_datt_price(400).should == Page.find_by_dynamic_attribute_price(400) end it "should be able to return single records" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 400 @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 500 @page_2.save @results = Page.find_by_dynamic_attribute_price(400) @results.should == @page @results.should_not == @page_2 end it "should be able to return all matching records" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 400 @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 500 @page_2.save @page_3 = Page.create @page_3.add_dynamic_attribute(:price, "integer") @page_3.write_dynamic_attribute :price, 400 @page_3.save @results = Page.find_all_by_dynamic_attribute_price(400) @results.should include(@page) @results.should_not include(@page_2) @results.should include(@page_3) end it "should still allow raising of NoMethodError if the original attribute does not exist" do lambda { Page.find_by_faker("hi") }.should raise_error(NoMethodError) end it "should be able to return the last record" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 400 @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 400 @page_2.save Page.find_last_by_dynamic_attribute_price(400) == @page_2 end it "should allow normal attributes to work" do @page.name = "Roland" @page.save Page.find_by_name("Roland").should == @page end it "should allow chaining" do @page.add_dynamic_attribute(:price, "integer") @page.name = "fixed" @page.write_dynamic_attribute :price, 400 @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 500 @page_2.save @pages = Page.where(:name => "fixed").find_by_dynamic_attribute_price(400) @pages.should == @page @pages.should_not == @page_2 end end