statesman_mongoid 0.4.0 → 0.4.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97d9816cbf3c2f3ff3c8cdf9c39c9a90fa83593a67d4fa774b3fcfbbc43f6699
|
4
|
+
data.tar.gz: a822c04a61bdb81c487d7125504d17dcf6fc239e48c67d669be5eee691077d22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c51f46372492ec81efd46bce9ed697a3a0dbe1621a6bc3d4d3979fcd4d5b54e5f103af4b0cd7ec7839f3c9da96b098c2b377621240a22754e427942a9073a24d
|
7
|
+
data.tar.gz: 83da04c91091fe007474acd36fa9decc40420024ad03380dc57b4353bcd6a17b3a819fb0081f2af68b2420cf64a7a50a8f2f71664e400629589df9bdc0c46284
|
data/Gemfile.lock
CHANGED
@@ -94,14 +94,43 @@ module Statesman
|
|
94
94
|
|
95
95
|
def states_where(states)
|
96
96
|
ids = aggregate_ids_for_most_recent(states, inclusive_match: true)
|
97
|
+
|
98
|
+
if initial_state.to_s.in?(states.map(&:to_s))
|
99
|
+
all_ids = aggregate_ids_for_all_state(states)
|
100
|
+
ids += model.where(_id: { '$nin' => all_ids }).pluck(:id)
|
101
|
+
end
|
102
|
+
|
97
103
|
model.where(_id: { '$in' => ids })
|
98
104
|
end
|
99
105
|
|
100
106
|
def states_where_not(states)
|
101
107
|
ids = aggregate_ids_for_most_recent(states, inclusive_match: false)
|
108
|
+
|
109
|
+
unless initial_state.to_s.in?(states.map(&:to_s))
|
110
|
+
all_ids = aggregate_ids_for_all_state(states)
|
111
|
+
ids += model.where(_id: { '$nin' => all_ids }).pluck(:id)
|
112
|
+
end
|
113
|
+
|
102
114
|
model.where(_id: { '$in' => ids })
|
103
115
|
end
|
104
116
|
|
117
|
+
def aggregate_ids_for_all_state(states)
|
118
|
+
aggregation = [
|
119
|
+
# Group by foreign key
|
120
|
+
{
|
121
|
+
'$group': {
|
122
|
+
_id: "$#{model_foreign_key}",
|
123
|
+
model_foreign_key => { '$first': "$#{model_foreign_key}" },
|
124
|
+
},
|
125
|
+
},
|
126
|
+
# Trim response to only the foreign key
|
127
|
+
{ '$project': { _id: 0 } },
|
128
|
+
]
|
129
|
+
|
130
|
+
# Hit the database and return a flat array of ids
|
131
|
+
transition_class.collection.aggregate(aggregation).pluck(model_foreign_key)
|
132
|
+
end
|
133
|
+
|
105
134
|
def aggregate_ids_for_most_recent(states, inclusive_match: true)
|
106
135
|
aggregation = [
|
107
136
|
# Sort most recent
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Restored mongoid support, fixes applied: concernized the module and added includes + fields
|
1
|
+
# Restored mongoid support, fixes applied: concernized the module and added includes + fields + index
|
2
2
|
# Extracted from commit b9906ee1cf0ac6c1bbdd56c003ffe407c2f59833
|
3
3
|
|
4
4
|
module Statesman
|
@@ -15,6 +15,9 @@ module Statesman
|
|
15
15
|
field :sort_key, type: Integer
|
16
16
|
field :most_recent, type: ::Mongoid::Boolean
|
17
17
|
|
18
|
+
index({ sort_key: 1 })
|
19
|
+
|
20
|
+
# TODO: Remove or document why is this neccessary
|
18
21
|
self.send(:alias_method, :metadata, :statesman_metadata)
|
19
22
|
self.send(:alias_method, :metadata=, :statesman_metadata=)
|
20
23
|
end
|