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: e91c70458d4fc19660876de07d0943d6556dd74b21128224fac9fbff5e949bc3
4
- data.tar.gz: 7c334f7ab383ba19a2ea4a023069c295f61011fbad0a104dd0953b866201cb01
3
+ metadata.gz: 97d9816cbf3c2f3ff3c8cdf9c39c9a90fa83593a67d4fa774b3fcfbbc43f6699
4
+ data.tar.gz: a822c04a61bdb81c487d7125504d17dcf6fc239e48c67d669be5eee691077d22
5
5
  SHA512:
6
- metadata.gz: 6774cea42364ac55c0be27f00de380f6a4404069099b7a0656c4ac36969dfb67d4d8125b05abbf56775b2f0530f1d843270c0f736f5ec9ccfa22302b2f2982ad
7
- data.tar.gz: b277d6b9e6611b538955fcce1cb75b088f600d053c0e8d0297f6cd5239ba534f01d9af98bc77a1ee7c8ada6afd2807f0530eb9e8b53168b4b10881477c0f797f
6
+ metadata.gz: c51f46372492ec81efd46bce9ed697a3a0dbe1621a6bc3d4d3979fcd4d5b54e5f103af4b0cd7ec7839f3c9da96b098c2b377621240a22754e427942a9073a24d
7
+ data.tar.gz: 83da04c91091fe007474acd36fa9decc40420024ad03380dc57b4353bcd6a17b3a819fb0081f2af68b2420cf64a7a50a8f2f71664e400629589df9bdc0c46284
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.2)
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,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
@@ -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.2"
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - oz-tal