Skip to content

Commit

Permalink
allow model to have attribute that is the same as its name
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Tribone committed Sep 11, 2017
1 parent ac830c4 commit 3d2636b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/pkg
/tmp
/coverage
.bundle
/vendor
4 changes: 4 additions & 0 deletions .pryrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
4 changes: 4 additions & 0 deletions her.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ Gem::Specification.new do |s|
s.add_development_dependency "rake", "~> 10.0"
s.add_development_dependency "rspec", "~> 3.5"
s.add_development_dependency "json", "~> 1.8"
s.add_development_dependency "pry", "~> 0.10"
s.add_development_dependency "pry-byebug", "~> 3.4"
s.add_development_dependency "pry-remote", "~> 0.1"
s.add_development_dependency "pry-stack_explorer", "~> 0.4"

s.add_runtime_dependency "activemodel", ">= 3.0.0", "<= 6.0.0"
s.add_runtime_dependency "activesupport", ">= 3.0.0", "<= 6.0.0"
Expand Down
3 changes: 2 additions & 1 deletion lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,8 @@ def root_element(value = nil)

# @private
def root_element_included?(data)
data.keys.to_s.include? @_her_root_element.to_s
data.keys.include?(root_element) &&
(data[root_element].is_a?(Hash) || data[root_element].is_a?(Array))
end

# @private
Expand Down
22 changes: 22 additions & 0 deletions spec/model/parse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,28 @@
require File.join(File.dirname(__FILE__), "../spec_helper.rb")

describe Her::Model::Parse do
context "when no root element not included" do
before do
Her::API.setup url: "https://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
builder.use Faraday::Request::UrlEncoded
end

Her::API.default_api.connection.adapter :test do |stub|
stub.post("/users") { |env| [200, {}, { user: "foobar", id: 1, fullname: params(env)[:fullname] }.to_json] }
end

spawn_model "Foo::User"
end


it "ignores root keys if not array or object" do
user = Foo::User.create(fullname: "barfoo")
expect(user.fullname).to eq "barfoo"
expect(user.user).to eq "foobar"
end
end

context "when include_root_in_json is set" do
before do
Her::API.setup url: "https://api.example.com" do |builder|
Expand Down

0 comments on commit 3d2636b

Please sign in to comment.