diff options
author | Jacques Crocker <railsjedi@gmail.com> | 2010-09-18 20:21:03 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-09-25 11:15:40 +0200 |
commit | ee5ef67c443407f616feef3a8cade8ba3a9d6ef0 (patch) | |
tree | 5a3adcaa9837d2638d88bd9ebbe915c98e15e2bf /activeresource/lib | |
parent | 1b7d30bfc925562cffff10db2a0388770e9bc06c (diff) | |
download | rails-ee5ef67c443407f616feef3a8cade8ba3a9d6ef0.tar.gz rails-ee5ef67c443407f616feef3a8cade8ba3a9d6ef0.tar.bz2 rails-ee5ef67c443407f616feef3a8cade8ba3a9d6ef0.zip |
Allow ActiveResource to work with non-generated ids [#5660 state:resolved]
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 <jose.valim@gmail.com>
Diffstat (limited to 'activeresource/lib')
-rw-r--r-- | activeresource/lib/active_resource/base.rb | 10 |
1 files changed, 6 insertions, 4 deletions
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 <tt>\id</tt> 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 |