json_api_client 1.6.4 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43ad8115fe9360a602c120c6f4989ee54760d6a3
4
- data.tar.gz: 9709b02b48e2c8b2295bd76b8620fcb1efaad487
3
+ metadata.gz: b1eaf54d769a3c0308319a18629440a2fea1fcdc
4
+ data.tar.gz: da232e4dfd3b4e13a45c69c22b993fa9c08f209b
5
5
  SHA512:
6
- metadata.gz: 0f7c487d82b18df80d0b7ca0a076028ea9b0b5b6eeb9e7a154f83a95d3a7f0af6af418418b06168dd860fd5c1225ec9a064603660354bfae3c42d7524d8bccb8
7
- data.tar.gz: c4d8967b2a2e85a8c9ba486290e1f860551e83eab7e3022b3fc8d408ef2f3bd204ec56cd57e6e20c60cf72c6dfbddcf433281b4b45777fd00a6c49226080e6e9
6
+ metadata.gz: 9737c509ddc0c91b6bf759e2b891acee5de9fe7c99e83f88bf4b1f8deef40b648dc166a03bad952ebcac0aada03a39aafcc538f05adea2473ec28a25bf54f2a2
7
+ data.tar.gz: b5e197b2d9164b5496cb0df20d248660d6191d5186403ea0da9f6df234bd2d9275974214000c5a730d07dac0ecfd0a40323d0727e95d8ae77f37d4347919f69d
data/README.md CHANGED
@@ -157,13 +157,17 @@ articles.links.related
157
157
 
158
158
  You can force nested resource paths for your models by using a `belongs_to` association.
159
159
 
160
- **Note: Using belongs_to is only necessary for setting a nested path.**
160
+ **Note: Using belongs_to is only necessary for setting a nested path unless you provide `shallow_path: true` option.**
161
161
 
162
162
  ```ruby
163
163
  module MyApi
164
164
  class Account < JsonApiClient::Resource
165
165
  belongs_to :user
166
166
  end
167
+
168
+ class Customer < JsonApiClient::Resource
169
+ belongs_to :user, shallow_path: true
170
+ end
167
171
  end
168
172
 
169
173
  # try to find without the nested parameter
@@ -173,6 +177,28 @@ MyApi::Account.find(1)
173
177
  # makes request to /users/2/accounts/1
174
178
  MyApi::Account.where(user_id: 2).find(1)
175
179
  # => returns ResultSet
180
+
181
+ # makes request to /customers/1
182
+ MyApi::Customer.find(1)
183
+ # => returns ResultSet
184
+
185
+ # makes request to /users/2/customers/1
186
+ MyApi::Customer.where(user_id: 2).find(1)
187
+ # => returns ResultSet
188
+ ```
189
+
190
+ you can also override param name for `belongs_to` association
191
+
192
+ ```ruby
193
+ module MyApi
194
+ class Account < JsonApiClient::Resource
195
+ belongs_to :user, param: :customer_id
196
+ end
197
+ end
198
+
199
+ # makes request to /users/2/accounts/1
200
+ MyApi::Account.where(customer_id: 2).find(1)
201
+ # => returns ResultSet
176
202
  ```
177
203
 
178
204
  ## Custom Methods
@@ -3,8 +3,17 @@ module JsonApiClient
3
3
  module BelongsTo
4
4
  class Association < BaseAssociation
5
5
  include Helpers::URI
6
- def param
7
- :"#{attr_name}_id"
6
+
7
+ attr_reader :param
8
+
9
+ def initialize(attr_name, klass, options = {})
10
+ super
11
+ @param = options.fetch(:param, :"#{attr_name}_id").to_sym
12
+ @shallow_path = options.fetch(:shallow_path, false)
13
+ end
14
+
15
+ def shallow_path?
16
+ @shallow_path
8
17
  end
9
18
 
10
19
  def to_prefix_path(formatter)
@@ -12,6 +21,7 @@ module JsonApiClient
12
21
  end
13
22
 
14
23
  def set_prefix_path(attrs, formatter)
24
+ return if shallow_path? && !attrs[param]
15
25
  attrs[param] = encode_part(attrs[param]) if attrs.key?(param)
16
26
  to_prefix_path(formatter) % attrs
17
27
  end
@@ -7,6 +7,7 @@ module JsonApiClient
7
7
  class_attribute :associations, instance_accessor: false
8
8
  self.associations = []
9
9
  attr_accessor :__cached_associations
10
+ attr_accessor :__belongs_to_params
10
11
  end
11
12
 
12
13
  module ClassMethods
@@ -14,6 +15,10 @@ module JsonApiClient
14
15
  attr_name = attr_name.to_sym
15
16
  association = association_klass.new(attr_name, self, options)
16
17
  self.associations += [association]
18
+ end
19
+
20
+ def _define_relationship_methods(attr_name)
21
+ attr_name = attr_name.to_sym
17
22
 
18
23
  define_method(attr_name) do
19
24
  _cached_relationship(attr_name) do
@@ -31,17 +36,36 @@ module JsonApiClient
31
36
 
32
37
  def belongs_to(attr_name, options = {})
33
38
  _define_association(attr_name, JsonApiClient::Associations::BelongsTo::Association, options)
39
+
40
+ param = associations.last.param
41
+ define_method(param) do
42
+ _belongs_to_params[param]
43
+ end
44
+
45
+ define_method(:"#{param}=") do |value|
46
+ _belongs_to_params[param] = value
47
+ end
34
48
  end
35
49
 
36
50
  def has_many(attr_name, options = {})
37
51
  _define_association(attr_name, JsonApiClient::Associations::HasMany::Association, options)
52
+ _define_relationship_methods(attr_name)
38
53
  end
39
54
 
40
55
  def has_one(attr_name, options = {})
41
56
  _define_association(attr_name, JsonApiClient::Associations::HasOne::Association, options)
57
+ _define_relationship_methods(attr_name)
42
58
  end
43
59
  end
44
60
 
61
+ def _belongs_to_params
62
+ self.__belongs_to_params ||= {}
63
+ end
64
+
65
+ def _clear_belongs_to_params
66
+ self.__belongs_to_params = {}
67
+ end
68
+
45
69
  def _cached_associations
46
70
  self.__cached_associations ||= {}
47
71
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support/all'
2
+
1
3
  module JsonApiClient
2
4
  module Query
3
5
  class Builder
@@ -64,8 +66,12 @@ module JsonApiClient
64
66
  paginate(page: 1, per_page: 1).pages.last.to_a.last
65
67
  end
66
68
 
67
- def build
68
- klass.new(params)
69
+ def build(attrs = {})
70
+ klass.new @path_params.merge(attrs.symbolize_keys)
71
+ end
72
+
73
+ def create(attrs = {})
74
+ klass.create @path_params.merge(attrs.symbolize_keys)
69
75
  end
70
76
 
71
77
  def params
@@ -10,14 +10,14 @@ module JsonApiClient
10
10
 
11
11
  # expects a record
12
12
  def create(record)
13
- request(:post, klass.path(record.attributes), {
13
+ request(:post, klass.path(record.path_attributes), {
14
14
  body: { data: record.as_json_api },
15
15
  params: record.request_params.to_params
16
16
  })
17
17
  end
18
18
 
19
19
  def update(record)
20
- request(:patch, resource_path(record.attributes), {
20
+ request(:patch, resource_path(record.path_attributes), {
21
21
  body: { data: record.as_json_api },
22
22
  params: record.request_params.to_params
23
23
  })
@@ -30,7 +30,7 @@ module JsonApiClient
30
30
  end
31
31
 
32
32
  def destroy(record)
33
- request(:delete, resource_path(record.attributes))
33
+ request(:delete, resource_path(record.path_attributes))
34
34
  end
35
35
 
36
36
  def linked(path)
@@ -11,7 +11,6 @@ module JsonApiClient
11
11
  def initialize(record_class, relations)
12
12
  @record_class = record_class
13
13
  self.attributes = relations
14
- clear_changes_information
15
14
  end
16
15
 
17
16
  def present?
@@ -98,6 +98,7 @@ module JsonApiClient
98
98
  new(params).tap do |resource|
99
99
  resource.mark_as_persisted!
100
100
  resource.clear_changes_information
101
+ resource.relationships.clear_changes_information
101
102
  end
102
103
  end
103
104
 
@@ -318,7 +319,8 @@ module JsonApiClient
318
319
  @destroyed = nil
319
320
  self.links = self.class.linker.new(params.delete(:links) || {})
320
321
  self.relationships = self.class.relationship_linker.new(self.class, params.delete(:relationships) || {})
321
- self.attributes = self.class.default_attributes.merge(params)
322
+ self.attributes = self.class.default_attributes.merge params.except(*self.class.prefix_params)
323
+ self.__belongs_to_params = params.slice(*self.class.prefix_params)
322
324
 
323
325
  setup_default_properties
324
326
 
@@ -465,6 +467,7 @@ module JsonApiClient
465
467
  mark_as_destroyed!
466
468
  self.relationships.last_result_set = nil
467
469
  _clear_cached_relationships
470
+ _clear_belongs_to_params
468
471
  true
469
472
  end
470
473
  end
@@ -498,6 +501,10 @@ module JsonApiClient
498
501
  self
499
502
  end
500
503
 
504
+ def path_attributes
505
+ _belongs_to_params.merge attributes.slice('id').symbolize_keys
506
+ end
507
+
501
508
  protected
502
509
 
503
510
  def setup_default_properties
@@ -566,10 +573,7 @@ module JsonApiClient
566
573
  end
567
574
 
568
575
  def non_serializing_attributes
569
- [
570
- self.class.read_only_attributes,
571
- self.class.prefix_params.map(&:to_s)
572
- ].flatten
576
+ self.class.read_only_attributes
573
577
  end
574
578
 
575
579
  def attributes_for_serialization
@@ -1,3 +1,3 @@
1
1
  module JsonApiClient
2
- VERSION = "1.6.4"
2
+ VERSION = "1.7.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.4
4
+ version: 1.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeff Ching
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-26 00:00:00.000000000 Z
11
+ date: 2018-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport