her 0.4 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,98 @@
1
+ # encoding: utf-8
2
+ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
3
+
4
+ describe Her::Model::NestedAttributes do
5
+ context "with a belongs_to relation" do
6
+ before do
7
+ Her::API.setup :url => "https://api.example.com" do |builder|
8
+ builder.use Her::Middleware::FirstLevelParseJSON
9
+ builder.use Faraday::Request::UrlEncoded
10
+ end
11
+
12
+ spawn_model "Foo::User" do
13
+ belongs_to :company, :path => "/organizations/:id", :foreign_key => :organization_id
14
+ accepts_nested_attributes_for :company
15
+ end
16
+
17
+ spawn_model "Foo::Company"
18
+
19
+ @user_with_data_through_nested_attributes = Foo::User.new :name => "Test", :company_attributes => { :name => "Example Company" }
20
+ end
21
+
22
+ context "when child does not yet exist" do
23
+ it "creates an instance of the associated class" do
24
+ @user_with_data_through_nested_attributes.company.should be_a(Foo::Company)
25
+ @user_with_data_through_nested_attributes.company.name.should == "Example Company"
26
+ end
27
+ end
28
+
29
+ context "when child does exist" do
30
+ it "updates the attributes of the associated object" do
31
+ @user_with_data_through_nested_attributes.company_attributes = { :name => "Fünke's Company" }
32
+ @user_with_data_through_nested_attributes.company.should be_a(Foo::Company)
33
+ @user_with_data_through_nested_attributes.company.name.should == "Fünke's Company"
34
+ end
35
+ end
36
+ end
37
+
38
+ context "with a has_one relation" do
39
+ before do
40
+ Her::API.setup :url => "https://api.example.com" do |builder|
41
+ builder.use Her::Middleware::FirstLevelParseJSON
42
+ builder.use Faraday::Request::UrlEncoded
43
+ end
44
+
45
+ spawn_model "Foo::User" do
46
+ has_one :pet
47
+ accepts_nested_attributes_for :pet
48
+ end
49
+
50
+ spawn_model "Foo::Pet"
51
+
52
+ @user_with_data_through_nested_attributes = Foo::User.new :name => "Test", :pet_attributes => { :name => "Hasi" }
53
+ end
54
+
55
+ context "when child does not yet exist" do
56
+ it "creates an instance of the associated class" do
57
+ @user_with_data_through_nested_attributes.pet.should be_a(Foo::Pet)
58
+ @user_with_data_through_nested_attributes.pet.name.should == "Hasi"
59
+ end
60
+ end
61
+
62
+ context "when child does exist" do
63
+ it "updates the attributes of the associated object" do
64
+ @user_with_data_through_nested_attributes.pet_attributes = { :name => "Rodriguez" }
65
+ @user_with_data_through_nested_attributes.pet.should be_a(Foo::Pet)
66
+ @user_with_data_through_nested_attributes.pet.name.should == "Rodriguez"
67
+ end
68
+ end
69
+ end
70
+
71
+ context "with a has_many relation" do
72
+ before do
73
+ Her::API.setup :url => "https://api.example.com" do |builder|
74
+ builder.use Her::Middleware::FirstLevelParseJSON
75
+ builder.use Faraday::Request::UrlEncoded
76
+ end
77
+
78
+ spawn_model "Foo::User" do
79
+ has_many :pets
80
+ accepts_nested_attributes_for :pets
81
+ end
82
+
83
+ spawn_model "Foo::Pet"
84
+
85
+ @user_with_data_through_nested_attributes = Foo::User.new :name => "Test", :pets_attributes => [{ :name => "Hasi" }, { :name => "Rodriguez" }]
86
+ end
87
+
88
+ context "when children do not yet exist" do
89
+ it "creates an instance of the associated class" do
90
+ @user_with_data_through_nested_attributes.pets.length.should == 2
91
+ @user_with_data_through_nested_attributes.pets[0].should be_a(Foo::Pet)
92
+ @user_with_data_through_nested_attributes.pets[1].should be_a(Foo::Pet)
93
+ @user_with_data_through_nested_attributes.pets[0].name.should == "Hasi"
94
+ @user_with_data_through_nested_attributes.pets[1].name.should == "Rodriguez"
95
+ end
96
+ end
97
+ end
98
+ end
@@ -87,6 +87,9 @@ describe Her::Model::Relationships do
87
87
  stub.get("/users/1/posts") { |env| [200, {}, {:id => 1, :body => 'blogging stuff', :admin_id => 1 }.to_json] }
88
88
  stub.get("/organizations/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company Foo" }.to_json] }
89
89
  stub.get("/organizations/2") { |env| [200, {}, { :id => 2, :name => "Bluth Company" }.to_json] }
90
+ stub.post("/users") { |env| [200, {}, { :id => 5, :name => "Mr. Krabs", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
91
+ stub.put("/users/5") { |env| [200, {}, { :id => 5, :name => "Clancy Brown", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
92
+ stub.delete("/users/5") { |env| [200, {}, { :id => 5, :name => "Clancy Brown", :comments => [{ :id => 99, :body => "Rodríguez, nasibisibusi?", :user_id => 5 }], :role => { :id => 1, :body => "Admin" }, :organization => { :id => 3, :name => "Krusty Krab" }, :organization_id => 3 }.to_json] }
90
93
  end
91
94
  end
92
95
 
@@ -110,6 +113,10 @@ describe Her::Model::Relationships do
110
113
  @user_without_included_data = Foo::User.find(2)
111
114
  end
112
115
 
116
+ let(:user_with_included_data_after_create) { Foo::User.create }
117
+ let(:user_with_included_data_after_save_existing) { Foo::User.save_existing(5, :name => "Clancy Brown") }
118
+ let(:user_with_included_data_after_destroy) { Foo::User.new(:id => 5).destroy }
119
+
113
120
  it "maps an array of included data through has_many" do
114
121
  @user_with_included_data.comments.first.should be_a(Foo::Comment)
115
122
  @user_with_included_data.comments.length.should == 2
@@ -177,6 +184,25 @@ describe Her::Model::Relationships do
177
184
  @user_without_included_data.get_relationship(:unknown_relationship).should be_nil
178
185
  @user_without_included_data.get_relationship(:organization).name.should == "Bluth Company"
179
186
  end
187
+
188
+ [:create, :save_existing, :destroy].each do |type|
189
+ context "after #{type}" do
190
+ let(:subject) { self.send("user_with_included_data_after_#{type}")}
191
+
192
+ it "maps an array of included data through has_many" do
193
+ subject.comments.first.should be_a(Foo::Comment)
194
+ subject.comments.length.should == 1
195
+ subject.comments.first.id.should == 99
196
+ subject.comments.first.body.should == "Rodríguez, nasibisibusi?"
197
+ end
198
+
199
+ it "maps an array of included data through has_one" do
200
+ subject.role.should be_a(Foo::Role)
201
+ subject.role.id.should == 1
202
+ subject.role.body.should == "Admin"
203
+ end
204
+ end
205
+ end
180
206
  end
181
207
 
182
208
  context "handling relationships with details" do
data/spec/model_spec.rb CHANGED
@@ -3,41 +3,29 @@ require 'spec_helper'
3
3
 
4
4
  describe Her::Model do
5
5
  before do
6
- Her::API.setup :url => "https://api.example.com" do |builder|
7
- builder.use Her::Middleware::FirstLevelParseJSON
8
- builder.use Faraday::Request::UrlEncoded
9
- builder.adapter :test do |stub|
6
+ Her::API.setup :url => "https://api.example.com" do |connection|
7
+ connection.use Her::Middleware::FirstLevelParseJSON
8
+ connection.adapter :test do |stub|
10
9
  stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke" }.to_json] }
11
10
  stub.get("/users/1/comments") { |env| [200, {}, [{ :id => 4, :body => "They're having a FIRESALE?" }].to_json] }
12
11
  end
13
12
  end
14
13
 
15
- spawn_model "Foo::User" do
16
- has_many :comments
17
- end
18
-
19
- spawn_model "Foo::Comment"
20
-
21
- @user_without_included_data = Foo::User.find(1)
22
- end
23
-
24
- it "handles has_key? for data" do
25
- @user_without_included_data.should_not have_key(:unknown_method_for_a_user)
26
- @user_without_included_data.should have_key(:name)
27
- end
28
-
29
- it "handles has_key? for relationships" do
30
- @user_without_included_data.should_not have_key(:unknown_method_for_a_user)
31
- @user_without_included_data.should have_key(:comments)
14
+ spawn_model("Foo::User") { has_many :comments }
15
+ spawn_model("Foo::Comment")
32
16
  end
17
+ subject { Foo::User.find(1) }
33
18
 
34
- it "handles [] for data" do
35
- @user_without_included_data[:unknown_method_for_a_user].should be_nil
36
- @user_without_included_data[:name].should == "Tobias Fünke"
19
+ describe :has_key? do
20
+ it { should_not have_key(:unknown_method_for_a_user) }
21
+ it { should_not have_key(:unknown_method_for_a_user) }
22
+ it { should have_key(:name) }
23
+ it { should have_key(:comments) }
37
24
  end
38
25
 
39
- it "handles [] for relationships" do
40
- @user_without_included_data[:unknown_relationship].should be_nil
41
- @user_without_included_data[:comments].first.body.should == "They're having a FIRESALE?"
26
+ describe :[] do
27
+ it { should_not have_key(:unknown_method_for_a_user) }
28
+ specify { subject[:name].should == "Tobias Fünke" }
29
+ specify { subject[:comments].first.body.should == "They're having a FIRESALE?" }
42
30
  end
43
31
  end
data/spec/spec_helper.rb CHANGED
@@ -4,14 +4,14 @@ require "rspec"
4
4
  require "mocha/api"
5
5
  require "her"
6
6
 
7
- RSpec.configure do |c|
8
- c.mock_with :mocha
7
+ RSpec.configure do |config|
8
+ config.mock_with :mocha
9
9
 
10
- c.before :each do
10
+ config.before :each do
11
11
  @globals = []
12
12
  end
13
13
 
14
- c.after :each do
14
+ config.after :each do
15
15
  @globals.each do |global|
16
16
  Object.instance_eval { remove_const global } if Object.const_defined?(global)
17
17
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
5
- prerelease:
4
+ version: 0.4.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rémi Prévost
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-19 00:00:00.000000000 Z
11
+ date: 2013-02-27 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,23 +27,20 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
37
- version: '2.12'
33
+ version: '2.13'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
45
- version: '2.12'
40
+ version: '2.13'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: mocha
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
@@ -54,7 +48,6 @@ dependencies:
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
@@ -62,7 +55,6 @@ dependencies:
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: activesupport
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
59
  - - ! '>='
68
60
  - !ruby/object:Gem::Version
@@ -70,7 +62,6 @@ dependencies:
70
62
  type: :runtime
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
66
  - - ! '>='
76
67
  - !ruby/object:Gem::Version
@@ -78,7 +69,6 @@ dependencies:
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: faraday
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
73
  - - ~>
84
74
  - !ruby/object:Gem::Version
@@ -86,7 +76,6 @@ dependencies:
86
76
  type: :runtime
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
80
  - - ~>
92
81
  - !ruby/object:Gem::Version
@@ -94,7 +83,6 @@ dependencies:
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: multi_json
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
87
  - - ~>
100
88
  - !ruby/object:Gem::Version
@@ -102,7 +90,6 @@ dependencies:
102
90
  type: :runtime
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
94
  - - ~>
108
95
  - !ruby/object:Gem::Version
@@ -117,12 +104,12 @@ files:
117
104
  - .gitignore
118
105
  - .rspec
119
106
  - .travis.yml
107
+ - CONTRIBUTING.md
120
108
  - Gemfile
121
109
  - LICENSE
122
110
  - README.md
123
111
  - Rakefile
124
- - docs/CONTRIBUTING.md
125
- - docs/UPGRADE.md
112
+ - UPGRADE.md
126
113
  - examples/twitter-oauth/Gemfile
127
114
  - examples/twitter-oauth/app.rb
128
115
  - examples/twitter-oauth/config.ru
@@ -145,6 +132,7 @@ files:
145
132
  - lib/her/model/hooks.rb
146
133
  - lib/her/model/http.rb
147
134
  - lib/her/model/introspection.rb
135
+ - lib/her/model/nested_attributes.rb
148
136
  - lib/her/model/orm.rb
149
137
  - lib/her/model/paths.rb
150
138
  - lib/her/model/relationships.rb
@@ -157,6 +145,7 @@ files:
157
145
  - spec/model/hooks_spec.rb
158
146
  - spec/model/http_spec.rb
159
147
  - spec/model/introspection_spec.rb
148
+ - spec/model/nested_attributes_spec.rb
160
149
  - spec/model/orm_spec.rb
161
150
  - spec/model/paths_spec.rb
162
151
  - spec/model/relationships_spec.rb
@@ -165,33 +154,26 @@ files:
165
154
  homepage: http://remiprev.github.com/her
166
155
  licenses:
167
156
  - MIT
157
+ metadata: {}
168
158
  post_install_message:
169
159
  rdoc_options: []
170
160
  require_paths:
171
161
  - lib
172
162
  required_ruby_version: !ruby/object:Gem::Requirement
173
- none: false
174
163
  requirements:
175
164
  - - ! '>='
176
165
  - !ruby/object:Gem::Version
177
166
  version: '0'
178
- segments:
179
- - 0
180
- hash: -2845158545991818661
181
167
  required_rubygems_version: !ruby/object:Gem::Requirement
182
- none: false
183
168
  requirements:
184
169
  - - ! '>='
185
170
  - !ruby/object:Gem::Version
186
171
  version: '0'
187
- segments:
188
- - 0
189
- hash: -2845158545991818661
190
172
  requirements: []
191
173
  rubyforge_project:
192
- rubygems_version: 1.8.23
174
+ rubygems_version: 2.0.0
193
175
  signing_key:
194
- specification_version: 3
176
+ specification_version: 4
195
177
  summary: A simple Representational State Transfer-based Hypertext Transfer Protocol-powered
196
178
  Object Relational Mapper. Her?
197
179
  test_files:
@@ -203,6 +185,7 @@ test_files:
203
185
  - spec/model/hooks_spec.rb
204
186
  - spec/model/http_spec.rb
205
187
  - spec/model/introspection_spec.rb
188
+ - spec/model/nested_attributes_spec.rb
206
189
  - spec/model/orm_spec.rb
207
190
  - spec/model/paths_spec.rb
208
191
  - spec/model/relationships_spec.rb