json_api_toolbox 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/enums.rb +16 -0
- data/lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb +3 -1
- data/lib/json_api_toolbox/version.rb +1 -1
- data/lib/json_api_toolbox.rb +3 -33
- data/lib/postable.rb +26 -0
- data/lib/renderizable.rb +1 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2121491b78188c63d80c9d89fc39ad0198aaaede
|
4
|
+
data.tar.gz: 4f8d1c1deb6cc68e490bc69f2886c95430a5f8ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94e0a7fdbf2eb3913d23521b08d30d7c9ca1019924bf9c4b04164372ad198483749830bad5004a112ea1d2ce11e7c6783bb8d8c9b267c5847675d31add6ba53a
|
7
|
+
data.tar.gz: c02b2a1df450aa008e0212400954ae3a93046d5b9c3a7b9b6f407cdedcb8575dcc0c70b1189f11224e509b27b0ecb13ac4c122259073ca7e44358de32f2f66aa
|
data/lib/enums.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonApiToolbox
|
4
|
+
module Enums
|
5
|
+
def self.build_enum(model, enum_attribute)
|
6
|
+
enum_values = model.send(enum_attribute)
|
7
|
+
enum_values.map do |attribute|
|
8
|
+
{
|
9
|
+
id: attribute[1],
|
10
|
+
description: attribute[0],
|
11
|
+
type: enum_attribute
|
12
|
+
}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -27,15 +27,17 @@ module JsonApiToolbox
|
|
27
27
|
RSpec.shared_examples 'a get with jsonapi with default value of' do |model|
|
28
28
|
let(:attributes) { data['attributes'].keys }
|
29
29
|
let(:model_keys) do
|
30
|
+
enums = []
|
30
31
|
model.new.attributes.keys.map do |attr|
|
31
32
|
if attr.match?(/_cd$/)
|
33
|
+
enums << attr
|
32
34
|
attr.gsub(/_cd/, '')
|
33
35
|
elsif attr.match?(/_id$|^id$/)
|
34
36
|
nil
|
35
37
|
else
|
36
38
|
attr
|
37
39
|
end
|
38
|
-
end.compact
|
40
|
+
end.compact + enums
|
39
41
|
end
|
40
42
|
|
41
43
|
it 'returns response with default values' do
|
data/lib/json_api_toolbox.rb
CHANGED
@@ -1,37 +1,7 @@
|
|
1
1
|
require 'active_support/inflector'
|
2
2
|
require_relative 'json_api_toolbox/spec_support/shared_examples_for_controllers'
|
3
3
|
require 'renderizable'
|
4
|
+
require 'enums'
|
5
|
+
require 'postable'
|
4
6
|
|
5
|
-
module JsonApiToolbox
|
6
|
-
def self.build_enum(model, enum_attribute)
|
7
|
-
enum_values = model.send(enum_attribute)
|
8
|
-
enum_values.map do |attribute|
|
9
|
-
{
|
10
|
-
id: attribute[1],
|
11
|
-
description: attribute[0],
|
12
|
-
type: enum_attribute
|
13
|
-
}
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.normalize_post(hash, model)
|
18
|
-
associations = model.reflect_on_all_associations.map(&:name)
|
19
|
-
normalized_hash = hash.map do |key, value|
|
20
|
-
child_model = self.association_class(model, key) if value.is_a? Hash
|
21
|
-
key = associations.include?(key.to_sym) ? "#{key}_attributes" : key
|
22
|
-
[key, value.is_a?(Hash) ? self.normalize_post(value, child_model) : value]
|
23
|
-
end
|
24
|
-
Hash[normalized_hash]
|
25
|
-
end
|
26
|
-
|
27
|
-
private
|
28
|
-
|
29
|
-
def self.association_class(model, key)
|
30
|
-
association = model.reflect_on_all_associations.find do |association|
|
31
|
-
association.name.to_s == key.to_s
|
32
|
-
end
|
33
|
-
return false unless association
|
34
|
-
class_name = (association.options.symbolize_keys[:class_name] || key)
|
35
|
-
ActiveSupport::Inflector.singularize(class_name).camelize.constantize
|
36
|
-
end
|
37
|
-
end
|
7
|
+
module JsonApiToolbox; end
|
data/lib/postable.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JsonApiToolbox
|
4
|
+
module Postable
|
5
|
+
def self.normalize_post(hash, model)
|
6
|
+
associations = model.reflect_on_all_associations.map(&:name)
|
7
|
+
normalized_hash = hash.map do |key, value|
|
8
|
+
child_model = self.association_class(model, key) if value.is_a? Hash
|
9
|
+
key = associations.include?(key.to_sym) ? "#{key}_attributes" : key
|
10
|
+
[key, value.is_a?(Hash) ? self.normalize_post(value, child_model) : value]
|
11
|
+
end
|
12
|
+
Hash[normalized_hash]
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def self.association_class(model, key)
|
18
|
+
association = model.reflect_on_all_associations.find do |association|
|
19
|
+
association.name.to_s == key.to_s
|
20
|
+
end
|
21
|
+
return false unless association
|
22
|
+
class_name = (association.options.symbolize_keys[:class_name] || key)
|
23
|
+
ActiveSupport::Inflector.singularize(class_name).camelize.constantize
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/renderizable.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
-
require 'pry'
|
3
2
|
|
4
3
|
module JsonApiToolbox
|
5
4
|
module Renderizable
|
@@ -28,7 +27,7 @@ module JsonApiToolbox
|
|
28
27
|
def permitted_params
|
29
28
|
model = controller_name.singularize
|
30
29
|
parsed_params = params.require(model).permit(permitted_attributes).to_h
|
31
|
-
JsonApiToolbox.normalize_post(parsed_params, model_name)
|
30
|
+
JsonApiToolbox::Postable.normalize_post(parsed_params, model_name)
|
32
31
|
end
|
33
32
|
|
34
33
|
def default_includes_params
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Adriano Bacha
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -137,9 +137,11 @@ files:
|
|
137
137
|
- README.md
|
138
138
|
- Rakefile
|
139
139
|
- json_api_toolbox.gemspec
|
140
|
+
- lib/enums.rb
|
140
141
|
- lib/json_api_toolbox.rb
|
141
142
|
- lib/json_api_toolbox/spec_support/shared_examples_for_controllers.rb
|
142
143
|
- lib/json_api_toolbox/version.rb
|
144
|
+
- lib/postable.rb
|
143
145
|
- lib/renderizable.rb
|
144
146
|
homepage: http://bitbucket.org/guideinvestimentos/json_api_toolbox
|
145
147
|
licenses: []
|