render_json_rails 0.1.3 → 0.1.8

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: b82eabebc8a9b20c855e8f75f77fce6bf92105cd07de76f5e377a935114bb6b1
4
- data.tar.gz: 175b1a4817a38e2c768c5b8827fe0d020043721d6998d73c9c0fafe063479cbf
3
+ metadata.gz: eb405a241676a116ccdac4592f94dc5c594ecc9e2ef57e143abac35a0c16a8da
4
+ data.tar.gz: c5b19f1919ded93a0284f7840318db2e3ed6348398b564a52ff10e66eb2289ee
5
5
  SHA512:
6
- metadata.gz: 2dfaad84541f4d4e07e1dd4b16695ab06ae8345f768ac1defe288d188f3e4771a82d5d56812d3d14198d9ab1d66de84fc8138e7a5a9ff228993e680da1546c41
7
- data.tar.gz: a7d110349b9fd3e84366676f42963914cc74cd071e9af54da9181bf3761e786516405427331bea4a648c14844b085f505ec488371ad2c81c5981b8980e4aef8d
6
+ metadata.gz: 942b07e300001cf70c9295e0dff798277f4b846871acedc9c255109c314983d3882cf34a47be445f6181675d1897de65502af8c07e7bf14066fba30b4632c560
7
+ data.tar.gz: 3d2e700f497789a001860b17794f5441598237f28dbe4178fd021dcca65a448eb3f9786d799224639943cb02293b0644f62e7870c5fb556603ab889b0d7d4ecc
data/README.md CHANGED
@@ -1,51 +1,113 @@
1
1
  # RenderJsonRails
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/render_json_rails`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ RenderJsonRails pozwala w łatwy sposób dodać możliwość renderowania JSON z ActiveRecord-ów z zależnościami (has_many itp).
4
+ Dzięki temu łatwo jest stworzyć backend Json API np. do pracy z Reactem lub Vue.js
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ ## Przykład
6
7
 
7
- ## Installation
8
+ ```ruby
8
9
 
9
- Add this line to your application's Gemfile:
10
+ class Team < ActiveRecord::Base
11
+ has_many :users
10
12
 
11
- ```ruby
12
- gem 'render_json_rails', git: 'https://github.com/intum/render_json_rails'
13
+ include RenderJsonRails::Concern
14
+
15
+ render_json_config name: :team,
16
+ includes: {
17
+ users: User
18
+ }
19
+ end
20
+
21
+ class User < ActiveRecord::Base
22
+ belongs_to :team
23
+
24
+ include RenderJsonRails::Concern
13
25
 
14
- # or
26
+ render_json_config name: :user,
27
+ except: [:account_id, :id],
28
+ default_fields: [:login, :email, :calculated_age],
29
+ allowed_methods: [:calculated_age],
30
+ includes: {
31
+ team: Team
32
+ }
15
33
 
16
- source "https://rubygems.pkg.github.com/intum" do
17
- gem "render_json_rails"
34
+ def calculated_age
35
+ rand(100)
36
+ end
18
37
  end
19
38
  ```
20
39
 
21
- And then execute:
40
+ Dodajemy też w kontrolerze ```teams_controller.rb```
22
41
 
23
- $ bundle install
24
-
25
- Or install it yourself as:
42
+ ```ruby
43
+ include RenderJsonRails::Helper
44
+
45
+ def index
46
+ @team = Team.all
47
+ respond_to do |format|
48
+ format.html
49
+ format.json { render_json @team }
50
+ end
51
+ end
52
+ ```
26
53
 
27
- $ gem install render_json_rails
54
+ i możemy już otrzymać JSON team-u wraz z userami
28
55
 
29
- ## Usage
56
+ ```html
57
+ http://example.test/teams/1.json?include=users
58
+ ```
30
59
 
31
- TODO: Write usage instructions here
60
+ możemy też określić jakie pola mają być w json
32
61
 
33
- ## Development
62
+ ```html
63
+ http://example.test/teams/1.json?fields[team]=name,description
64
+ ```
34
65
 
35
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
66
+ i możemy łączyć to z include
36
67
 
37
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
68
+ ```html
69
+ http://example.text/teams/1.json?fields[team]=name,description&fields[user]=email,name&include=users
70
+ ```
38
71
 
39
- ## Contributing
72
+ ## Pełny opis ```render_json_config```
40
73
 
41
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/render_json_rails.
74
+ ```ruby
75
+ render_json_config name: :team,
76
+ 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)
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
80
+ allowed_methods: [:members], # te metody mogą być dodane przez parametr fileds np: fields[team]=id,members
81
+ includes: { # to mozna dołączać za pomoca parametru include np include=users,category
82
+ users: Users,
83
+ category: Category
84
+ }
85
+ ```
42
86
 
87
+ ## Installation
43
88
 
44
- Tworzenie gema
89
+ Add this line to your application's Gemfile:
45
90
 
91
+ ```ruby
92
+ gem 'render_json_rails'
46
93
  ```
47
- gem build render_json_rails.gemspec
48
94
 
49
- gem push --key github --host https://rubygems.pkg.github.com/intum render_json_rails-0.1.XXX.gem
95
+ And then execute:
96
+
97
+ $ bundle install
98
+
99
+ Or install it yourself as:
100
+
101
+ $ gem install render_json_rails
102
+
103
+ ## Tests
104
+
105
+ ```
106
+ rake test
50
107
  ```
51
108
 
109
+
110
+ ## Contributing
111
+
112
+ Bug reports and pull requests are welcome on GitHub at https://github.com/intum/render_json_rails.
113
+
data/Rakefile CHANGED
@@ -1,5 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require "rake/testtask"
3
+ # require 'active_support/core_ext/object/blank'
4
+ # require 'active_support'
3
5
 
4
6
  Rake::TestTask.new(:test) do |t|
5
7
  t.libs << "test"
@@ -17,20 +17,27 @@ 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(',').find_all{ |el| !except.include?(el.to_sym) }
28
- options[:methods] = methods&.find_all{ |el| options[:only].include?(el.to_s) }
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
31
+ options[:methods] = methods&.find_all { |el| options[:only].include?(el) }
29
32
  if allowed_methods
30
- options[:methods] = (options[:methods] || []) | allowed_methods.find_all{ |el| options[:only].include?(el.to_s) }
33
+ options[:methods] = (options[:methods] || []) | allowed_methods.find_all { |el| options[:only].include?(el) }
34
+ end
35
+ if options[:methods].present? && options[:only].present?
36
+ options[:methods].each { |method| options[:only].delete(method) }
31
37
  end
32
38
  else
33
39
  options[:except] = except
40
+ options[:only] = only if only.present?
34
41
  options[:methods] = methods
35
42
  end
36
43
  options
@@ -38,16 +45,22 @@ module RenderJsonRails
38
45
 
39
46
  def render_json_config(config)
40
47
  @render_json_config = config
41
- # @render_json_config[:methods] = [:image]
42
48
  end
43
49
 
44
- # rubocop:disable Lint/UnusedMethodArgument
45
50
  def render_json_options(includes: nil, fields: nil, additional_config: nil)
46
51
  raise "należy skonfigurowac render_json metodą: render_json_config" if !defined?(@render_json_config)
47
52
 
53
+ name = @render_json_config[:name].to_s
54
+
55
+ if (fields.blank? || fields[name].blank?) && @render_json_config[:default_fields].present?
56
+ fields ||= {}
57
+ fields[name] = @render_json_config[:default_fields].join(',')
58
+ end
59
+
48
60
  options = default_json_options(
49
- name: @render_json_config[:name].to_s,
61
+ name: name,
50
62
  fields: fields,
63
+ only: @render_json_config[:only],
51
64
  except: @render_json_config[:except],
52
65
  methods: @render_json_config[:methods],
53
66
  allowed_methods: @render_json_config[:allowed_methods]
@@ -58,29 +71,22 @@ module RenderJsonRails
58
71
  @render_json_config[:includes].each do |name, klass|
59
72
  if includes.include?(name.to_s)
60
73
  includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: name.to_s)
61
- # raise includes2.inspect + ' ' + includes.inspect
62
74
  include_options << { name => klass.render_json_options(includes: includes2, fields: fields) }
63
75
  end
64
76
  end if @render_json_config[:includes]
65
77
 
66
- # if includes.include?('questions')
67
- # includes2 = RenderJsonRails::Concern.includes_for_model(includes: includes, model: 'questions')
68
- # # raise includes2.inspect + ' ' + includes.inspect
69
- # include_options << { questions: Organize::Question.render_json_options(includes: includes2, fields: fields) }
70
- # end
71
78
  options[:include] = include_options
72
79
  end
73
80
 
74
- options
75
- end # render_json_options
76
- # rubocop:enable Lint/UnusedMethodArgument
81
+ options = RenderJsonRails::Concern.deep_meld(options, additional_config) if additional_config
77
82
 
83
+ options.delete(:methods) if options[:methods].blank?
78
84
 
85
+ options
86
+ end # render_json_options
79
87
  end # class_methods
80
88
 
81
89
  def self.includes_for_model(includes:, model:)
82
- # include.split(',')
83
- # includes.delete(to_remove)
84
90
  includes = includes.map do |el|
85
91
  if el.start_with?(model + '.')
86
92
  el = el.gsub(/^#{model}\./, '')
@@ -88,8 +94,25 @@ module RenderJsonRails
88
94
  el = nil
89
95
  end
90
96
  end
91
- includes.find_all{ |el| el.present? }
92
- # raise includes.to_json
97
+ includes.find_all { |el| el.present? }
98
+ end
99
+
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
105
+ this_val
106
+ elsif this_val == nil && other_val != nil
107
+ other_val
108
+ elsif this_val.is_a?(Array) && other_val.is_a?(Array)
109
+ this_val | other_val
110
+ elsif this_val.is_a?(Hash) && other_val.is_a?(Hash)
111
+ deep_meld(this_val, other_val)
112
+ else
113
+ [this_val, other_val]
114
+ end
115
+ end
93
116
  end
94
117
  end
95
- end
118
+ end
@@ -27,7 +27,7 @@ module RenderJsonRails
27
27
  else
28
28
  class_object = object.class
29
29
  end
30
- includes = params[:include].to_s.split(',').map{ |el| el.to_s.strip } if params[:include]
30
+ includes = params[:include].to_s.split(',').map { |el| el.to_s.strip } if params[:include]
31
31
  options = class_object.render_json_options(
32
32
  includes: includes,
33
33
  fields: params[:fields],
@@ -44,4 +44,4 @@ module RenderJsonRails
44
44
  end
45
45
  end
46
46
  end
47
- end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module RenderJsonRails
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -7,9 +7,9 @@ Gem::Specification.new do |spec|
7
7
  spec.authors = ["Marcin"]
8
8
  spec.email = ["marcin@radgost.com"]
9
9
 
10
- spec.summary = "Simle JSON render like JonApi"
10
+ spec.summary = "Simle JSON API render like JonApi"
11
11
  spec.description = "render json with 'includes' and 'fields' with simple config"
12
- spec.homepage = "https://radgost.com/gems/render_json_rails"
12
+ spec.homepage = "https://github.com/intum/render_json_rails"
13
13
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
14
 
15
15
  # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
@@ -20,10 +20,12 @@ 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('..', __FILE__)) 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"
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
+ spec.add_development_dependency "activesupport"
30
+ spec.add_development_dependency "byebug"
29
31
  end
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.3
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marcin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-07-01 00:00:00.000000000 Z
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
@@ -29,7 +57,7 @@ files:
29
57
  - lib/render_json_rails/helper.rb
30
58
  - lib/render_json_rails/version.rb
31
59
  - render_json_rails.gemspec
32
- homepage: https://radgost.com/gems/render_json_rails
60
+ homepage: https://github.com/intum/render_json_rails
33
61
  licenses:
34
62
  - MIT
35
63
  metadata: {}
@@ -48,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
76
  - !ruby/object:Gem::Version
49
77
  version: '0'
50
78
  requirements: []
51
- rubygems_version: 3.1.2
79
+ rubygems_version: 3.0.3
52
80
  signing_key:
53
81
  specification_version: 4
54
- summary: Simle JSON render like JonApi
82
+ summary: Simle JSON API render like JonApi
55
83
  test_files: []