statesman_mongoid 0.4.0 → 0.4.1

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: e91c70458d4fc19660876de07d0943d6556dd74b21128224fac9fbff5e949bc3
4
- data.tar.gz: 7c334f7ab383ba19a2ea4a023069c295f61011fbad0a104dd0953b866201cb01
3
+ metadata.gz: e026f69df2640019768b793fbbf588052ee15fca01cc1062d805ccaf0601bb35
4
+ data.tar.gz: 5ae0bea5ecc9b360244784191863a7003d974ac1f46d44f859ee860269d1f440
5
5
  SHA512:
6
- metadata.gz: 6774cea42364ac55c0be27f00de380f6a4404069099b7a0656c4ac36969dfb67d4d8125b05abbf56775b2f0530f1d843270c0f736f5ec9ccfa22302b2f2982ad
7
- data.tar.gz: b277d6b9e6611b538955fcce1cb75b088f600d053c0e8d0297f6cd5239ba534f01d9af98bc77a1ee7c8ada6afd2807f0530eb9e8b53168b4b10881477c0f797f
6
+ metadata.gz: 858083c254432838c46df2c32f1bd0421e6aa1cf7c9172bee49b7fda1abd87fef5cab3b3bec7ffcf5cc042b8b5706ad06a4f7eb26c46f60ee2b4facc81069f91
7
+ data.tar.gz: 9759390d1a84d8b898504f74dfd461eafde888e4accd6117bc5e9f939c53398e72616acbec32e4a1d44591262b56d4fc02161d701c854467d1d9ef2acd514e24
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- statesman_mongoid (0.4.0)
4
+ statesman_mongoid (0.4.1)
5
5
  statesman (~> 10.0)
6
6
 
7
7
  GEM
@@ -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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module StatesmanMongoid
4
- VERSION = "0.4.0"
4
+ VERSION = "0.4.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: statesman_mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - oz-tal