mongoid-collection-separated 0.1.1 → 0.1.2
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/README.md +42 -2
- data/lib/mongoid/collection_separated/monkey_patches.rb +11 -4
- data/lib/mongoid/collection_separated/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfba70a37de67de41dc7d5d0adf80a2a2b3d5bc8e2c33ad8aa69b135e9b6f5f3
|
4
|
+
data.tar.gz: a44ba06116ad965932694728814144307946de2e3defe2718d4266cce185e3b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cba3d857433998e06971c0493c0708986509ad5afd63ecb78c6c68347eef00537052f44fbbea291fe8b3b8aa9c7b9469f9cc1d9fd370b45e0b36e4508b9bba6c
|
7
|
+
data.tar.gz: '048c3cf68d0e75226afc477b70fbb9acf42ce2cd419e263c97201ac7fec5b498ab4a27b27e39907c7263a1a25dc536d2f1dfab53347ed667dcd9b1523f04cedd'
|
data/README.md
CHANGED
@@ -20,13 +20,53 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
Add the following line into the model class that you want to split:
|
23
|
+
### 1. spepated by simple field
|
23
24
|
```ruby
|
24
|
-
|
25
|
+
class Entry
|
26
|
+
include Mongoid::Document
|
27
|
+
include Mongoid::Timestamps
|
28
|
+
include Mongoid::CollectionSeparated
|
29
|
+
include Mongoid::Attributes::Dynamic
|
30
|
+
|
31
|
+
separated_by :collection_suffix, :calc_collection_name
|
32
|
+
class << self
|
33
|
+
def calc_collection_name collection_suffix
|
34
|
+
return if collection_suffix.nil?
|
35
|
+
"entries_#{collection_suffix}"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
### 2. spepated by belongs to model id
|
42
|
+
```ruby
|
43
|
+
class Entry
|
44
|
+
include Mongoid::Document
|
45
|
+
include Mongoid::Timestamps
|
46
|
+
include Mongoid::CollectionSeparated
|
47
|
+
include Mongoid::Attributes::Dynamic
|
48
|
+
belongs_to :form
|
49
|
+
|
50
|
+
separated_by :form_id, :calc_collection_name, parent_class: 'Form'
|
51
|
+
class << self
|
52
|
+
def calc_collection_name form_id
|
53
|
+
return if form_id.nil?
|
54
|
+
"entries_#{form_id}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
class Form
|
61
|
+
include Mongoid::Document
|
62
|
+
has_many :entries
|
63
|
+
end
|
64
|
+
|
25
65
|
```
|
26
66
|
|
27
67
|
## Contributing
|
28
68
|
|
29
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
69
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/WeiGangqiang/mongoid_collection_separated
|
30
70
|
|
31
71
|
## License
|
32
72
|
|
@@ -20,8 +20,8 @@ module Mongoid
|
|
20
20
|
|
21
21
|
def calc_new_collection_name query_class
|
22
22
|
return unless query_class.respond_to?(:separated_field) && query_class.send(:separated_field).present?
|
23
|
-
return unless query_class.respond_to?(:calc_collection_name_fun)
|
24
|
-
query_class.send(query_class.calc_collection_name_fun,
|
23
|
+
return unless query_class.respond_to?(:calc_collection_name_fun) && query_class.respond_to?(query_class.calc_collection_name_fun)
|
24
|
+
query_class.send(query_class.calc_collection_name_fun, separated_value(query_class))
|
25
25
|
end
|
26
26
|
|
27
27
|
def separated_value query_class
|
@@ -68,11 +68,18 @@ module Mongoid
|
|
68
68
|
end
|
69
69
|
|
70
70
|
def should_query_from_separated_collection?(query_class)
|
71
|
-
|
71
|
+
is_separated_query_class?(query_class) && is_separated_parent_class?(query_class)
|
72
|
+
end
|
73
|
+
|
74
|
+
def is_separated_query_class? query_class
|
75
|
+
query_class.respond_to?(:separated_field) && query_class.send(:separated_field) && query_class.respond_to?(:calc_collection_name_fun) && query_class.respond_to?(query_class.calc_collection_name_fun)
|
76
|
+
end
|
77
|
+
|
78
|
+
def is_separated_parent_class? query_class
|
79
|
+
base.is_a?(query_class.separated_parent_class) && base.respond_to?(query_class.separated_parent_field)
|
72
80
|
end
|
73
81
|
|
74
82
|
def calc_new_collection_name query_class
|
75
|
-
return unless query_class.respond_to?(:calc_collection_name_fun) or query_class.respond_to?(query_class.calc_collection_name_fun)
|
76
83
|
query_class.send(query_class.calc_collection_name_fun, base.send(query_class.separated_parent_field))
|
77
84
|
end
|
78
85
|
|