render_json_rails 0.1.7 → 0.2.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/.gitignore +1 -0
- data/.rubocop.yml +4119 -0
- data/Gemfile +1 -1
- data/README.md +24 -4
- data/Rakefile +1 -1
- data/lib/render_json_rails/concern.rb +44 -29
- data/lib/render_json_rails/helper.rb +28 -13
- data/lib/render_json_rails/version.rb +1 -1
- data/render_json_rails.gemspec +1 -1
- metadata +7 -6
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -24,9 +24,17 @@ class User < ActiveRecord::Base
|
|
24
24
|
include RenderJsonRails::Concern
|
25
25
|
|
26
26
|
render_json_config name: :user,
|
27
|
+
except: [:account_id, :id],
|
28
|
+
# only: [:login, :email], # jesli wolelibyśmy wymienić pola zamiast je wykluczać przy pomocy "except"
|
29
|
+
default_fields: [:login, :email, :calculated_age],
|
30
|
+
allowed_methods: [:calculated_age],
|
27
31
|
includes: {
|
28
32
|
team: Team
|
29
33
|
}
|
34
|
+
|
35
|
+
def calculated_age
|
36
|
+
rand(100)
|
37
|
+
end
|
30
38
|
end
|
31
39
|
```
|
32
40
|
|
@@ -62,14 +70,26 @@ i możemy łączyć to z include
|
|
62
70
|
http://example.text/teams/1.json?fields[team]=name,description&fields[user]=email,name&include=users
|
63
71
|
```
|
64
72
|
|
65
|
-
|
73
|
+
include mogą być zagnieżdżane (po kropce)
|
74
|
+
|
75
|
+
```html
|
76
|
+
http://example.text/teams/1.json?fields[team]=name,description&fields[user]=email,name&fields[role]=name&include=users,users.roles
|
77
|
+
```
|
78
|
+
|
79
|
+
## Wiecej przykładów użycia
|
80
|
+
|
81
|
+
Więcej przykładów jest w testach: [test/render_json_rails_test.rb](test/render_json_rails_test.rb)
|
82
|
+
|
83
|
+
## Wszystkie opcje ```render_json_config```
|
66
84
|
|
67
85
|
```ruby
|
68
86
|
render_json_config name: :team,
|
69
87
|
except: [:account_id, :config], # tych pól nie będzie w json-ie
|
70
|
-
|
71
|
-
|
72
|
-
|
88
|
+
only: [:id, :name], # dozwolone pola będą w jsonie (wymiennie z except)
|
89
|
+
methods: [:image], # dozwolone i domyślnie wyświetlone metody, ten parametr warto uzywac tylko, gdy nie ma parametru "default_fields" - przy ustawionym "default_fields" trzeba metody wymienic w allowed_methods
|
90
|
+
default_fields: [:id, :name, :members], # domyślnie wyświetlone pola + metody
|
91
|
+
allowed_methods: [:members], # dozwolone metody, mogą być dodane przez parametr fileds np: fields[team]=id,members
|
92
|
+
includes: { # to mozna dołączać za pomoca parametru include np include=users,category,users.roles
|
73
93
|
users: Users,
|
74
94
|
category: Category
|
75
95
|
}
|
data/Rakefile
CHANGED
@@ -17,14 +17,22 @@ 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, 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
|
-
except ||= [:account_id, :agent, :ip]
|
23
|
+
# except ||= [:account_id, :agent, :ip]
|
24
24
|
|
25
25
|
options = {}
|
26
26
|
if fields && fields[name].present?
|
27
|
-
|
27
|
+
if additional_fields && additional_fields[name].present?
|
28
|
+
fields[name] += ",#{additional_fields[name]}"
|
29
|
+
end
|
30
|
+
options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except&.include?(el) }
|
31
|
+
if only.present?
|
32
|
+
options[:only] = options[:only].find_all do |el|
|
33
|
+
only.include?(el) || allowed_methods&.include?(el) || methods&.include?(el)
|
34
|
+
end
|
35
|
+
end
|
28
36
|
options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
|
29
37
|
if allowed_methods
|
30
38
|
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el) }
|
@@ -34,7 +42,12 @@ module RenderJsonRails
|
|
34
42
|
end
|
35
43
|
else
|
36
44
|
options[:except] = except
|
45
|
+
options[:only] = only if only.present?
|
37
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
|
38
51
|
end
|
39
52
|
options
|
40
53
|
end
|
@@ -43,34 +56,44 @@ module RenderJsonRails
|
|
43
56
|
@render_json_config = config
|
44
57
|
end
|
45
58
|
|
46
|
-
def render_json_options(includes: nil, fields: nil, additional_config: nil)
|
59
|
+
def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil, additional_fields: nil)
|
47
60
|
raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
|
48
61
|
|
49
|
-
|
62
|
+
if 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]
|
66
|
+
else
|
67
|
+
current_json_config = @render_json_config
|
68
|
+
end
|
69
|
+
|
70
|
+
name = current_json_config[:name].to_s
|
50
71
|
|
51
|
-
if (fields.blank? || fields[name].blank?) &&
|
72
|
+
if (fields.blank? || fields[name].blank?) && current_json_config[:default_fields].present?
|
52
73
|
fields ||= {}
|
53
|
-
fields[name] =
|
74
|
+
fields[name] = current_json_config[:default_fields].join(',')
|
54
75
|
end
|
55
76
|
|
56
77
|
options = default_json_options(
|
57
78
|
name: name,
|
58
79
|
fields: fields,
|
59
|
-
|
60
|
-
|
61
|
-
|
80
|
+
only: current_json_config[:only],
|
81
|
+
except: current_json_config[:except],
|
82
|
+
methods: current_json_config[:methods],
|
83
|
+
allowed_methods: current_json_config[:allowed_methods],
|
84
|
+
additional_fields: additional_fields
|
62
85
|
)
|
63
86
|
|
64
87
|
if includes
|
65
88
|
include_options = []
|
66
|
-
|
67
|
-
if includes.include?(
|
68
|
-
includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model:
|
69
|
-
include_options << {
|
89
|
+
current_json_config[:includes]&.each do |model_name, klass|
|
90
|
+
if includes.include?(model_name.to_s)
|
91
|
+
includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: model_name.to_s)
|
92
|
+
include_options << { model_name => klass.render_json_options(includes: includes2, fields: fields) }
|
70
93
|
end
|
71
|
-
end
|
94
|
+
end
|
72
95
|
|
73
|
-
options[:include] = include_options
|
96
|
+
options[:include] = include_options if include_options.present?
|
74
97
|
end
|
75
98
|
|
76
99
|
options = RenderJsonRails::Concern.deep_meld(options, additional_config) if additional_config
|
@@ -82,23 +105,15 @@ module RenderJsonRails
|
|
82
105
|
end # class_methods
|
83
106
|
|
84
107
|
def self.includes_for_model(includes:, model:)
|
85
|
-
includes = includes.map
|
86
|
-
if el.start_with?(model + '.')
|
87
|
-
el = el.gsub(/^#{model}\./, '')
|
88
|
-
else
|
89
|
-
el = nil
|
90
|
-
end
|
91
|
-
end
|
108
|
+
includes = includes.map { |el| el.gsub(/^#{model}\./, '') if el.start_with?(model + '.') }
|
92
109
|
includes.find_all { |el| el.present? }
|
93
110
|
end
|
94
111
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
h1.deep_merge(h2) do |key, this_val, other_val|
|
99
|
-
if this_val != nil && other_val == nil
|
112
|
+
def self.deep_meld(hh1, hh2)
|
113
|
+
hh1.deep_merge(hh2) do |_key, this_val, other_val|
|
114
|
+
if !this_val.nil? && other_val == nil
|
100
115
|
this_val
|
101
|
-
elsif this_val == nil && other_val
|
116
|
+
elsif this_val == nil && !other_val.nil?
|
102
117
|
other_val
|
103
118
|
elsif this_val.is_a?(Array) && other_val.is_a?(Array)
|
104
119
|
this_val | other_val
|
@@ -17,22 +17,21 @@ module RenderJsonRails
|
|
17
17
|
# fields[invoice]=number,sales_code
|
18
18
|
# fields[invoice_position]=price_gross
|
19
19
|
# include=positions
|
20
|
-
def render_json(object, additional_config: nil, status: nil, location: nil)
|
21
|
-
raise "objekt nie moze byc null" if object == nil
|
20
|
+
def render_json(object, override_render_json_config: nil, additional_config: nil, status: nil, location: 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
|
-
additional_config: additional_config
|
35
|
-
)
|
34
|
+
|
36
35
|
if params[:formatted] && !Rails.env.development? || params[:formatted] != 'no' && Rails.env.development?
|
37
36
|
json = JSON.pretty_generate(object.as_json(options))
|
38
37
|
render json: json, status: status, location: location
|
@@ -43,5 +42,21 @@ module RenderJsonRails
|
|
43
42
|
render options
|
44
43
|
end
|
45
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
|
+
|
46
61
|
end
|
47
62
|
end
|
data/render_json_rails.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
22
22
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
-
spec.files = Dir.chdir(File.expand_path(
|
23
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
24
24
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
25
|
end
|
26
26
|
spec.bindir = "exe"
|
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.
|
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
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
49
50
|
- Gemfile
|
50
51
|
- LICENSE.txt
|
51
52
|
- README.md
|
@@ -61,7 +62,7 @@ homepage: https://github.com/intum/render_json_rails
|
|
61
62
|
licenses:
|
62
63
|
- MIT
|
63
64
|
metadata: {}
|
64
|
-
post_install_message:
|
65
|
+
post_install_message:
|
65
66
|
rdoc_options: []
|
66
67
|
require_paths:
|
67
68
|
- lib
|
@@ -76,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
77
|
- !ruby/object:Gem::Version
|
77
78
|
version: '0'
|
78
79
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
80
|
-
signing_key:
|
80
|
+
rubygems_version: 3.2.3
|
81
|
+
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: Simle JSON API render like JonApi
|
83
84
|
test_files: []
|