mongoid-denormalize 0.3.0 → 0.3.1
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 +4 -4
- data/.travis.yml +5 -0
- data/Rakefile +3 -3
- data/gemfiles/mongoid-5.0.gemfile +7 -0
- data/gemfiles/mongoid-6.0.gemfile +7 -0
- data/lib/mongoid-denormalize/version.rb +1 -1
- data/lib/mongoid-denormalize.rb +27 -9
- data/mongoid-denormalize.gemspec +1 -1
- metadata +13 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 305306418eb9b932560215ef74e6aee0ea803444310dd9a36b82bc601026a268
|
4
|
+
data.tar.gz: 2ca4400f0e00dc5fa365637cd2002cd132b1c9e36f14f37f94e8716a5d4ae332
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6f245dfef2f06c8d07f472225751257f6173787ab6ee4baacccec67caee4d9fe422acd732374055d52c38eb161d2ea7725078f4406a0c8c38b8a6126fb98bacc
|
7
|
+
data.tar.gz: 2255a8d098dbf6796aa0e3de5586ec38bc34a79c317098a928d5e757c2c9c6ec21e15e7917d38203a1c0ec4adbfa19337cc40df32bf0f4bb6a13252f62cb1ffc
|
data/.travis.yml
CHANGED
data/Rakefile
CHANGED
data/lib/mongoid-denormalize.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
require
|
1
|
+
require 'mongoid-denormalize/version'
|
2
2
|
|
3
3
|
module Mongoid
|
4
4
|
module Denormalize
|
@@ -14,10 +14,10 @@ module Mongoid
|
|
14
14
|
raise ArgumentError, 'Option :from is needed (e.g. denormalize :name, from: :user).'
|
15
15
|
end
|
16
16
|
|
17
|
-
fields = Mongoid::Denormalize.get_fields_with_names(fields, options)
|
17
|
+
fields = Mongoid::Denormalize.get_fields_with_names(self, fields, options)
|
18
18
|
|
19
19
|
# Add fields to model
|
20
|
-
fields.each { |field| field field[:as] }
|
20
|
+
fields.each { |field| field field[:as], type: field[:type] }
|
21
21
|
|
22
22
|
# Add hooks
|
23
23
|
Mongoid::Denormalize.add_hook_to_child(self, fields, options)
|
@@ -26,7 +26,9 @@ module Mongoid
|
|
26
26
|
end
|
27
27
|
|
28
28
|
# Check options and return name for each field
|
29
|
-
def self.get_fields_with_names(fields, options)
|
29
|
+
def self.get_fields_with_names(child_class, fields, options)
|
30
|
+
parent = parent_class(child_class, options[:from].to_s)
|
31
|
+
|
30
32
|
if options.include?(:as)
|
31
33
|
options[:as] = [options[:as]] unless options[:as].is_a?(Array)
|
32
34
|
|
@@ -34,12 +36,18 @@ module Mongoid
|
|
34
36
|
raise ArgumentError, 'When option :as is used you must pass a name for each field.'
|
35
37
|
end
|
36
38
|
|
37
|
-
return fields.map.with_index
|
39
|
+
return fields.map.with_index do |field, index|
|
40
|
+
{name: field, as: options[:as][index], type: field_type(parent, field)}
|
41
|
+
end
|
38
42
|
elsif options.include?(:prefix)
|
39
|
-
return fields.map
|
43
|
+
return fields.map do |field|
|
44
|
+
{name: field, as: "#{options[:prefix]}_#{field}", type: field_type(parent, field)}
|
45
|
+
end
|
40
46
|
end
|
41
47
|
|
42
|
-
fields.map
|
48
|
+
fields.map do |field|
|
49
|
+
{name: field, as: "#{options[:from]}_#{field}", type: field_type(parent, field)}
|
50
|
+
end
|
43
51
|
end
|
44
52
|
|
45
53
|
# Add hook to child class to denormalize fields when parent relation is changed
|
@@ -59,8 +67,7 @@ module Mongoid
|
|
59
67
|
def self.add_hook_to_parent(child_class, fields, options)
|
60
68
|
from = options[:from].to_s
|
61
69
|
|
62
|
-
parent = (child_class
|
63
|
-
child_class.relations[from].name.capitalize).constantize
|
70
|
+
parent = parent_class(child_class, from)
|
64
71
|
|
65
72
|
relation = parent.relations[child_class.relations[from].inverse_of.to_s] ||
|
66
73
|
parent.relations[child_class.model_name.plural] ||
|
@@ -89,5 +96,16 @@ module Mongoid
|
|
89
96
|
end
|
90
97
|
end
|
91
98
|
end
|
99
|
+
|
100
|
+
# Retrieve parent class
|
101
|
+
def self.parent_class(child_class, from)
|
102
|
+
(child_class.relations[from].class_name || child_class.relations[from].name.capitalize)
|
103
|
+
.constantize
|
104
|
+
end
|
105
|
+
|
106
|
+
# Retreive the type of a field from the given class
|
107
|
+
def self.field_type(klass, field)
|
108
|
+
klass.fields[field.to_s].options[:type]
|
109
|
+
end
|
92
110
|
end
|
93
111
|
end
|
data/mongoid-denormalize.gemspec
CHANGED
@@ -25,5 +25,5 @@ Gem::Specification.new do |spec|
|
|
25
25
|
spec.add_development_dependency 'bundler', '~> 1.15'
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
27
27
|
spec.add_development_dependency 'rspec', '~> 3.0'
|
28
|
-
spec.add_dependency 'mongoid'
|
28
|
+
spec.add_dependency 'mongoid', ['>= 3.0', '< 7.0']
|
29
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-denormalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- rjurado01
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -58,14 +58,20 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
61
|
+
version: '3.0'
|
62
|
+
- - "<"
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '7.0'
|
62
65
|
type: :runtime
|
63
66
|
prerelease: false
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
65
68
|
requirements:
|
66
69
|
- - ">="
|
67
70
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
71
|
+
version: '3.0'
|
72
|
+
- - "<"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '7.0'
|
69
75
|
description: Helper module for denormalizing relations attributes in Mongoid models
|
70
76
|
email:
|
71
77
|
- rjurado01@gmail.com
|
@@ -82,6 +88,8 @@ files:
|
|
82
88
|
- Rakefile
|
83
89
|
- bin/console
|
84
90
|
- bin/setup
|
91
|
+
- gemfiles/mongoid-5.0.gemfile
|
92
|
+
- gemfiles/mongoid-6.0.gemfile
|
85
93
|
- lib/mongoid-denormalize.rb
|
86
94
|
- lib/mongoid-denormalize/version.rb
|
87
95
|
- mongoid-denormalize.gemspec
|
@@ -105,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
113
|
version: '0'
|
106
114
|
requirements: []
|
107
115
|
rubyforge_project:
|
108
|
-
rubygems_version: 2.7.
|
116
|
+
rubygems_version: 2.7.6
|
109
117
|
signing_key:
|
110
118
|
specification_version: 4
|
111
119
|
summary: Denormalize fields from relations
|