json_api_model 0.3.1 → 0.3.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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +15 -1
- data/lib/json_api_model/associations/has.rb +11 -1
- data/lib/json_api_model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6f8100a1075e901d8733ccea171cb0fa211de66
|
4
|
+
data.tar.gz: d1fa53d8997a473bd2efc1b83b8e9cf65fdef14b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8eef7df9cc3053feaa2600a7b38a87fdb17d6f1a8a3df89e8ed8390470cff0f8479422e328e91a29948fc7ffc4befc3a36371a4518a20b2afd75675f97df5316
|
7
|
+
data.tar.gz: 8228712a1522f1d91bf3771b0afa41895dae137095541b45cbbe31a9f8c1fdac4773ccaa187dce137176ff1e9f2ffbfacddfa668a39457165c09a529345521bb
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -65,9 +65,10 @@ Associations have some of the standard ActiveRecord options. Namely:
|
|
65
65
|
end
|
66
66
|
```
|
67
67
|
|
68
|
+
__NOTE__: Due to perf implications the following are __only__ available for local classes (not things that come through the `relatinships` block in the payload). This will __*never ever*__ be available for remote models.
|
69
|
+
|
68
70
|
* `through`: many to may association helper.
|
69
71
|
|
70
|
-
__NOTE__: Due to perf implications this is __only__ available for local classes (not things that come through the `relatinships` block in the payload). This will __*never ever*__ be available for remote models.
|
71
72
|
|
72
73
|
```ruby
|
73
74
|
class Person < JsonApiModel::Model
|
@@ -86,6 +87,19 @@ __NOTE__: Due to perf implications this is __only__ available for local classes
|
|
86
87
|
end
|
87
88
|
```
|
88
89
|
|
90
|
+
* `as`: specifies what the associated object calls the caller
|
91
|
+
```ruby
|
92
|
+
class Person < JsonApiModel::Model
|
93
|
+
wraps MyApi::Client::Person
|
94
|
+
has_many :images, as: :target
|
95
|
+
end
|
96
|
+
|
97
|
+
class Image < ActiveRecord::Base
|
98
|
+
belongs_to :target
|
99
|
+
end
|
100
|
+
```
|
101
|
+
__NOTE__: only works for `has_one` and `has_many`
|
102
|
+
|
89
103
|
### Example
|
90
104
|
|
91
105
|
If you have an app that talks to a `user` service. Here's how your `User` model might look:
|
@@ -11,13 +11,15 @@ module JsonApiModel
|
|
11
11
|
{ id: instance.relationship_ids( name ) }
|
12
12
|
elsif through?
|
13
13
|
{ id: target_ids( instance ) }
|
14
|
+
elsif as?
|
15
|
+
{ "#{as}_id" => instance.id }
|
14
16
|
else
|
15
17
|
{ key => instance.id }
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def additional_options
|
20
|
-
[ :through ]
|
22
|
+
[ :as, :through ]
|
21
23
|
end
|
22
24
|
|
23
25
|
protected
|
@@ -30,6 +32,14 @@ module JsonApiModel
|
|
30
32
|
opts.has_key? :through
|
31
33
|
end
|
32
34
|
|
35
|
+
def as
|
36
|
+
opts[:as]
|
37
|
+
end
|
38
|
+
|
39
|
+
def as?
|
40
|
+
opts.has_key? :as
|
41
|
+
end
|
42
|
+
|
33
43
|
def target_ids( instance )
|
34
44
|
intermadiates = Array(instance.send( through ) )
|
35
45
|
|