her 0.10.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -204,7 +204,7 @@ describe Her::Model::Attributes do
204
204
 
205
205
  spawn_model "Foo::User" do
206
206
  def document
207
- @attributes[:document][:url]
207
+ self.attributes[:document][:url]
208
208
  end
209
209
  end
210
210
  end
@@ -229,7 +229,7 @@ describe Her::Model::Attributes do
229
229
 
230
230
  spawn_model "Foo::User" do
231
231
  def document=(document)
232
- @attributes[:document] = document[:url]
232
+ @_her_attributes[:document] = document[:url]
233
233
  end
234
234
  end
235
235
  end
@@ -288,19 +288,19 @@ describe Her::Model::Attributes do
288
288
  it { is_expected.to respond_to(:fullname?) }
289
289
  end
290
290
 
291
- it "defines setter that affects @attributes" do
291
+ it "defines setter that affects attributes" do
292
292
  user = Foo::User.new
293
293
  user.fullname = "Tobias Fünke"
294
294
  expect(user.attributes[:fullname]).to eq("Tobias Fünke")
295
295
  end
296
296
 
297
- it "defines getter that reads @attributes" do
297
+ it "defines getter that reads attributes" do
298
298
  user = Foo::User.new
299
299
  user.assign_attributes(fullname: "Tobias Fünke")
300
300
  expect(user.fullname).to eq("Tobias Fünke")
301
301
  end
302
302
 
303
- it "defines predicate that reads @attributes" do
303
+ it "defines predicate that reads attributes" do
304
304
  user = Foo::User.new
305
305
  expect(user.fullname?).to be_falsey
306
306
  user.assign_attributes(fullname: "Tobias Fünke")
@@ -335,19 +335,19 @@ describe Her::Model::Attributes do
335
335
  expect(Foo::User.generated_attribute_methods.instance_methods).to include(:fullname?)
336
336
  end
337
337
 
338
- it "defines setter that affects @attributes" do
338
+ it "defines setter that affects attributes" do
339
339
  user = Foo::User.new
340
340
  user.fullname = "Tobias Fünke"
341
341
  expect(user.attributes[:fullname]).to eq("Tobias Fünke")
342
342
  end
343
343
 
344
- it "defines getter that reads @attributes" do
344
+ it "defines getter that reads attributes" do
345
345
  user = Foo::User.new
346
346
  user.attributes[:fullname] = "Tobias Fünke"
347
347
  expect(user.fullname).to eq("Tobias Fünke")
348
348
  end
349
349
 
350
- it "defines predicate that reads @attributes" do
350
+ it "defines predicate that reads attributes" do
351
351
  user = Foo::User.new
352
352
  expect(user.fullname?).to be_falsey
353
353
  user.attributes[:fullname] = "Tobias Fünke"
@@ -58,7 +58,7 @@ describe "Her::Model and ActiveModel::Callbacks" do
58
58
  end
59
59
  end
60
60
 
61
- it "should call the server with the canged value" do
61
+ it "should call the server with the changed value" do
62
62
  expect(subject.name).to eq("Tobias Funke")
63
63
  subject.save
64
64
  expect(subject.name).to eq("Lumberjack")
@@ -8,16 +8,24 @@ describe "Her::Model and ActiveModel::Dirty" do
8
8
  builder.use Her::Middleware::FirstLevelParseJSON
9
9
  builder.use Faraday::Request::UrlEncoded
10
10
  builder.adapter :test do |stub|
11
+ stub.get("/users") { [200, {}, [{ id: 1, fullname: "Lindsay Fünke" }, { id: 2, fullname: "Maeby Fünke" }].to_json] }
11
12
  stub.get("/users/1") { [200, {}, { id: 1, fullname: "Lindsay Fünke" }.to_json] }
12
13
  stub.get("/users/2") { [200, {}, { id: 2, fullname: "Maeby Fünke" }.to_json] }
13
14
  stub.get("/users/3") { [200, {}, { user_id: 3, fullname: "Maeby Fünke" }.to_json] }
14
15
  stub.put("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
15
16
  stub.put("/users/2") { [400, {}, { errors: ["Email cannot be blank"] }.to_json] }
16
17
  stub.post("/users") { [200, {}, { id: 1, fullname: "Tobias Fünke" }.to_json] }
18
+ stub.get("/users/1/posts") { [200, {}, [{ id: 1, user_id: 1, body: "Hello" }].to_json] }
19
+ stub.get("/users/1/posts/1") { [200, {}, { id: 1, user_id: 1, body: "Hello" }.to_json] }
17
20
  end
18
21
  end
19
22
 
23
+ spawn_model "Foo::Post" do
24
+ belongs_to :user
25
+ attributes :body
26
+ end
20
27
  spawn_model "Foo::User" do
28
+ has_many :posts
21
29
  attributes :fullname, :email
22
30
  end
23
31
  spawn_model "Dynamic::User" do
@@ -74,6 +82,32 @@ describe "Her::Model and ActiveModel::Dirty" do
74
82
  end
75
83
  end
76
84
 
85
+ context "for an existing resource from an association" do
86
+ let(:post) { Foo::User.find(1).posts.find(1) }
87
+ it "has no changes" do
88
+ expect(post.changes).to be_empty
89
+ expect(post).to_not be_changed
90
+ end
91
+ end
92
+
93
+ context "for an existing resource from an association collection" do
94
+ let(:post) { Foo::User.find(1).posts.first }
95
+ it "has no changes" do
96
+ expect(post.changes).to be_empty
97
+ expect(post).to_not be_changed
98
+ end
99
+ end
100
+
101
+ context "for resources from a collection" do
102
+ let(:users) { Foo::User.all.fetch }
103
+ it "has no changes" do
104
+ users.each do |user|
105
+ expect(user.changes).to be_empty
106
+ expect(user).to_not be_changed
107
+ end
108
+ end
109
+ end
110
+
77
111
  context "for new resource" do
78
112
  let(:user) { Foo::User.new(fullname: "Lindsay Fünke") }
79
113
  it "has changes" do
@@ -87,6 +121,12 @@ describe "Her::Model and ActiveModel::Dirty" do
87
121
  expect(user).not_to be_changed
88
122
  end
89
123
  end
124
+
125
+ context "for a new resource from an association" do
126
+ let(:post) { Foo::User.find(1).posts.build }
127
+ it "has changes" do
128
+ expect(post).to be_changed
129
+ end
130
+ end
90
131
  end
91
132
  end
92
- #
@@ -73,7 +73,7 @@ describe Her::Model::Introspection do
73
73
  expect(Foo::User.her_nearby_class("AccessRecord")).to eq(Foo::AccessRecord)
74
74
  expect(AccessRecord.her_nearby_class("Log")).to eq(Log)
75
75
  expect(Foo::User.her_nearby_class("Log")).to eq(Log)
76
- expect(Foo::User.her_nearby_class("X")).to be_nil
76
+ expect{Foo::User.her_nearby_class("X")}.to raise_error(NameError)
77
77
  end
78
78
  end
79
79
  end
@@ -172,11 +172,11 @@ describe Her::Model::ORM do
172
172
 
173
173
  def friends=(val)
174
174
  val = val.delete("\r").split("\n").map { |friend| friend.gsub(/^\s*\*\s*/, "") } if val && val.is_a?(String)
175
- @attributes[:friends] = val
175
+ @_her_attributes[:friends] = val
176
176
  end
177
177
 
178
178
  def friends
179
- @attributes[:friends].map { |friend| "* #{friend}" }.join("\n")
179
+ @_her_attributes[:friends].map { |friend| "* #{friend}" }.join("\n")
180
180
  end
181
181
  end
182
182
  end
@@ -185,7 +185,7 @@ describe Her::Model::ORM do
185
185
  @user = User.find(1)
186
186
  expect(@user.friends).to eq("* Maeby\n* GOB\n* Anne")
187
187
  @user.instance_eval do
188
- @attributes[:friends] = %w(Maeby GOB Anne)
188
+ @_her_attributes[:friends] = %w(Maeby GOB Anne)
189
189
  end
190
190
  end
191
191
 
@@ -194,7 +194,7 @@ describe Her::Model::ORM do
194
194
  @user.friends = "* George\n* Oscar\n* Lucille"
195
195
  expect(@user.friends).to eq("* George\n* Oscar\n* Lucille")
196
196
  @user.instance_eval do
197
- @attributes[:friends] = %w(George Oscar Lucille)
197
+ @_her_attributes[:friends] = %w(George Oscar Lucille)
198
198
  end
199
199
  end
200
200
  end
@@ -491,12 +491,30 @@ describe Her::Model::ORM do
491
491
  builder.adapter :test do |stub|
492
492
  stub.get("/users/1") { [200, {}, { id: 1, fullname: "Tobias Fünke", active: true }.to_json] }
493
493
  stub.delete("/users/1") { [status, {}, { id: 1, fullname: "Lindsay Fünke", active: false }.to_json] }
494
+
495
+ stub.get("/child_users") { [200, {}, { data: [{ id: 1, name: "Tobias Fünke" }, { id: 2, name: "Lindsay Fünke" }], metadata: { total_pages: 10, next_page: 2 }, errors: %w(Oh My God) }.to_json] }
496
+ stub.get("/child_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] }
497
+ stub.post("/child_users") { |env| [200, {}, { :data => { :name => "George Michael Bluth" }, :metadata => { :foo => "bar" }, :errors => ["Yes", "Sir"] }.to_json] }
498
+ stub.delete("/child_users/1") { |env| [200, {}, { :data => { :id => 1 }, :metadata => { :foo => "bar" }, :errors => ["Yes", "Sir"] }.to_json] }
494
499
  end
495
500
  end
496
501
 
497
502
  spawn_model "Foo::User"
498
503
  end
499
504
 
505
+ it "handles proper resource deletion on a child model class" do
506
+ child_user_klass = Class.new(Foo::User) do
507
+ def self.name
508
+ "ChildUser"
509
+ end
510
+ end
511
+
512
+ child_user_klass.destroy_existing(1)
513
+ @child_user = child_user_klass.create(name: "George Michael Bluth")
514
+
515
+ expect { @child_user.save! }.to raise_error(Her::Errors::ResourceInvalid)
516
+ end
517
+
500
518
  it "handle resource deletion through the .destroy class method" do
501
519
  @user = Foo::User.destroy_existing(1)
502
520
  expect(@user.active).to be_falsey
@@ -24,6 +24,21 @@ describe Her::Model::Parse do
24
24
  end
25
25
  end
26
26
 
27
+ it "inherits attributes from parent class" do
28
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
29
+ end
30
+
31
+ expect(Foo::ChildUser).to be_include_root_in_json
32
+ end
33
+
34
+ it "allows `include_root_in_json` to be set to `false` on a child model" do
35
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
36
+ include_root_in_json false
37
+ end
38
+
39
+ expect(Foo::ChildUser).to_not be_include_root_in_json
40
+ end
41
+
27
42
  it "wraps params in the element name in `to_params`" do
28
43
  @new_user = Foo::User.new(fullname: "Tobias Fünke")
29
44
  expect(@new_user.to_params).to eq(user: { fullname: "Tobias Fünke" })
@@ -35,6 +50,29 @@ describe Her::Model::Parse do
35
50
  end
36
51
  end
37
52
 
53
+ context "to false" do
54
+ before do
55
+ spawn_model "Foo::User" do
56
+ include_root_in_json false
57
+ end
58
+ end
59
+
60
+ it "inherits attributes from parent class" do
61
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
62
+ end
63
+
64
+ expect(Foo::ChildUser).to_not be_include_root_in_json
65
+ end
66
+
67
+ it "allows `include_root_in_json` to be set to `true` on a child model" do
68
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
69
+ include_root_in_json true
70
+ end
71
+
72
+ expect(Foo::ChildUser).to be_include_root_in_json
73
+ end
74
+ end
75
+
38
76
  context "to a symbol" do
39
77
  before do
40
78
  spawn_model "Foo::User" do
@@ -64,6 +102,54 @@ describe Her::Model::Parse do
64
102
  end
65
103
  end
66
104
 
105
+ context "when `request_new_object_on_build` is set" do
106
+ context "to true" do
107
+ before do
108
+ spawn_model "Foo::User" do
109
+ request_new_object_on_build true
110
+ end
111
+ end
112
+
113
+ it "inherits attributes from parent class" do
114
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
115
+ end
116
+
117
+ expect(Foo::ChildUser).to be_request_new_object_on_build
118
+ end
119
+
120
+ it "allows `request_new_object_on_build` to be set to `false` on a child model" do
121
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
122
+ request_new_object_on_build false
123
+ end
124
+
125
+ expect(Foo::ChildUser).to_not be_request_new_object_on_build
126
+ end
127
+ end
128
+
129
+ context "to false" do
130
+ before do
131
+ spawn_model "Foo::User" do
132
+ request_new_object_on_build false
133
+ end
134
+ end
135
+
136
+ it "inherits attributes from parent class" do
137
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
138
+ end
139
+
140
+ expect(Foo::ChildUser).to_not be_request_new_object_on_build
141
+ end
142
+
143
+ it "allows `request_new_object_on_build` to be set to `true` on a child model" do
144
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
145
+ request_new_object_on_build true
146
+ end
147
+
148
+ expect(Foo::ChildUser).to be_request_new_object_on_build
149
+ end
150
+ end
151
+ end
152
+
67
153
  context "when parse_root_in_json is set" do
68
154
  before do
69
155
  Her::API.setup url: "https://api.example.com" do |builder|
@@ -88,6 +174,21 @@ describe Her::Model::Parse do
88
174
  end
89
175
  end
90
176
 
177
+ it "inherits attributes from parent class" do
178
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
179
+ end
180
+
181
+ expect(Foo::ChildUser).to be_parse_root_in_json
182
+ end
183
+
184
+ it "allows `parse_root_in_json` to be set to `false` on a child model" do
185
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
186
+ parse_root_in_json false
187
+ end
188
+
189
+ expect(Foo::ChildUser).to_not be_parse_root_in_json
190
+ end
191
+
91
192
  it "parse the data from the JSON root element after .create" do
92
193
  @new_user = Foo::User.create(fullname: "Lindsay Fünke")
93
194
  expect(@new_user.fullname).to eq("Lindsay Fünke")
@@ -116,6 +217,29 @@ describe Her::Model::Parse do
116
217
  end
117
218
  end
118
219
 
220
+ context "to false" do
221
+ before do
222
+ spawn_model "Foo::User" do
223
+ parse_root_in_json false
224
+ end
225
+ end
226
+
227
+ it "inherits attributes from parent class" do
228
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
229
+ end
230
+
231
+ expect(Foo::ChildUser).to_not be_parse_root_in_json
232
+ end
233
+
234
+ it "allows `parse_root_in_json` to be set to `true` on a child model" do
235
+ spawn_model "Foo::ChildUser", super_class: Foo::User do
236
+ parse_root_in_json true
237
+ end
238
+
239
+ expect(Foo::ChildUser).to be_parse_root_in_json
240
+ end
241
+ end
242
+
119
243
  context "to a symbol" do
120
244
  before do
121
245
  Her::API.default_api.connection.adapter :test do |stub|
@@ -342,4 +466,31 @@ describe Her::Model::Parse do
342
466
  expect(user.to_params).to eql(user: { first_name: "Someone" })
343
467
  end
344
468
  end
469
+
470
+ context 'when passed a non-Her ActiveModel instance' do
471
+ before do
472
+ klass = Class.new do
473
+ include ActiveModel::Serialization
474
+
475
+ def attributes
476
+ { 'name' => nil }
477
+ end
478
+
479
+ def name
480
+ 'foo'
481
+ end
482
+ end
483
+
484
+ @model = klass.new
485
+
486
+ Her::API.setup
487
+ spawn_model 'Foo::User'
488
+ end
489
+
490
+ it 'serializes the instance in `to_params`' do
491
+ attributes = { model: @model }
492
+ user = Foo::User.new(attributes)
493
+ expect(user.to_params).to eq(model: { name: 'foo' })
494
+ end
495
+ end
345
496
  end
@@ -79,8 +79,8 @@ describe Her::Model::Relation do
79
79
  end
80
80
 
81
81
  it "propagates the scopes through its children" do
82
- @users = User.page(2)
83
- expect(@users.length).to eq(2)
82
+ expect(User.page(2).length).to eq(2)
83
+ expect(User.scoped.page(2).length).to eq(2)
84
84
  end
85
85
  end
86
86
  end
@@ -135,6 +135,7 @@ describe Her::Model::Relation do
135
135
  stub.get("/users?what=4&where=3") { ok! [{ id: 3, fullname: "Maeby Fünke" }] }
136
136
  stub.get("/users?what=2") { ok! [{ id: 2, fullname: "Lindsay Fünke" }] }
137
137
  stub.get("/users?where=6") { ok! [{ id: 4, fullname: "Tobias Fünke" }] }
138
+ stub.get('/bar/users') { ok! [] }
138
139
  end
139
140
  end
140
141
 
@@ -143,6 +144,11 @@ describe Her::Model::Relation do
143
144
  scope :bar, ->(v) { where(where: v) }
144
145
  scope :baz, -> { bar(6) }
145
146
  end
147
+
148
+ spawn_model "Bar::User" do
149
+ collection_path '/bar/users'
150
+ scope :baz, -> { where(where: 7) }
151
+ end
146
152
  end
147
153
 
148
154
  it "passes query parameters" do
@@ -159,6 +165,12 @@ describe Her::Model::Relation do
159
165
  @user = Foo::User.baz.first
160
166
  expect(@user.id).to eq(4)
161
167
  end
168
+
169
+ it "does not share scope with other models" do
170
+ expect(Bar::User.scoped).not_to respond_to(:foo, :bar)
171
+ expect(Foo::User.scoped.baz.params[:where]).to eq(6)
172
+ expect(Bar::User.scoped.baz.params[:where]).to eq(7)
173
+ end
162
174
  end
163
175
 
164
176
  describe :default_scope do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: her
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rémi Prévost
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-04 00:00:00.000000000 Z
11
+ date: 2018-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -59,9 +59,9 @@ dependencies:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: 3.0.0
62
- - - "<="
62
+ - - "<"
63
63
  - !ruby/object:Gem::Version
64
- version: 6.0.0
64
+ version: 5.2.0
65
65
  type: :runtime
66
66
  prerelease: false
67
67
  version_requirements: !ruby/object:Gem::Requirement
@@ -69,9 +69,9 @@ dependencies:
69
69
  - - ">="
70
70
  - !ruby/object:Gem::Version
71
71
  version: 3.0.0
72
- - - "<="
72
+ - - "<"
73
73
  - !ruby/object:Gem::Version
74
- version: 6.0.0
74
+ version: 5.2.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: activesupport
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -79,9 +79,9 @@ dependencies:
79
79
  - - ">="
80
80
  - !ruby/object:Gem::Version
81
81
  version: 3.0.0
82
- - - "<="
82
+ - - "<"
83
83
  - !ruby/object:Gem::Version
84
- version: 6.0.0
84
+ version: 5.2.0
85
85
  type: :runtime
86
86
  prerelease: false
87
87
  version_requirements: !ruby/object:Gem::Requirement
@@ -89,9 +89,9 @@ dependencies:
89
89
  - - ">="
90
90
  - !ruby/object:Gem::Version
91
91
  version: 3.0.0
92
- - - "<="
92
+ - - "<"
93
93
  - !ruby/object:Gem::Version
94
- version: 6.0.0
94
+ version: 5.2.0
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: faraday
97
97
  requirement: !ruby/object:Gem::Requirement
@@ -225,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
225
225
  version: '0'
226
226
  requirements: []
227
227
  rubyforge_project:
228
- rubygems_version: 2.5.1
228
+ rubygems_version: 2.4.5.1
229
229
  signing_key:
230
230
  specification_version: 4
231
231
  summary: A simple Representational State Transfer-based Hypertext Transfer Protocol-powered