mongoid-denormalization 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +53 -6
- data/lib/mongoid/denormalization/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70d9924038cb24d4c90d1a759f1a5f6a1ad5ce38
|
4
|
+
data.tar.gz: 37f730557a69d668d26763e991136d2469a69d94
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dad19e9a0236597a5b33d28d344f7af9c052987e187db742e853ca2fb4643b3405325470bf6126f42a1bee4dfdc2a6f2081711993d4526eeadf5af24a2e7ddd5
|
7
|
+
data.tar.gz: 6cf60d715b07b80892fb97851abf5376ba46dec638732bb9abd1b69b928b6f4d00330e82d5ce099b9c6d53a4b743e4dc9d5dbecfa368d490713fc4a2d5673d4e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
# Mongoid::Denormalization
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Helper module for denormalizing association attributes in Mongoid models & embedded models.
|
4
|
+
Supports rails 5.1.4, mongoid 6.2, ruby 2.4.2
|
6
5
|
|
7
6
|
## Installation
|
8
7
|
|
@@ -14,7 +13,7 @@ gem 'mongoid-denormalization'
|
|
14
13
|
|
15
14
|
And then execute:
|
16
15
|
|
17
|
-
$ bundle
|
16
|
+
$ bundle install
|
18
17
|
|
19
18
|
Or install it yourself as:
|
20
19
|
|
@@ -22,7 +21,55 @@ Or install it yourself as:
|
|
22
21
|
|
23
22
|
## Usage
|
24
23
|
|
25
|
-
|
24
|
+
In your model:
|
25
|
+
```ruby
|
26
|
+
# Include the helper method
|
27
|
+
include Mongoid::Denormalization
|
28
|
+
|
29
|
+
# Define your denormalization
|
30
|
+
denormalize_from(:user, :name) # this will add a user_name field on your model
|
31
|
+
denormalize_from(:user, :email) # this will add a user_email field on your model
|
32
|
+
```
|
33
|
+
|
34
|
+
Optionally, you can also write it as
|
35
|
+
```ruby
|
36
|
+
# this will add a username field on your model
|
37
|
+
denormalize_from(:user, :name, :denormalized_field_name => :username)
|
38
|
+
|
39
|
+
# this will add a useremail field on your model
|
40
|
+
denormalize_from(:user, :email, :denormalized_field_name => :useremail)
|
41
|
+
```
|
42
|
+
|
43
|
+
Lets say we have a company model with embedded jobs in it. We want to denormalize user_name and user_email in jobs.
|
44
|
+
We can write the following code in our jobs model:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
denormalize_from(:user, :name, to_klass_infos: [{
|
48
|
+
klasses: [::Company],
|
49
|
+
selector_proc: Proc.new do |id, value|
|
50
|
+
{:jobs => {"$elemMatch" => {:user_id => id, :user_name => {"$ne" => value}}}}
|
51
|
+
end,
|
52
|
+
updator: "jobs.$.user_name",
|
53
|
+
index_key: "jobs.user_id"
|
54
|
+
}])
|
55
|
+
|
56
|
+
denormalize_from(:user, :email, to_klass_infos: [{
|
57
|
+
klasses: [::Company],
|
58
|
+
selector_proc: Proc.new do |id, value|
|
59
|
+
{:jobs => {"$elemMatch" => {:user_id => id, :user_email => {"$ne" => value}}}}
|
60
|
+
end,
|
61
|
+
updator: "jobs.$.user_email",
|
62
|
+
index_key: "jobs.user_id"
|
63
|
+
}])
|
64
|
+
```
|
65
|
+
|
66
|
+
You can call force_denormalize also,
|
67
|
+
```ruby
|
68
|
+
company.force_denormalize # this will simply sync all the denormalized fields and will not triiger a save on document.
|
69
|
+
company.force_denormalize! # this will sync all the denormalized fields and will also triiger a save on document.
|
70
|
+
```
|
71
|
+
|
72
|
+
Note: you should also create proper db indexes required, otherwise the module may through an error.
|
26
73
|
|
27
74
|
## Development
|
28
75
|
|
@@ -32,7 +79,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
32
79
|
|
33
80
|
## Contributing
|
34
81
|
|
35
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/chaudhary/mongoid-denormalization. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
83
|
|
37
84
|
## License
|
38
85
|
|