render_json_rails 0.1.6 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +4119 -0
- data/Gemfile +1 -1
- data/README.md +40 -13
- data/Rakefile +3 -1
- data/lib/render_json_rails/concern.rb +47 -29
- data/lib/render_json_rails/helper.rb +2 -1
- data/lib/render_json_rails/version.rb +1 -1
- data/render_json_rails.gemspec +3 -1
- metadata +32 -3
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -9,9 +9,9 @@ Dzięki temu łatwo jest stworzyć backend Json API np. do pracy z Reactem lub V
|
|
9
9
|
|
10
10
|
class Team < ActiveRecord::Base
|
11
11
|
has_many :users
|
12
|
-
|
12
|
+
|
13
13
|
include RenderJsonRails::Concern
|
14
|
-
|
14
|
+
|
15
15
|
render_json_config name: :team,
|
16
16
|
includes: {
|
17
17
|
users: User
|
@@ -20,13 +20,21 @@ end
|
|
20
20
|
|
21
21
|
class User < ActiveRecord::Base
|
22
22
|
belongs_to :team
|
23
|
-
|
23
|
+
|
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
|
|
@@ -34,7 +42,7 @@ Dodajemy też w kontrolerze ```teams_controller.rb```
|
|
34
42
|
|
35
43
|
```ruby
|
36
44
|
include RenderJsonRails::Helper
|
37
|
-
|
45
|
+
|
38
46
|
def index
|
39
47
|
@team = Team.all
|
40
48
|
respond_to do |format|
|
@@ -42,8 +50,8 @@ Dodajemy też w kontrolerze ```teams_controller.rb```
|
|
42
50
|
format.json { render_json @team }
|
43
51
|
end
|
44
52
|
end
|
45
|
-
```
|
46
|
-
|
53
|
+
```
|
54
|
+
|
47
55
|
i możemy już otrzymać JSON team-u wraz z userami
|
48
56
|
|
49
57
|
```html
|
@@ -62,16 +70,28 @@ 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
|
-
render_json_config name: :team,
|
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
|
}
|
76
96
|
```
|
77
97
|
|
@@ -91,6 +111,13 @@ Or install it yourself as:
|
|
91
111
|
|
92
112
|
$ gem install render_json_rails
|
93
113
|
|
114
|
+
## Tests
|
115
|
+
|
116
|
+
```
|
117
|
+
rake test
|
118
|
+
```
|
119
|
+
|
120
|
+
|
94
121
|
## Contributing
|
95
122
|
|
96
123
|
Bug reports and pull requests are welcome on GitHub at https://github.com/intum/render_json_rails.
|
data/Rakefile
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require "rake/testtask"
|
3
|
+
# require 'active_support/core_ext/object/blank'
|
4
|
+
# require 'active_support'
|
3
5
|
|
4
6
|
Rake::TestTask.new(:test) do |t|
|
5
7
|
t.libs << "test"
|
@@ -7,4 +9,4 @@ Rake::TestTask.new(:test) do |t|
|
|
7
9
|
t.test_files = FileList["test/**/*_test.rb"]
|
8
10
|
end
|
9
11
|
|
10
|
-
task :
|
12
|
+
task default: :test
|
@@ -17,20 +17,29 @@ 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)
|
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
|
-
options[:only] = fields[name].split(',').find_all { |el| !except
|
28
|
-
|
27
|
+
options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except&.include?(el) }
|
28
|
+
if only.present?
|
29
|
+
options[:only] = options[:only].find_all do |el|
|
30
|
+
only.include?(el) || allowed_methods&.include?(el) || methods&.include?(el)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
|
29
34
|
if allowed_methods
|
30
|
-
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el
|
35
|
+
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el) }
|
36
|
+
end
|
37
|
+
if options[:methods].present? && options[:only].present?
|
38
|
+
options[:methods].each { |method| options[:only].delete(method) }
|
31
39
|
end
|
32
40
|
else
|
33
41
|
options[:except] = except
|
42
|
+
options[:only] = only if only.present?
|
34
43
|
options[:methods] = methods
|
35
44
|
end
|
36
45
|
options
|
@@ -40,52 +49,61 @@ module RenderJsonRails
|
|
40
49
|
@render_json_config = config
|
41
50
|
end
|
42
51
|
|
43
|
-
def render_json_options(includes: nil, fields: nil, additional_config: nil)
|
52
|
+
def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil)
|
44
53
|
raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
|
45
54
|
|
55
|
+
if override_render_json_config
|
56
|
+
current_json_config = @render_json_config.merge(override_render_json_config)
|
57
|
+
else
|
58
|
+
current_json_config = @render_json_config
|
59
|
+
end
|
60
|
+
|
61
|
+
name = current_json_config[:name].to_s
|
62
|
+
|
63
|
+
if (fields.blank? || fields[name].blank?) && current_json_config[:default_fields].present?
|
64
|
+
fields ||= {}
|
65
|
+
fields[name] = current_json_config[:default_fields].join(',')
|
66
|
+
end
|
67
|
+
|
46
68
|
options = default_json_options(
|
47
|
-
name:
|
69
|
+
name: name,
|
48
70
|
fields: fields,
|
49
|
-
|
50
|
-
|
51
|
-
|
71
|
+
only: current_json_config[:only],
|
72
|
+
except: current_json_config[:except],
|
73
|
+
methods: current_json_config[:methods],
|
74
|
+
allowed_methods: current_json_config[:allowed_methods]
|
52
75
|
)
|
53
76
|
|
54
77
|
if includes
|
55
78
|
include_options = []
|
56
|
-
|
57
|
-
if includes.include?(
|
58
|
-
includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model:
|
59
|
-
include_options << {
|
79
|
+
current_json_config[:includes]&.each do |model_name, klass|
|
80
|
+
if includes.include?(model_name.to_s)
|
81
|
+
includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: model_name.to_s)
|
82
|
+
include_options << { model_name => klass.render_json_options(includes: includes2, fields: fields) }
|
60
83
|
end
|
61
|
-
end
|
84
|
+
end
|
62
85
|
|
63
|
-
options[:include] = include_options
|
86
|
+
options[:include] = include_options if include_options.present?
|
64
87
|
end
|
65
88
|
|
66
89
|
options = RenderJsonRails::Concern.deep_meld(options, additional_config) if additional_config
|
90
|
+
|
91
|
+
options.delete(:methods) if options[:methods].blank?
|
92
|
+
|
67
93
|
options
|
68
94
|
end # render_json_options
|
69
95
|
end # class_methods
|
70
96
|
|
71
97
|
def self.includes_for_model(includes:, model:)
|
72
|
-
includes = includes.map
|
73
|
-
if el.start_with?(model + '.')
|
74
|
-
el = el.gsub(/^#{model}\./, '')
|
75
|
-
else
|
76
|
-
el = nil
|
77
|
-
end
|
78
|
-
end
|
98
|
+
includes = includes.map { |el| el.gsub(/^#{model}\./, '') if el.start_with?(model + '.') }
|
79
99
|
includes.find_all { |el| el.present? }
|
80
100
|
end
|
81
101
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
h1.deep_merge(h2) do |key, this_val, other_val|
|
86
|
-
if this_val != nil && other_val == nil
|
102
|
+
def self.deep_meld(hh1, hh2)
|
103
|
+
hh1.deep_merge(hh2) do |_key, this_val, other_val|
|
104
|
+
if !this_val.nil? && other_val == nil
|
87
105
|
this_val
|
88
|
-
elsif this_val == nil && other_val
|
106
|
+
elsif this_val == nil && !other_val.nil?
|
89
107
|
other_val
|
90
108
|
elsif this_val.is_a?(Array) && other_val.is_a?(Array)
|
91
109
|
this_val | other_val
|
@@ -17,7 +17,7 @@ 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)
|
20
|
+
def render_json(object, override_render_json_config: nil, additional_config: nil, status: nil, location: nil)
|
21
21
|
raise "objekt nie moze byc null" if object == nil
|
22
22
|
|
23
23
|
if object.class.to_s.include?('ActiveRecord_Relation')
|
@@ -31,6 +31,7 @@ module RenderJsonRails
|
|
31
31
|
options = class_object.render_json_options(
|
32
32
|
includes: includes,
|
33
33
|
fields: params[:fields],
|
34
|
+
override_render_json_config: override_render_json_config,
|
34
35
|
additional_config: additional_config
|
35
36
|
)
|
36
37
|
if params[:formatted] && !Rails.env.development? || params[:formatted] != 'no' && Rails.env.development?
|
data/render_json_rails.gemspec
CHANGED
@@ -20,10 +20,12 @@ 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"
|
27
27
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
28
|
spec.require_paths = ["lib"]
|
29
|
+
spec.add_development_dependency "activesupport"
|
30
|
+
spec.add_development_dependency "byebug"
|
29
31
|
end
|
metadata
CHANGED
@@ -1,15 +1,43 @@
|
|
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.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcin
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
12
|
-
dependencies:
|
11
|
+
date: 2020-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
13
41
|
description: render json with 'includes' and 'fields' with simple config
|
14
42
|
email:
|
15
43
|
- marcin@radgost.com
|
@@ -18,6 +46,7 @@ extensions: []
|
|
18
46
|
extra_rdoc_files: []
|
19
47
|
files:
|
20
48
|
- ".gitignore"
|
49
|
+
- ".rubocop.yml"
|
21
50
|
- Gemfile
|
22
51
|
- LICENSE.txt
|
23
52
|
- README.md
|