From ee5ef67c443407f616feef3a8cade8ba3a9d6ef0 Mon Sep 17 00:00:00 2001 From: Jacques Crocker Date: Sat, 18 Sep 2010 20:21:03 -0700 Subject: Allow ActiveResource to work with non-generated ids [#5660 state:resolved] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit updates new? so that it knows whether or not the record was actually new or not, and doesn't rely solely on the presence of id. This enables the ability to set a custom primary_key that is not autogenerated by the server. Signed-off-by: José Valim --- .../test/cases/base/custom_methods_test.rb | 2 +- activeresource/test/cases/base_test.rb | 49 ++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) (limited to 'activeresource/test/cases') diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb index 459d33c24f..0fbf94bc0e 100644 --- a/activeresource/test/cases/base/custom_methods_test.rb +++ b/activeresource/test/cases/base/custom_methods_test.rb @@ -91,7 +91,7 @@ class CustomMethodsTest < Test::Unit::TestCase 201, {'Location' => '/people/1/addresses/2.xml'}), addy.post(:link) - matz = Person.new(:id => 1, :name => 'Matz') + matz = Person.find(1) assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register) end diff --git a/activeresource/test/cases/base_test.rb b/activeresource/test/cases/base_test.rb index b5914683e9..b63a218663 100644 --- a/activeresource/test/cases/base_test.rb +++ b/activeresource/test/cases/base_test.rb @@ -5,6 +5,8 @@ require "fixtures/street_address" require "fixtures/sound" require "fixtures/beast" require "fixtures/proxy" +require "fixtures/address" +require "fixtures/subscription_plan" require 'active_support/json' require 'active_support/ordered_hash' require 'active_support/core_ext/hash/conversions' @@ -1034,4 +1036,51 @@ class BaseTest < Test::Unit::TestCase end end end + + def test_with_custom_formatter + @addresses = [{:id => "1", :street => "1 Infinite Loop", :city => "Cupertino", :state => "CA"}].to_xml(:root => 'addresses') + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/addresses.xml", {}, @addresses, 200 + end + + # late bind the site + AddressResource.site = "http://localhost" + addresses = AddressResource.find(:all) + + assert_equal "Cupertino, CA", addresses.first.city_state + end + + def test_create_with_custom_primary_key + silver_plan = {:code => "silver", :price => 5.00}.to_xml(:root => "plan") + + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/plans.xml", {}, silver_plan, 201, 'Location' => '/plans/silver.xml' + end + + plan = SubscriptionPlan.new(:code => "silver", :price => 5.00) + assert plan.new? + + plan.save! + assert !plan.new? + end + + def test_update_with_custom_primary_key + silver_plan = {:code => "silver", :price => 5.00}.to_xml(:root => "plan") + silver_plan_updated = {:code => "silver", :price => 10.00}.to_xml(:root => "plan") + + ActiveResource::HttpMock.respond_to do |mock| + mock.get "/plans/silver.xml", {}, silver_plan + mock.put "/plans/silver.xml", {}, silver_plan_updated, 201, 'Location' => '/plans/silver.xml' + end + + plan = SubscriptionPlan.find("silver") + assert !plan.new? + assert 5.00, plan.price + + # update price + plan.price = 10.00 + plan.save! + assert 10.00, plan.price + end end -- cgit v1.2.3