render_json_rails 0.1.7 → 0.1.8

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1dc319274e2045a7c895ce17f4d43cfafa0029a67becb6f54f61edb46d659d53
4
- data.tar.gz: 6cd8e4556b08a9c6aee0a610773527523e24f2533c4d0b6f2b8020ffeb062918
3
+ metadata.gz: eb405a241676a116ccdac4592f94dc5c594ecc9e2ef57e143abac35a0c16a8da
4
+ data.tar.gz: c5b19f1919ded93a0284f7840318db2e3ed6348398b564a52ff10e66eb2289ee
5
5
  SHA512:
6
- metadata.gz: 7480441de90996af3a76fec81114064ff9fc2d6a8212c70741cdf7a0f313560d34ecf06d5b89f76ec892a4edcef63c600cbde57681f041a1daf21176badbf283
7
- data.tar.gz: c22575b485b96b885c30c127f94d95f829d4bb00911f5d0c9638c97dc6d49129387836e8c837c0f6dec5eb3b932e0491729c3398f5c6afeadfa607dfaa9e66b5
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
- methods: [:image], # te metody zostaną dołączone
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.include?(el) }
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]
@@ -1,3 +1,3 @@
1
1
  module RenderJsonRails
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: render_json_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin