render_json_rails 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/lib/render_json_rails/concern.rb +8 -3
- data/lib/render_json_rails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eb405a241676a116ccdac4592f94dc5c594ecc9e2ef57e143abac35a0c16a8da
|
4
|
+
data.tar.gz: c5b19f1919ded93a0284f7840318db2e3ed6348398b564a52ff10e66eb2289ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 942b07e300001cf70c9295e0dff798277f4b846871acedc9c255109c314983d3882cf34a47be445f6181675d1897de65502af8c07e7bf14066fba30b4632c560
|
7
|
+
data.tar.gz: 3d2e700f497789a001860b17794f5441598237f28dbe4178fd021dcca65a448eb3f9786d799224639943cb02293b0644f62e7870c5fb556603ab889b0d7d4ecc
|
data/README.md
CHANGED
@@ -24,9 +24,16 @@ class User < ActiveRecord::Base
|
|
24
24
|
include RenderJsonRails::Concern
|
25
25
|
|
26
26
|
render_json_config name: :user,
|
27
|
+
except: [:account_id, :id],
|
28
|
+
default_fields: [:login, :email, :calculated_age],
|
29
|
+
allowed_methods: [:calculated_age],
|
27
30
|
includes: {
|
28
31
|
team: Team
|
29
32
|
}
|
33
|
+
|
34
|
+
def calculated_age
|
35
|
+
rand(100)
|
36
|
+
end
|
30
37
|
end
|
31
38
|
```
|
32
39
|
|
@@ -67,7 +74,9 @@ http://example.text/teams/1.json?fields[team]=name,description&fields[user]=emai
|
|
67
74
|
```ruby
|
68
75
|
render_json_config name: :team,
|
69
76
|
except: [:account_id, :config], # tych pól nie będzie w json-ie
|
70
|
-
|
77
|
+
only: [:id, :name], # tylko te pola będą w jsonie (wymiennie z except)
|
78
|
+
default_fields: [:id, :name, :members], # domyślnie wyświetlone pola + metody
|
79
|
+
methods: [:image], # ten parametr warto uzywac tylko, gdy nie ma parametru "default_fields" - przy ustawionym "default_fields" trzeba metody wymienic w allowed_methods
|
71
80
|
allowed_methods: [:members], # te metody mogą być dodane przez parametr fileds np: fields[team]=id,members
|
72
81
|
includes: { # to mozna dołączać za pomoca parametru include np include=users,category
|
73
82
|
users: Users,
|
@@ -17,14 +17,17 @@ 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(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except
|
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 { |el| only.include?(el) || allowed_methods&.include?(el) || methods&.include?(el) }
|
30
|
+
end
|
28
31
|
options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
|
29
32
|
if allowed_methods
|
30
33
|
options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el) }
|
@@ -34,6 +37,7 @@ module RenderJsonRails
|
|
34
37
|
end
|
35
38
|
else
|
36
39
|
options[:except] = except
|
40
|
+
options[:only] = only if only.present?
|
37
41
|
options[:methods] = methods
|
38
42
|
end
|
39
43
|
options
|
@@ -56,6 +60,7 @@ module RenderJsonRails
|
|
56
60
|
options = default_json_options(
|
57
61
|
name: name,
|
58
62
|
fields: fields,
|
63
|
+
only: @render_json_config[:only],
|
59
64
|
except: @render_json_config[:except],
|
60
65
|
methods: @render_json_config[:methods],
|
61
66
|
allowed_methods: @render_json_config[:allowed_methods]
|