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 --- activeresource/lib/active_resource/base.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'activeresource/lib') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 3016f62b80..915021a7b3 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -886,7 +886,7 @@ module ActiveResource end def instantiate_record(record, prefix_options = {}) - new(record).tap do |resource| + new(record, true).tap do |resource| resource.prefix_options = prefix_options end end @@ -959,9 +959,10 @@ module ActiveResource # # my_other_course = Course.new(:name => "Philosophy: Reason and Being", :lecturer => "Ralph Cling") # my_other_course.save - def initialize(attributes = {}) + def initialize(attributes = {}, persisted = false) @attributes = {}.with_indifferent_access @prefix_options = {} + @persisted = persisted load(attributes) end @@ -1011,7 +1012,7 @@ module ActiveResource # is_new.new? # => false # def new? - id.nil? + !persisted? end alias :new_record? :new? @@ -1028,7 +1029,7 @@ module ActiveResource # not_persisted.persisted? # => true # def persisted? - !new? + @persisted end # Gets the \id attribute of the resource. @@ -1317,6 +1318,7 @@ module ActiveResource def load_attributes_from_response(response) if !response['Content-Length'].blank? && response['Content-Length'] != "0" && !response.body.nil? && response.body.strip.size > 0 load(self.class.format.decode(response.body)) + @persisted = true end end -- cgit v1.2.3