render_json_rails 0.1.8 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -3,5 +3,5 @@ source "https://rubygems.org"
3
3
  # Specify your gem's dependencies in render_json_rails.gemspec
4
4
  gemspec
5
5
 
6
- gem "rake", "~> 12.0"
7
6
  gem "minitest", "~> 5.0"
7
+ gem "rake", "~> 12.0"
data/README.md CHANGED
@@ -25,6 +25,7 @@ class User < ActiveRecord::Base
25
25
 
26
26
  render_json_config name: :user,
27
27
  except: [:account_id, :id],
28
+ # only: [:login, :email], # jesli wolelibyśmy wymienić pola zamiast je wykluczać przy pomocy "except"
28
29
  default_fields: [:login, :email, :calculated_age],
29
30
  allowed_methods: [:calculated_age],
30
31
  includes: {
@@ -69,15 +70,23 @@ i możemy łączyć to z include
69
70
  http://example.text/teams/1.json?fields[team]=name,description&fields[user]=email,name&include=users
70
71
  ```
71
72
 
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
+ Więcej przykładów jest w testach: [test/render_json_rails_test.rb](test/render_json_rails_test.rb)
80
+
72
81
  ## Pełny opis ```render_json_config```
73
82
 
74
83
  ```ruby
75
84
  render_json_config name: :team,
76
85
  except: [:account_id, :config], # tych pól nie będzie w json-ie
77
- only: [:id, :name], # tylko te pola będą w jsonie (wymiennie z except)
86
+ only: [:id, :name], # dozwolone pola będą w jsonie (wymiennie z except)
87
+ 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
78
88
  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
80
- allowed_methods: [:members], # te metody mogą być dodane przez parametr fileds np: fields[team]=id,members
89
+ allowed_methods: [:members], # dozwolone metody, mogą być dodane przez parametr fileds np: fields[team]=id,members
81
90
  includes: { # to mozna dołączać za pomoca parametru include np include=users,category
82
91
  users: Users,
83
92
  category: Category
data/Rakefile CHANGED
@@ -9,4 +9,4 @@ Rake::TestTask.new(:test) do |t|
9
9
  t.test_files = FileList["test/**/*_test.rb"]
10
10
  end
11
11
 
12
- task :default => :test
12
+ task default: :test
@@ -26,7 +26,9 @@ module RenderJsonRails
26
26
  if fields && fields[name].present?
27
27
  options[:only] = fields[name].split(',').map{ |e| e.to_s.strip.to_sym }.find_all { |el| !except&.include?(el) }
28
28
  if only.present?
29
- options[:only] = options[:only].find_all { |el| only.include?(el) || allowed_methods&.include?(el) || methods&.include?(el) }
29
+ options[:only] = options[:only].find_all do |el|
30
+ only.include?(el) || allowed_methods&.include?(el) || methods&.include?(el)
31
+ end
30
32
  end
31
33
  options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
32
34
  if allowed_methods
@@ -68,14 +70,14 @@ module RenderJsonRails
68
70
 
69
71
  if includes
70
72
  include_options = []
71
- @render_json_config[:includes].each do |name, klass|
72
- if includes.include?(name.to_s)
73
- includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: name.to_s)
74
- include_options << { name => klass.render_json_options(includes: includes2, fields: fields) }
73
+ @render_json_config[:includes]&.each do |model_name, klass|
74
+ if includes.include?(model_name.to_s)
75
+ includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: model_name.to_s)
76
+ include_options << { model_name => klass.render_json_options(includes: includes2, fields: fields) }
75
77
  end
76
- end if @render_json_config[:includes]
78
+ end
77
79
 
78
- options[:include] = include_options
80
+ options[:include] = include_options if include_options.present?
79
81
  end
80
82
 
81
83
  options = RenderJsonRails::Concern.deep_meld(options, additional_config) if additional_config
@@ -87,23 +89,15 @@ module RenderJsonRails
87
89
  end # class_methods
88
90
 
89
91
  def self.includes_for_model(includes:, model:)
90
- includes = includes.map do |el|
91
- if el.start_with?(model + '.')
92
- el = el.gsub(/^#{model}\./, '')
93
- else
94
- el = nil
95
- end
96
- end
92
+ includes = includes.map { |el| el.gsub(/^#{model}\./, '') if el.start_with?(model + '.') }
97
93
  includes.find_all { |el| el.present? }
98
94
  end
99
95
 
100
- private
101
-
102
- def self.deep_meld(h1, h2)
103
- h1.deep_merge(h2) do |key, this_val, other_val|
104
- if this_val != nil && other_val == nil
96
+ def self.deep_meld(hh1, hh2)
97
+ hh1.deep_merge(hh2) do |_key, this_val, other_val|
98
+ if !this_val.nil? && other_val == nil
105
99
  this_val
106
- elsif this_val == nil && other_val != nil
100
+ elsif this_val == nil && !other_val.nil?
107
101
  other_val
108
102
  elsif this_val.is_a?(Array) && other_val.is_a?(Array)
109
103
  this_val | other_val
@@ -1,3 +1,3 @@
1
1
  module RenderJsonRails
2
- VERSION = "0.1.8"
2
+ VERSION = "0.2"
3
3
  end
@@ -20,7 +20,7 @@ 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('..', __FILE__)) do
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"
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.1.8
4
+ version: '0.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-10-20 00:00:00.000000000 Z
11
+ date: 2020-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -46,6 +46,7 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rubocop.yml"
49
50
  - Gemfile
50
51
  - LICENSE.txt
51
52
  - README.md