Permalink
Browse files

Update resource attribute has when saving

  • Loading branch information...
vesan committed Jul 19, 2012
1 parent 700eb30 commit 99058f75f1bcc43de5a040e1689063b019a0c20e
Showing with 22 additions and 5 deletions.
  1. +2 −0 lib/kippt/collection_resource.rb
  2. +13 −5 lib/kippt/resource.rb
  3. +7 −0 spec/spec_helper.rb
@@ -27,9 +27,11 @@ def save_resource(object)
end
save_response = {:success => response.success?}
+ save_response[:resource] = response.body
if response.body["message"]
save_response[:error_message] = response.body["message"]
end
+
save_response
end
View
@@ -4,23 +4,24 @@ def self.included(base)
extend Forwardable
attr_reader :attributes, :errors
- def_delegators "self.class", :writable_attribute_names
+ def_delegators "self.class", :writable_attribute_names, :attribute_names
end
base.extend(ClassMethods)
end
module ClassMethods
- attr_reader :writable_attribute_names
+ attr_reader :writable_attribute_names, :attribute_names
def attributes(*attribs)
- def_delegators :attributes, *attribs
+ @attribute_names = attribs.map {|attrib| attrib.to_sym }
+ def_delegators :attributes, *@attribute_names
end
def writable_attributes(*attribs)
- @writable_attribute_names = attribs
+ @writable_attribute_names = attribs.map {|attrib| attrib.to_sym }
@writable_attribute_names.freeze
- def_delegators :attributes, *(attribs.map {|attrib| attrib.to_s + "=" })
+ def_delegators :attributes, *(@writable_attribute_names.map {|attrib| attrib.to_s + "=" })
end
end
@@ -49,6 +50,13 @@ def save
response = @collection_resource.save_resource(self)
if response[:error_message]
errors << response[:error_message]
+ else
+ if response[:resource]
+ updated_attributes = response[:resource].select do |key, _|
+ attribute_names.include?(key.to_sym)
+ end
+ @attributes = OpenStruct.new(updated_attributes)
+ end
end
response[:success]
end
View
@@ -239,6 +239,13 @@ def fixture(file)
{:success => true})
subject.save.should be_true
end
+
+ it "sets the updated attributes received from the server" do
+ collection_resource.stub(:save_resource).and_return(
+ {:success => true, :resource => {id: 9999}})
+ subject.save
+ subject.id.should eq 9999
+ end
end
context "with invalid parameters" do

0 comments on commit 99058f7

Please sign in to comment.