render_json_rails 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a80785e8f96237cbf90167c64bcd74a997d572c4aa94cea4bdd53f21e0ad402c
4
- data.tar.gz: e34fb4f9f8af8dc1ef1eb642949721f36256e60a4e646f3cdbdfffa5a042102b
3
+ metadata.gz: 953bda8c189fb384794868cd8c587ae2ff5d77010ef8afc2f1774e0627909aee
4
+ data.tar.gz: f1dcce70063189b906bc054b862b224a7dcbd8e3ec1b924d54d5cc45d039115e
5
5
  SHA512:
6
- metadata.gz: f40743ed807224c5f6b2aa2efe5ea064e91fbdef182889d308a5712d7d9764f8cc3d822198c2712a00f17accf7fecf02da90308ab7026473b1c2775560404438
7
- data.tar.gz: 38c1db738769a548b9967572760f846372906b588bdbed3f76bb87671e64038752b3f0337c0aafaa86c6825439a323cd5a1e744644c7778036e08f867f3a8d83
6
+ metadata.gz: 8cc2b78ed7487a8c927eabb35562ac455c64bb03499aaca69fa9755f2a3ea0ca72fe0ca77e48d365cdd729bf1da28c94439dd56b220cf4223e4c22ca02fc625c
7
+ data.tar.gz: 78fd5b3917b300d660707b5ab108478428359a43fc8f5f698fceb5ed5c3ca5f54a9468ec1904e91e73df49860c37231c823101904d5a54d392388bb9c242a176
@@ -17,13 +17,16 @@ 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, only: 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
23
  # except ||= [:account_id, :agent, :ip]
24
24
 
25
25
  options = {}
26
26
  if fields && fields[name].present?
27
+ if additional_fields && additional_fields[name].present?
28
+ fields[name] += ",#{additional_fields[name]}"
29
+ end
27
30
  options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except&.include?(el) }
28
31
  if only.present?
29
32
  options[:only] = options[:only].find_all do |el|
@@ -41,6 +44,10 @@ module RenderJsonRails
41
44
  options[:except] = except
42
45
  options[:only] = only if only.present?
43
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
44
51
  end
45
52
  options
46
53
  end
@@ -49,11 +56,13 @@ module RenderJsonRails
49
56
  @render_json_config = config
50
57
  end
51
58
 
52
- def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil)
59
+ def render_json_options(includes: nil, fields: nil, override_render_json_config: nil, additional_config: nil, additional_fields: nil)
53
60
  raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
54
61
 
55
62
  if override_render_json_config
56
- current_json_config = @render_json_config.merge(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]
57
66
  else
58
67
  current_json_config = @render_json_config
59
68
  end
@@ -71,7 +80,8 @@ module RenderJsonRails
71
80
  only: current_json_config[:only],
72
81
  except: current_json_config[:except],
73
82
  methods: current_json_config[:methods],
74
- allowed_methods: current_json_config[:allowed_methods]
83
+ allowed_methods: current_json_config[:allowed_methods],
84
+ additional_fields: additional_fields
75
85
  )
76
86
 
77
87
  if includes
@@ -18,22 +18,20 @@ module RenderJsonRails
18
18
  # fields[invoice_position]=price_gross
19
19
  # include=positions
20
20
  def render_json(object, override_render_json_config: nil, additional_config: nil, status: nil, location: nil)
21
- raise "objekt nie moze byc null" if object == nil
22
21
 
23
- if object.class.to_s.include?('ActiveRecord_Relation')
24
- return render json: [] if !object[0]
25
-
26
- class_object = object[0].class
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
- class_object = object.class
32
+ options = {}
29
33
  end
30
- includes = params[:include].to_s.split(',').map { |el| el.to_s.strip } if params[:include]
31
- options = class_object.render_json_options(
32
- includes: includes,
33
- fields: params[:fields],
34
- override_render_json_config: override_render_json_config,
35
- additional_config: additional_config
36
- )
34
+
37
35
  if params[:formatted] && !Rails.env.development? || params[:formatted] != 'no' && Rails.env.development?
38
36
  json = JSON.pretty_generate(object.as_json(options))
39
37
  render json: json, status: status, location: location
@@ -44,5 +42,21 @@ module RenderJsonRails
44
42
  render options
45
43
  end
46
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
+
47
61
  end
48
62
  end
@@ -1,3 +1,3 @@
1
1
  module RenderJsonRails
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
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.2.2
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: 2020-10-21 00:00:00.000000000 Z
11
+ date: 2021-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -62,7 +62,7 @@ homepage: https://github.com/intum/render_json_rails
62
62
  licenses:
63
63
  - MIT
64
64
  metadata: {}
65
- post_install_message:
65
+ post_install_message:
66
66
  rdoc_options: []
67
67
  require_paths:
68
68
  - lib
@@ -77,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  - !ruby/object:Gem::Version
78
78
  version: '0'
79
79
  requirements: []
80
- rubygems_version: 3.0.3
81
- signing_key:
80
+ rubygems_version: 3.2.3
81
+ signing_key:
82
82
  specification_version: 4
83
83
  summary: Simle JSON API render like JonApi
84
84
  test_files: []