Skip to content
This repository

Delegate primary_key to superclass when necessary, otherwise default #53

Merged
merged 1 commit into from about 1 year ago

2 participants

Denzel Morris Guillermo Iguaran
Denzel Morris

Current Behavior

The primary_key attribute does not delegate to the superclass when read; causing the following to happen:

class BaseResource < ActiveResource::Base
  self.primary_key = :custom_key
end

class DerivedResource < BaseResource
end

BaseResource.primary_key    => :custom_key
DerivedResource.primary_key => 'id'

New Behavior

Simply delegate the primary_key attribute to the superclass if it isn't set. Thus becoming:

BaseResource.primary_key    => :custom_key
DerivedResource.primary_key => :custom_key
Guillermo Iguaran guilleiguaran merged commit d5be36d into from
Guillermo Iguaran guilleiguaran closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 18, 2013
Denzel Morris Delegate primary_key to superclass when necessary, otherwise default;…
… added testing
265007e
This page is out of date. Refresh to see the latest.
10  lib/active_resource/base.rb
@@ -625,7 +625,15 @@ def collection_name
625 625
       attr_writer :primary_key
626 626
 
627 627
       def primary_key
628  
-        @primary_key ||= 'id'
  628
+        if defined?(@primary_key)
  629
+          @primary_key
  630
+        elsif superclass != Object && superclass.primary_key
  631
+          primary_key = superclass.primary_key
  632
+          return primary_key if primary_key.is_a?(Symbol)
  633
+          primary_key.dup.freeze
  634
+        else
  635
+          'id'
  636
+        end
629 637
       end
630 638
 
631 639
       # Gets the \prefix for a resource's nested URL (e.g., <tt>prefix/collectionname/1.json</tt>)
37  test/cases/base_test.rb
@@ -353,6 +353,43 @@ def test_timeout_reader_uses_superclass_timeout_until_written
353 353
     assert_equal fruit.timeout, apple.timeout, 'subclass did not adopt changes from parent class'
354 354
   end
355 355
 
  356
+  def test_primary_key_uses_superclass_primary_key_until_written
  357
+    # Superclass is Object so defaults to 'id'
  358
+    assert_equal 'id', ActiveResource::Base.primary_key
  359
+    assert_equal 'id', Class.new(ActiveResource::Base).primary_key
  360
+    Person.primary_key = :first
  361
+
  362
+    # Subclass uses superclass primary_key
  363
+    actor = Class.new(Person)
  364
+    assert_equal Person.primary_key, actor.primary_key
  365
+
  366
+    # Changing subclass primary_key doesn't change superclass primary_key
  367
+    actor.primary_key = :second
  368
+    assert_not_equal Person.primary_key, actor.primary_key
  369
+
  370
+    # Changing superclass primary_key doesn't overwrite subclass primary_key
  371
+    Person.primary_key = :third
  372
+    assert_not_equal Person.primary_key, actor.primary_key
  373
+
  374
+    # Changing superclass primary_key after subclassing changes subclass primary_key
  375
+    jester = Class.new(actor)
  376
+    actor.primary_key = :fourth
  377
+    assert_equal actor.primary_key, jester.primary_key
  378
+
  379
+    # Subclass primary_keys are always equal to superclass primary_key when not overridden
  380
+    fruit = Class.new(ActiveResource::Base)
  381
+    apple = Class.new(fruit)
  382
+
  383
+    fruit.primary_key = :fifth
  384
+    assert_equal fruit.primary_key, apple.primary_key, 'subclass did not adopt changes from parent class'
  385
+
  386
+    fruit.primary_key = :sixth
  387
+    assert_equal fruit.primary_key, apple.primary_key, 'subclass did not adopt changes from parent class'
  388
+
  389
+    # Reset the primary key for subsequent tests
  390
+    Person.primary_key = 'id'
  391
+  end
  392
+
356 393
   def test_ssl_options_reader_uses_superclass_ssl_options_until_written
357 394
     # Superclass is Object so returns nil.
358 395
     assert_nil ActiveResource::Base.ssl_options
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.