render_json_rails 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/render_json_rails/concern.rb +14 -4
- data/lib/render_json_rails/helper.rb +27 -13
- data/lib/render_json_rails/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 953bda8c189fb384794868cd8c587ae2ff5d77010ef8afc2f1774e0627909aee
|
4
|
+
data.tar.gz: f1dcce70063189b906bc054b862b224a7dcbd8e3ec1b924d54d5cc45d039115e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8cc2b78ed7487a8c927eabb35562ac455c64bb03499aaca69fa9755f2a3ea0ca72fe0ca77e48d365cdd729bf1da28c94439dd56b220cf4223e4c22ca02fc625c
|
7
|
+
data.tar.gz: 78fd5b3917b300d660707b5ab108478428359a43fc8f5f698fceb5ed5c3ca5f54a9468ec1904e91e73df49860c37231c823101904d5a54d392388bb9c242a176
|
@@ -17,13 +17,16 @@ module RenderJsonRails
|
|
17
17
|
# zostaną one wyświelone w json-ie
|
18
18
|
# TODO:
|
19
19
|
# [ ] spradzanie czy parametry "fields" i "include" sa ok i jesli nie to error
|
20
|
-
def default_json_options(name:, fields: nil, only: nil, except: nil, methods: nil, allowed_methods: nil)
|
20
|
+
def default_json_options(name:, fields: nil, only: nil, except: nil, methods: nil, allowed_methods: nil, additional_fields: nil)
|
21
21
|
# name ||= self.name.underscore.gsub('/', '_')
|
22
22
|
# raise self.name.underscore.gsub('/', '_')
|
23
23
|
# except ||= [:account_id, :agent, :ip]
|
24
24
|
|
25
25
|
options = {}
|
26
26
|
if fields && fields[name].present?
|
27
|
+
if additional_fields && additional_fields[name].present?
|
28
|
+
fields[name] += ",#{additional_fields[name]}"
|
29
|
+
end
|
27
30
|
options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except&.include?(el) }
|
28
31
|
if only.present?
|
29
32
|
options[:only] = options[:only].find_all do |el|
|
@@ -41,6 +44,10 @@ module RenderJsonRails
|
|
41
44
|
options[:except] = except
|
42
45
|
options[:only] = only if only.present?
|
43
46
|
options[:methods] = methods
|
47
|
+
if additional_fields && additional_fields[name].present? && allowed_methods
|
48
|
+
additional_methods = additional_fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| allowed_methods.include?(el) }
|
49
|
+
options[:methods] = (options[:methods] || []) | additional_methods
|
50
|
+
end
|
44
51
|
end
|
45
52
|
options
|
46
53
|
end
|
@@ -49,11 +56,13 @@ module RenderJsonRails
|
|
49
56
|
@render_json_config = config
|
50
57
|
end
|
51
58
|
|
52
|
-
def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil)
|
59
|
+
def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil, additional_fields: nil)
|
53
60
|
raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
|
54
61
|
|
55
62
|
if override_render_json_config
|
56
|
-
current_json_config = @render_json_config.merge(override_render_json_config)
|
63
|
+
current_json_config = override_render_json_config # @render_json_config.merge(override_render_json_config)
|
64
|
+
current_json_config[:name] ||= @render_json_config[:name]
|
65
|
+
current_json_config[:default_fields] ||= @render_json_config[:default_fields]
|
57
66
|
else
|
58
67
|
current_json_config = @render_json_config
|
59
68
|
end
|
@@ -71,7 +80,8 @@ module RenderJsonRails
|
|
71
80
|
only: current_json_config[:only],
|
72
81
|
except: current_json_config[:except],
|
73
82
|
methods: current_json_config[:methods],
|
74
|
-
allowed_methods: current_json_config[:allowed_methods]
|
83
|
+
allowed_methods: current_json_config[:allowed_methods],
|
84
|
+
additional_fields: additional_fields
|
75
85
|
)
|
76
86
|
|
77
87
|
if includes
|
@@ -18,22 +18,20 @@ module RenderJsonRails
|
|
18
18
|
# fields[invoice_position]=price_gross
|
19
19
|
# include=positions
|
20
20
|
def render_json(object, override_render_json_config: nil, additional_config: nil, status: nil, location: nil)
|
21
|
-
raise "objekt nie moze byc null" if object == nil
|
22
21
|
|
23
|
-
if
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
if (class_object = RenderJsonRails::Helper.find_render_json_options_class!(object))
|
23
|
+
includes = params[:include].to_s.split(',').map { |el| el.to_s.strip } if params[:include]
|
24
|
+
options = class_object.render_json_options(
|
25
|
+
includes: includes,
|
26
|
+
fields: params[:fields],
|
27
|
+
override_render_json_config: override_render_json_config,
|
28
|
+
additional_config: additional_config,
|
29
|
+
additional_fields: params[:additional_fields]
|
30
|
+
)
|
27
31
|
else
|
28
|
-
|
32
|
+
options = {}
|
29
33
|
end
|
30
|
-
|
31
|
-
options = class_object.render_json_options(
|
32
|
-
includes: includes,
|
33
|
-
fields: params[:fields],
|
34
|
-
override_render_json_config: override_render_json_config,
|
35
|
-
additional_config: additional_config
|
36
|
-
)
|
34
|
+
|
37
35
|
if params[:formatted] && !Rails.env.development? || params[:formatted] != 'no' && Rails.env.development?
|
38
36
|
json = JSON.pretty_generate(object.as_json(options))
|
39
37
|
render json: json, status: status, location: location
|
@@ -44,5 +42,21 @@ module RenderJsonRails
|
|
44
42
|
render options
|
45
43
|
end
|
46
44
|
end
|
45
|
+
|
46
|
+
def self.find_render_json_options_class!(object)
|
47
|
+
return nil if object == nil
|
48
|
+
|
49
|
+
if object.class.respond_to?(:render_json_options)
|
50
|
+
object.class
|
51
|
+
# elsif object.is_a?(ActiveRecord::Base)
|
52
|
+
# raise "klasa: #{object.class} nie ma konfiguracji 'render_json_config'"
|
53
|
+
elsif object.respond_to?(:first)
|
54
|
+
RenderJsonRails::Helper.find_render_json_options_class!(object[0])
|
55
|
+
else
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
47
61
|
end
|
48
62
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: render_json_rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -62,7 +62,7 @@ homepage: https://github.com/intum/render_json_rails
|
|
62
62
|
licenses:
|
63
63
|
- MIT
|
64
64
|
metadata: {}
|
65
|
-
post_install_message:
|
65
|
+
post_install_message:
|
66
66
|
rdoc_options: []
|
67
67
|
require_paths:
|
68
68
|
- lib
|
@@ -77,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
77
|
- !ruby/object:Gem::Version
|
78
78
|
version: '0'
|
79
79
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
81
|
-
signing_key:
|
80
|
+
rubygems_version: 3.2.3
|
81
|
+
signing_key:
|
82
82
|
specification_version: 4
|
83
83
|
summary: Simle JSON API render like JonApi
|
84
84
|
test_files: []
|