require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe DattsRight do before do reset_database @page = Page.create end it "should not allow arbitrary creation of attributes" do lambda { @page.some_field = "woot" }.should raise_error(NoMethodError) end it "should allow saving of integers" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 300 @page.save @page = Page.last @page.read_dynamic_attribute(:price).should == 300 end it "should allow saving of floats" do @page.add_dynamic_attribute(:price, "float") @page.write_dynamic_attribute :price, 300.0 @page.save @page = Page.last @page.read_dynamic_attribute(:price).should == 300.0 end it "should allow saving of true" do @page.add_dynamic_attribute(:real, "boolean") @page.write_dynamic_attribute :real, true @page.save @page = Page.last @page.read_dynamic_attribute(:real).should be_true end it "should allow saving of false" do @page.add_dynamic_attribute(:real, "boolean") @page.write_dynamic_attribute :real, false @page.save @page = Page.last @page.read_dynamic_attribute(:real).should be_false end it "should allow saving of strings" do @page.add_dynamic_attribute(:real, "string") @page.write_dynamic_attribute :real, "its real alright" @page.save @page = Page.last @page.read_dynamic_attribute(:real).should == "its real alright" end it "should allow saving of text" do @page.add_dynamic_attribute(:real, "text") @page.write_dynamic_attribute :real, "t"*256 @page.save @page = Page.last @page.read_dynamic_attribute(:real).should == "t"*256 end it "should cache the attribute" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 200 @page.read_dynamic_attribute(:price).should == 200 end it "should not save the attribute if save on the attributable object isn't called" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 200 @page = Page.last @page.read_dynamic_attribute(:price).should be_nil end it "should allow overwriting of values" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 200 @page.save @page = Page.last @page.write_dynamic_attribute :price, 300 @page.save @page = Page.last @page.read_dynamic_attribute(:price).should == 300 end it "should allow nullifying of attributes, but keeping the fields there" do @page.add_dynamic_attribute(:farce, "string") @page.write_dynamic_attribute :farce, "Nothing here my friend" @page.save @page.write_dynamic_attribute :farce, nil @page.dynamic_attribute?(:farce).should be_true end it "should allow multiple attributes" do @page.add_dynamic_attribute(:price, "integer") @page.write_dynamic_attribute :price, 400 @page.add_dynamic_attribute(:farce, "string") @page.write_dynamic_attribute :farce, "hitt" @page.save @page_2 = Page.create @page_2.add_dynamic_attribute(:price, "integer") @page_2.write_dynamic_attribute :price, 400 @page_2.add_dynamic_attribute(:farce, "string") @page_2.write_dynamic_attribute :farce, "hi" @page_2.save @pages = Page.find_by_dynamic_attribute_price_and_farce(400, "hi") @pages.should == @page_2 @pages.should_not == @page end describe "when the value being assigned is not of the same type (with exceptions)" do before do @page.add_dynamic_attribute(:price, "integer") end it "should not save the value" do @page.write_dynamic_attribute :price, 200 @page.save @page.write_dynamic_attribute :price, "hi there" @page.save Page.last.read_dynamic_attribute(:price).should == 200 end end it "should allow mass assignment" do @page.add_dynamic_attribute(:price, "integer") @page.add_dynamic_attribute(:farce, "string") @page.update_dynamic_attributes(:price => 200, :farce => "hi") @page.read_dynamic_attribute(:price).should == 200 @page.read_dynamic_attribute(:farce).should == "hi" @page.update_dynamic_attributes!(:price => 300, :farce => "not farce!") @page.read_dynamic_attribute(:price).should == 300 @page.read_dynamic_attribute(:farce).should == "not farce!" end it "should allow use of update_dynamic_attribute" do @page.add_dynamic_attribute(:price, "integer") @page.update_dynamic_attribute(:price, 200) @page.read_dynamic_attribute(:price).should == 200 end describe "when loading from database" do it "should load the dynamic attributes for use" do @page.add_datt(:body, "text") @page.write_datt(:body, "t"*300) @page.save @page = Page.last @page.read_datt(:body).should == "t"*300 @page.dynamic_attribute_details(:body).value.should == @page.read_datt(:body) end end it "should be able to write short strings into text variables" do @page.add_datt(:body, "text") @page.write_datt(:body, "short") @page.save @page.read_datt(:body).should == "short" end it "should write like a normal attribute" do @page.add_datt(:body, "text") @page.body = "dude" @page.save @page.read_datt(:body).should == "dude" end it "should read like a normal attribute" do @page.add_datt(:body, "text") @page.body = "dude" @page.save @page.body.should == "dude" end it "should still allow virtual attributes to work (for mass assignment)" do lambda {Category.create.update_attributes(:definition_attributes => {"attr_key" => "some_attr", "object_type" => "string"}, :name => "hello")}.should_not raise_error(ActiveRecord::UnknownAttributeError) end #it "should validate reflection of inheritance" do #PageWithoutReflection.new.errors[:base].should include("Page inherits its definition from category_without_reflection but that does not define Page.") #end end