json_api_test_helpers 1.1.1 → 1.2.1.2

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
  SHA256:
3
- metadata.gz: '085d264d18f15d881bcf15ed6afc54914872902bd5b08737e5db7105d8c4de0b'
4
- data.tar.gz: b4b3ef5093a2ebb42c030ba1776ebdb5babde215c684db5d48ba00e92a7509d1
3
+ metadata.gz: e5ee478148b8932146819a1fb023f43b747bc3054df0b195f8d0371a377a6e83
4
+ data.tar.gz: 84e9ce07016a996f29a83047ff12077f9abca94f8c738fc36babc593004652f4
5
5
  SHA512:
6
- metadata.gz: 89ffbc28e38bba6dceebc8085c393c93b5f40276568334e4189a8ccb6fb1ff0e38fdd6ba50ca603563a6194da385b50996808ac2dd448bf8df724f2ae90d7934
7
- data.tar.gz: b3b5eb41031be9c7215e2d2a21aec128a9d879a27524f8eb5e56d8382e69e1be34ca184f35d7865dd065c84ba664125b622e3956330f958e9d0b314ba519bfef
6
+ metadata.gz: '038a4cf093a1d4b1db5fac6340cf4567aa3a7289e8b570e3420336e290ed730d12c9500ec96e0837fe597856edacda4e2e08c64aae38cb1a5a3def156d0d12f4'
7
+ data.tar.gz: f77a4f8fcb01ef5c0551f6d18b6106fffacf23be9b809f042a21c96d897030a136e27745330f9bc9e62d7a28b02cce430c02d000b6b4aa33d92a02969e31c109
data/README.md CHANGED
@@ -27,6 +27,7 @@ Methods:
27
27
  | json_response | | Returns parsed `response.body` as Hash with indifferent access or Array |
28
28
  | json_api_record | `record`, `attributes`, `relationships: nil`, `additional: nil`| Returns Hash in JSON API format with serialized object |
29
29
  | json_api_collection | `collection`, `attributes = nil`, `relationships: nil` | Returns Hash in JSON API format with serialized array |
30
+ | json_api_relationships | `record`, `relationships` | Returns Hash of the record's relationships. When `relationships` argument is Array, all releations have name same as model class names. When it's Hash { relationship_name => class_name } |
30
31
  | fix_value_for_json | `value` | Fix values which are usually used in Rails apps. `DateTime`, `ActiveSupport::TimeWithZone` to `iso8601`; `CarrierWave::Uploader::Serialization` to `serializable_hash`; everything else to Hash with replaced underscores `_` to minus `-` in attribute names |
31
32
  | fix_comparing_types | `value` | Fix type value to easy compare. `DateTime`, `ActiveSupport::TimeWithZone` to `datetime` with UTC; `ActiveRecord::Point` to string `"#{value.x}, #{value.y}"`. |
32
33
  | attributes_for_nested | `attributes`, `**associations` | Merge `attributes` with associations with JSON API format |
@@ -39,6 +40,8 @@ expect(json_response).to include_json(json_api_record(User.first, [ :email, :ano
39
40
 
40
41
  expect(json_response).to include_json(json_api_collection(User.all))
41
42
 
43
+ expect(json_response).to include_json(json_api_relationships(user, posts: :user_posts))
44
+
42
45
  post 'some_post', params: json_api_params({ attribute1: :value1, attribute2: :value2 })
43
46
  ```
44
47
 
@@ -12,7 +12,7 @@ module JsonApiTestHelpers
12
12
  def json_api_record(record, attributes, relationships: nil, additional: nil)
13
13
  json = {
14
14
  'data' => {
15
- 'id' => record.id.to_s,
15
+ 'id' => record.uuid,
16
16
  'attributes' => json_api_record_attributes(record, attributes)
17
17
  }
18
18
  }
@@ -24,7 +24,7 @@ module JsonApiTestHelpers
24
24
  {
25
25
  'data' => collection.map do |record|
26
26
  hash = {
27
- 'id' => record.id.to_s
27
+ 'id' => record.uuid
28
28
  }
29
29
  hash['attributes'] = json_api_record_attributes(record, attributes) if attributes
30
30
  hash['relationships'] = json_api_relationships(record, relationships) if relationships
@@ -66,8 +66,6 @@ module JsonApiTestHelpers
66
66
  end
67
67
  end
68
68
 
69
- private
70
-
71
69
  def json_api_record_attributes(record, attributes)
72
70
  attributes.reduce({}) do |hash, attr|
73
71
  hash.merge! attr.to_s.tr('_', '-') => fix_value_for_json(record.send(attr))
@@ -76,13 +74,15 @@ module JsonApiTestHelpers
76
74
 
77
75
  def json_api_relationships(record, relationships)
78
76
  relationships.reduce({}) do |hash, relationship|
79
- object = record.send relationship
77
+ name = relation_has_another_class_name?(relationship) ? relationship[0] : relationship
78
+ class_name = relation_has_another_class_name?(relationship) ? relationship[1] : relationship
79
+ object = record.send name
80
80
  data = if object.is_a?(ActiveRecord::Associations::CollectionProxy)
81
- object.reduce([]) { |result, item| result << data_relationship_object(item, relationship) }
81
+ object.reduce([]) { |result, item| result << data_relationship_object(item, class_name) }
82
82
  else
83
- object.present? ? data_relationship_object(object, relationship) : {}
83
+ object.present? ? data_relationship_object(object, class_name) : {}
84
84
  end
85
- hash.merge! json_api_model_name(relationship) => { 'data' => data }
85
+ hash.merge! json_api_model_name(name) => { 'data' => data }
86
86
  end
87
87
  end
88
88
 
@@ -92,9 +92,13 @@ module JsonApiTestHelpers
92
92
 
93
93
  def data_relationship_object(object, relationship)
94
94
  {
95
- 'id' => object.id.to_s,
95
+ 'id' => object.uuid,
96
96
  'type' => json_api_model_name(relationship).pluralize
97
97
  }
98
98
  end
99
+
100
+ def relation_has_another_class_name?(relationship)
101
+ relationship.is_a? Array
102
+ end
99
103
  end
100
104
  end
@@ -1,3 +1,3 @@
1
1
  module JsonApiTestHelpers
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.1.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_test_helpers
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pavel Kalashnikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-07-25 00:00:00.000000000 Z
11
+ date: 2022-01-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -93,8 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  - !ruby/object:Gem::Version
94
94
  version: '0'
95
95
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.7.6
96
+ rubygems_version: 3.0.3.1
98
97
  signing_key:
99
98
  specification_version: 4
100
99
  summary: JSON API helpers for testing