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

Add response errors to ActiveModel using ActiveModel::Errors #178

Closed
wants to merge 2 commits 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/her/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ResourceInvalid < StandardError
attr_reader :resource
def initialize(resource)
@resource = resource
errors = @resource.response_errors.join(", ")
errors = @resource.errors.full_messages.join(", ")
super("Remote validation failed: #{errors}")
end
end
Expand Down
9 changes: 9 additions & 0 deletions lib/her/model/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def save
assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
@metadata = parsed_data[:metadata]
@response_errors = parsed_data[:errors]
add_errors_to_base if @response_errors.any?

return false if !response.success? || @response_errors.any?
self.changed_attributes.clear if self.changed_attributes.present?
Expand Down Expand Up @@ -81,6 +82,14 @@ def destroy
self
end

private
# @private
def add_errors_to_base
@response_errors.each do |attr, messages|
[*messages].map { |msg| self.errors.add(attr, msg) }
end
end

module ClassMethods
# Create a new chainable scope
#
Expand Down
2 changes: 1 addition & 1 deletion spec/model/dirty_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
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.put("/users/1") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
stub.put("/users/2") { |env| [400, {}, { :errors => ["Email cannot be blank"] }.to_json] }
stub.put("/users/2") { |env| [400, {}, { :errors => { :email => ["cannot be blank"] } }.to_json] }
stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => "Tobias Fünke" }.to_json] }
end
end
Expand Down
13 changes: 9 additions & 4 deletions spec/model/orm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.get("/users") { |env| [200, {}, { :data => [{ :id => 1, :name => "Tobias Fünke" }, { :id => 2, :name => "Lindsay Fünke" }], :metadata => { :total_pages => 10, :next_page => 2 }, :errors => ["Oh", "My", "God"] }.to_json] }
stub.post("/users") { |env| [200, {}, { :data => { :name => "George Michael Bluth" }, :metadata => { :foo => "bar" }, :errors => ["Yes", "Sir"] }.to_json] }
stub.post("/users") { |env| [200, {}, { :data => { :name => "George Michael Bluth" }, :metadata => { :foo => "bar" }, :errors => { :title => ["should not be blank"] } }.to_json] }
end
end

Expand All @@ -98,7 +98,12 @@

it "handles error data on a resource" do
@user = User.create(:name => "George Michael Bluth")
@user.response_errors.should == ["Yes", "Sir"]
@user.response_errors.should == { :title => ["should not be blank"] }
end

it "adds resource errors to base" do
@user = User.create(:name => "George Michael Bluth")
@user.errors.full_messages.should == ["Title should not be blank"]
end
end

Expand Down Expand Up @@ -252,7 +257,7 @@ def friends
builder.use Faraday::Request::UrlEncoded
builder.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { :id => 1, :fullname => Faraday::Utils.parse_query(env[:body])['fullname'], :email => Faraday::Utils.parse_query(env[:body])['email'] }.to_json] }
stub.post("/companies") { |env| [200, {}, { :errors => ["name is required"] }.to_json] }
stub.post("/companies") { |env| [200, {}, { :errors =>{ :name => ["is required"] } }.to_json] }
end
end

Expand Down Expand Up @@ -286,7 +291,7 @@ def friends

it "raises ResourceInvalid when #save! gets errors" do
@company = Foo::Company.new
expect { @company.save! }.to raise_error Her::Errors::ResourceInvalid, "Remote validation failed: name is required"
expect { @company.save! }.to raise_error Her::Errors::ResourceInvalid, "Remote validation failed: Name is required"
end

it "don't overwrite data if response is empty" do
Expand Down