render_json_rails 0.1.6 → 0.1.7
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/README.md +17 -10
- data/Rakefile +2 -0
- data/lib/render_json_rails/concern.rb +17 -4
- data/lib/render_json_rails/version.rb +1 -1
- data/render_json_rails.gemspec +2 -0
- metadata +31 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1dc319274e2045a7c895ce17f4d43cfafa0029a67becb6f54f61edb46d659d53
|
|
4
|
+
data.tar.gz: 6cd8e4556b08a9c6aee0a610773527523e24f2533c4d0b6f2b8020ffeb062918
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7480441de90996af3a76fec81114064ff9fc2d6a8212c70741cdf7a0f313560d34ecf06d5b89f76ec892a4edcef63c600cbde57681f041a1daf21176badbf283
|
|
7
|
+
data.tar.gz: c22575b485b96b885c30c127f94d95f829d4bb00911f5d0c9638c97dc6d49129387836e8c837c0f6dec5eb3b932e0491729c3398f5c6afeadfa607dfaa9e66b5
|
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,9 +20,9 @@ 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
27
|
includes: {
|
|
28
28
|
team: Team
|
|
@@ -34,7 +34,7 @@ Dodajemy też w kontrolerze ```teams_controller.rb```
|
|
|
34
34
|
|
|
35
35
|
```ruby
|
|
36
36
|
include RenderJsonRails::Helper
|
|
37
|
-
|
|
37
|
+
|
|
38
38
|
def index
|
|
39
39
|
@team = Team.all
|
|
40
40
|
respond_to do |format|
|
|
@@ -42,8 +42,8 @@ Dodajemy też w kontrolerze ```teams_controller.rb```
|
|
|
42
42
|
format.json { render_json @team }
|
|
43
43
|
end
|
|
44
44
|
end
|
|
45
|
-
```
|
|
46
|
-
|
|
45
|
+
```
|
|
46
|
+
|
|
47
47
|
i możemy już otrzymać JSON team-u wraz z userami
|
|
48
48
|
|
|
49
49
|
```html
|
|
@@ -65,13 +65,13 @@ http://example.text/teams/1.json?fields[team]=name,description&fields[user]=emai
|
|
|
65
65
|
## Pełny opis ```render_json_config```
|
|
66
66
|
|
|
67
67
|
```ruby
|
|
68
|
-
render_json_config name: :team,
|
|
68
|
+
render_json_config name: :team,
|
|
69
69
|
except: [:account_id, :config], # tych pól nie będzie w json-ie
|
|
70
|
-
methods: [:image], # te metody zostaną dołączone
|
|
70
|
+
methods: [:image], # te metody zostaną dołączone
|
|
71
71
|
allowed_methods: [:members], # te metody mogą być dodane przez parametr fileds np: fields[team]=id,members
|
|
72
72
|
includes: { # to mozna dołączać za pomoca parametru include np include=users,category
|
|
73
73
|
users: Users,
|
|
74
|
-
|
|
74
|
+
category: Category
|
|
75
75
|
}
|
|
76
76
|
```
|
|
77
77
|
|
|
@@ -91,6 +91,13 @@ Or install it yourself as:
|
|
|
91
91
|
|
|
92
92
|
$ gem install render_json_rails
|
|
93
93
|
|
|
94
|
+
## Tests
|
|
95
|
+
|
|
96
|
+
```
|
|
97
|
+
rake test
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
|
|
94
101
|
## Contributing
|
|
95
102
|
|
|
96
103
|
Bug reports and pull requests are welcome on GitHub at https://github.com/intum/render_json_rails.
|
data/Rakefile
CHANGED
|
@@ -24,10 +24,13 @@ module RenderJsonRails
|
|
|
24
24
|
|
|
25
25
|
options = {}
|
|
26
26
|
if fields && fields[name].present?
|
|
27
|
-
options[:only] = fields[name].split(',').find_all { |el| !except.include?(el
|
|
28
|
-
options[:methods] = methods&.find_all { |el| options[:only].include?(el
|
|
27
|
+
options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except.include?(el) }
|
|
28
|
+
options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
|
|
29
29
|
if allowed_methods
|
|
30
|
-
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el
|
|
30
|
+
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el) }
|
|
31
|
+
end
|
|
32
|
+
if options[:methods].present? && options[:only].present?
|
|
33
|
+
options[:methods].each { |method| options[:only].delete(method) }
|
|
31
34
|
end
|
|
32
35
|
else
|
|
33
36
|
options[:except] = except
|
|
@@ -43,8 +46,15 @@ module RenderJsonRails
|
|
|
43
46
|
def render_json_options(includes: nil, fields: nil, additional_config: nil)
|
|
44
47
|
raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
|
|
45
48
|
|
|
49
|
+
name = @render_json_config[:name].to_s
|
|
50
|
+
|
|
51
|
+
if (fields.blank? || fields[name].blank?) && @render_json_config[:default_fields].present?
|
|
52
|
+
fields ||= {}
|
|
53
|
+
fields[name] = @render_json_config[:default_fields].join(',')
|
|
54
|
+
end
|
|
55
|
+
|
|
46
56
|
options = default_json_options(
|
|
47
|
-
name:
|
|
57
|
+
name: name,
|
|
48
58
|
fields: fields,
|
|
49
59
|
except: @render_json_config[:except],
|
|
50
60
|
methods: @render_json_config[:methods],
|
|
@@ -64,6 +74,9 @@ module RenderJsonRails
|
|
|
64
74
|
end
|
|
65
75
|
|
|
66
76
|
options = RenderJsonRails::Concern.deep_meld(options, additional_config) if additional_config
|
|
77
|
+
|
|
78
|
+
options.delete(:methods) if options[:methods].blank?
|
|
79
|
+
|
|
67
80
|
options
|
|
68
81
|
end # render_json_options
|
|
69
82
|
end # class_methods
|
data/render_json_rails.gemspec
CHANGED
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.1.
|
|
4
|
+
version: 0.1.7
|
|
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-20 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
|