json_apiable 0.2.1 → 0.3
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 +2 -2
- data/lib/json_apiable/json_apiable.rb +22 -4
- data/lib/json_apiable/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d67de4927cb04df83deea484a0890bdfdc36cbf48d6bda986000ffabcda8f3b
|
4
|
+
data.tar.gz: a90a940bc16d451c698355f1e18c74a5e183135ed3315f7aa272ad9a37e345f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee5e99fac5548996d4222e3b281ca8d3d03c78e95b57414c08b8f6f624497140c1eff43d0f5e5d54a3c2c2fec9501e92b09d37deb2e5c688c132161ca88f031c
|
7
|
+
data.tar.gz: 8d36927df5f8684188cc4e6f4ef263640429ddff9a84f9824860fe1ff1519c94fdb1b90642291681d5612478e9913ca818539a1f2c8d642657dba2d24d0f6a48
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -103,8 +103,8 @@ class API::PostsController < API::BaseController
|
|
103
103
|
def create
|
104
104
|
# use jsonapi_attribute_present? to quickly test presence of specific attributes
|
105
105
|
raise UnprocessableEntityError, 'No title!' unless jsonapi_attribute_present?(:title)
|
106
|
-
# use
|
107
|
-
@title =
|
106
|
+
# use jsonapi_attribute to get attribute values. If non-existent, nil would be returned
|
107
|
+
@title = jsonapi_attribute(:title)
|
108
108
|
# exclude 'author' attribute from assign params, for example because it's a separate table on the DB level)
|
109
109
|
@author_name = jsonapi_exclude_attribute(:author_name)
|
110
110
|
# exclude 'comments' relationship from assign params, for example because we want to filter which ones are added to post
|
@@ -42,12 +42,29 @@ module JsonApiable
|
|
42
42
|
yield(configuration)
|
43
43
|
end
|
44
44
|
|
45
|
+
def jsonapi_attribute(attrib_key)
|
46
|
+
jsonapi_build_params.dig(:data, :attributes, attrib_key)
|
47
|
+
end
|
48
|
+
|
45
49
|
def jsonapi_attribute_present?(attrib_key)
|
46
|
-
|
50
|
+
jsonapi_attribute(attrib_key).present?
|
47
51
|
end
|
48
52
|
|
49
|
-
def
|
50
|
-
jsonapi_build_params.dig(:data, :
|
53
|
+
def jsonapi_relationship(attrib_key)
|
54
|
+
jsonapi_build_params.dig(:data, :relationships, attrib_key)
|
55
|
+
end
|
56
|
+
|
57
|
+
def jsonapi_relationship_data(attrib_key)
|
58
|
+
jsonapi_build_params.dig(:data, :relationships, attrib_key, :data)
|
59
|
+
end
|
60
|
+
|
61
|
+
def jsonapi_relationship_present?(attrib_key)
|
62
|
+
jsonapi_relationship(attrib_key).present?
|
63
|
+
end
|
64
|
+
|
65
|
+
def jsonapi_relationship_attribute(relationship, attribute)
|
66
|
+
[:id, :type].include?(attribute.to_sym) ? jsonapi_relationship_data(relationship)&.dig(attribute) :
|
67
|
+
jsonapi_relationship_data(relationship)&.dig(:attributes, attribute)
|
51
68
|
end
|
52
69
|
|
53
70
|
def jsonapi_assign_params
|
@@ -71,7 +88,8 @@ module JsonApiable
|
|
71
88
|
jsonapi_build_params.dig(:data, :relationships, rel_key)
|
72
89
|
end
|
73
90
|
|
74
|
-
# Should be overwritten in specific controllers
|
91
|
+
# Should be overwritten in specific controllers. If you need to manipulate params before they are parsed,
|
92
|
+
# that's the place to do it
|
75
93
|
def jsonapi_build_params
|
76
94
|
params
|
77
95
|
end
|
data/lib/json_apiable/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_apiable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Polischuk
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|