spyke 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: debb215522b638dd3b33070246527635aabfc284
4
- data.tar.gz: 5e02ddc322d6a4811229cf939616eb5227e0d90c
3
+ metadata.gz: e544879dfca5f90ba78e73e05f7370170445fff8
4
+ data.tar.gz: 991433596aec9c35a3f7065d0c71dcb798c58f12
5
5
  SHA512:
6
- metadata.gz: 123c24f04e9f34ed7f0f3a3335c94573266f39171d9c2183d1125d7235ed239a44766e6cf9771bcd6ca40e41ac8411756551777b215b27438def9ee1be233d44
7
- data.tar.gz: ebf9567257ae4c3b2ec20c7a837b667f2107f1877d3c18471ec886779b25916f7de0617ca37919a6a3da7c5384b29ec46ad6808a1b1d1ef8c74ec7d6d4ad2cff
6
+ metadata.gz: 1f9f7d0219671080bb9d91e083d763eb10c15ed624d77c40ff5691c0e3e0880960d7d2b97175473213d8df290ccfdde20333a1e2721301c7720a17e0cb55bf9d
7
+ data.tar.gz: 4eed190ef156ea97ea07e4d0911a98d75f48f70d7692f3a1d908bc3126c92df064adb7ae33f2738a27342cfbcdbbf8e0a8b047f63f70017b3bd501673090fd8b
data/README.md CHANGED
@@ -167,6 +167,24 @@ remap it in Faraday to match the above. Doing this will allow you to
167
167
  show errors returned from the server in forms and f.ex using
168
168
  `@post.errors.full_messages` just like ActiveRecord.
169
169
 
170
+ ### Attributes-wrapping
171
+
172
+ Spyke, like Rails, by default wraps sent attributes in a root element,
173
+ but this can be disabled or customized:
174
+
175
+ ```ruby
176
+ class Article < Spyke::Base
177
+ # Default
178
+ include_root_in_json true # { article: { title: ...} }
179
+
180
+ # Custom
181
+ include_root_in_json :post # { post: { title: ...} }
182
+
183
+ # Disabled
184
+ include_root_in_json false # { title: ... }
185
+ end
186
+ ```
187
+
170
188
  ### Using multiple APIs
171
189
 
172
190
  If you need to use different APIs, instead of configuring `Spyke::Base`
@@ -36,7 +36,7 @@ module Spyke
36
36
  end
37
37
 
38
38
  def foreign_key
39
- (@options[:foreign_key] || "#{parent.class.model_name.param_key}_id").to_sym
39
+ (@options[:foreign_key] || "#{parent.class.model_name.element}_id").to_sym
40
40
  end
41
41
 
42
42
  def fetch
@@ -3,7 +3,7 @@ module Spyke
3
3
  class BelongsTo < Association
4
4
  def initialize(*args)
5
5
  super
6
- @options.reverse_merge!(uri: "#{@name.to_s.pluralize}/:id", foreign_key: "#{klass.model_name.param_key}_id")
6
+ @options.reverse_merge!(uri: "#{@name.to_s.pluralize}/:id", foreign_key: "#{klass.model_name.element}_id")
7
7
  @params[:id] = parent.try(foreign_key)
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ module Spyke
3
3
  class HasMany < Association
4
4
  def initialize(*args)
5
5
  super
6
- @options.reverse_merge!(uri: "#{parent.class.model_name.plural}/:#{foreign_key}/#{@name}/(:id)")
6
+ @options.reverse_merge!(uri: "#{parent.class.model_name.element.pluralize}/:#{foreign_key}/#{@name}/(:id)")
7
7
  @params[foreign_key] = parent.id
8
8
  end
9
9
 
@@ -72,7 +72,7 @@ module Spyke
72
72
  end
73
73
 
74
74
  def default_uri
75
- "#{model_name.plural}/(:id)"
75
+ "#{model_name.element.pluralize}/(:id)"
76
76
  end
77
77
  end
78
78
 
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '2.0.0'
2
+ VERSION = '2.0.1'
3
3
  end
@@ -391,12 +391,23 @@ module Spyke
391
391
 
392
392
  def test_namespaced_model
393
393
  tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
394
- likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
395
- Cookbook::Tip.find(1).likes.first
394
+ nested_likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
395
+ likes_endpoint = stub_request(:get, 'http://sushi.com/likes')
396
+
397
+ Cookbook::Tip.new(id: 1).likes.first
398
+ Cookbook::Like.new(tip_id: 1).tip
399
+ Cookbook::Like.all.to_a
400
+
396
401
  assert_requested tip_endpoint
402
+ assert_requested nested_likes_endpoint
397
403
  assert_requested likes_endpoint
398
404
  end
399
405
 
406
+ def test_namespaced_foreign_key
407
+ like = Cookbook::Tip.new(id: 1).likes.build
408
+ assert_equal 1, like.tip_id
409
+ end
410
+
400
411
  def test_namespaced_association_class_auto_detect
401
412
  favorite = Cookbook::Tip.new.favorites.build
402
413
  assert_equal Cookbook::Favorite, favorite.class
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module Spyke
4
- class RelationTest < MiniTest::Test
4
+ class ScopesTest < MiniTest::Test
5
5
  def test_all
6
6
  stub_request(:get, 'http://sushi.com/recipes').to_return_json(result: [{ id: 1, title: 'Sushi' }, { id: 2, title: 'Nigiri' }], metadata: 'meta')
7
7
 
@@ -130,13 +130,14 @@ end
130
130
  module Cookbook
131
131
  class Tip < Spyke::Base
132
132
  uri 'tips/(:id)'
133
- has_many :likes, class_name: 'Cookbook::Like', uri: 'tips/:cookbook_tip_id/likes/(:id)'
133
+ has_many :likes, class_name: 'Cookbook::Like'
134
134
  has_many :favorites
135
135
  has_many :votes
136
136
  has_many :photos, class_name: 'Photo'
137
137
  end
138
138
 
139
139
  class Like < Spyke::Base
140
+ belongs_to :tip
140
141
  end
141
142
 
142
143
  class Favorite < Spyke::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spyke
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Balvig
@@ -295,7 +295,7 @@ files:
295
295
  - test/custom_request_test.rb
296
296
  - test/orm_test.rb
297
297
  - test/path_test.rb
298
- - test/relation_test.rb
298
+ - test/scopes_test.rb
299
299
  - test/support/fixtures.rb
300
300
  - test/support/webmock.rb
301
301
  - test/test_helper.rb
@@ -330,7 +330,7 @@ test_files:
330
330
  - test/custom_request_test.rb
331
331
  - test/orm_test.rb
332
332
  - test/path_test.rb
333
- - test/relation_test.rb
333
+ - test/scopes_test.rb
334
334
  - test/support/fixtures.rb
335
335
  - test/support/webmock.rb
336
336
  - test/test_helper.rb