jsonapi-serializer-formats 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jsonapi-serializer-formats.rb +29 -18
- 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: 46af0de9ebc9db137bf79ec1782170647c0644178a7680278a5427b6d5ea4e31
|
4
|
+
data.tar.gz: 8a1698007a3790c49e9952778b40fffab279fb45b141cfad8daa3f422441bdab
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f4dd9574fb1eb90413e69bb044437a8a564f3ca4e4cc5167ee9285e50dd3c09c81516f08e67f6ada4494bbc7e55b02167543e30a88c1366cc5d74a8a8168dcd
|
7
|
+
data.tar.gz: bdaeb23994576e785d90e09b0e38b24f8fa1982b4d2fe34157834bdba267cb8872a04747e0ef260c9badb715192e52a5aa16470c1c4e906bcd21a21b1d40e77b
|
@@ -6,7 +6,11 @@ module JSONAPI
|
|
6
6
|
class << self
|
7
7
|
|
8
8
|
def scoped_formats
|
9
|
-
|
9
|
+
@scoped_formats ||= []
|
10
|
+
end
|
11
|
+
|
12
|
+
def formats_per_attr
|
13
|
+
@formats_per_attr ||= {}
|
10
14
|
end
|
11
15
|
|
12
16
|
# --- Override the attribute and relationship methods to support contexts
|
@@ -23,23 +27,30 @@ module JSONAPI
|
|
23
27
|
alias_method original_method_name, method_name
|
24
28
|
|
25
29
|
define_method(method_name) do |*attributes_list, &block|
|
26
|
-
|
27
|
-
|
28
|
-
|
30
|
+
|
31
|
+
# --- If we're not in a format blocked, pass through
|
32
|
+
|
33
|
+
return send(original_method_name, *attributes_list, &block) unless scoped_formats.length.positive?
|
29
34
|
|
30
|
-
|
35
|
+
# --- Read options hash (if present)
|
31
36
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
+
opts = attributes_list.last
|
38
|
+
unless opts.is_a?(Hash)
|
39
|
+
opts = {}
|
40
|
+
attributes_list << opts
|
41
|
+
end
|
42
|
+
|
43
|
+
user_condition = opts[:if] || Proc.new { true }
|
44
|
+
|
45
|
+
attributes_list[0..-2].each do |field|
|
46
|
+
|
47
|
+
(formats_per_attr[field] ||= []) << [*scoped_formats]
|
37
48
|
|
38
49
|
# --- Inject an :if condition
|
39
50
|
|
40
|
-
|
41
|
-
|
42
|
-
if
|
51
|
+
field_opts = opts.dup
|
52
|
+
field_opts[:if] = Proc.new do |_, params = {}|
|
53
|
+
if !user_condition.call(_, params)
|
43
54
|
next false # --- The user's condition failed
|
44
55
|
end
|
45
56
|
|
@@ -47,13 +58,13 @@ module JSONAPI
|
|
47
58
|
|
48
59
|
# --- Return true if the user passed the require formats as params
|
49
60
|
|
50
|
-
|
61
|
+
formats_per_attr[field].any? do |formats|
|
62
|
+
formats.all? { |f| render_formats.include?(f) }
|
63
|
+
end
|
51
64
|
end
|
52
|
-
end
|
53
|
-
|
54
|
-
# --- Call the original method
|
55
65
|
|
56
|
-
|
66
|
+
send(original_method_name, *[field, field_opts], &block)
|
67
|
+
end
|
57
68
|
end
|
58
69
|
end
|
59
70
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-serializer-formats
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Rabier
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jsonapi-serializer
|