Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposed solution to issue #404 #411

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/her/model/attributes.rb
Expand Up @@ -36,14 +36,23 @@ def self.initialize_collection(klass, parsed_data={})
if item_data.kind_of?(klass)
resource = item_data
else
resource = klass.new(klass.parse(item_data))
resource.run_callbacks :find
resource = initialize_resource(klass, item_data)
end
resource
end
Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
end

# Initialize a resource
#
# @private
def self.initialize_resource(klass, item_data)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should come in handy elsewhere 👍

klass.new(klass.parse(item_data)).tap do |resource|
resource.instance_variable_set(:@changed_attributes, {})
resource.run_callbacks :find
end
end

# Use setter methods of model for each key / value pair in params
# Return key / value pairs for which no setter method was defined on the model
#
Expand Down
9 changes: 9 additions & 0 deletions spec/model/dirty_spec.rb
Expand Up @@ -8,6 +8,7 @@
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users") { |env| [200, {}, [{ :id => 1, :fullname => "Lindsay Fünke" }].to_json] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add multiple users here for consistency?

stub.get("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Lindsay Fünke" }.to_json] }
stub.get("/users/2") { |env| [200, {}, { :id => 2, :fullname => "Maeby Fünke" }.to_json] }
stub.get("/users/3") { |env| [200, {}, { :user_id => 3, :fullname => "Maeby Fünke" }.to_json] }
Expand All @@ -25,6 +26,14 @@
end
end

context "for existing resource in a collection" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about?

context "for existing collections" do
  let(:users) { Foo::User.all }
  it "has no changes" do
    expect(users).to_not include(be_changed)
  end
end

let(:user) { Foo::User.all[0] }
it "has no changes" do
user.changes.should be_empty
user.should_not be_changed
end
end

context "for existing resource" do
let(:user) { Foo::User.find(1) }
it "has no changes" do
Expand Down