statesman_mongoid 0.3.0 → 0.4.1
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/Gemfile.lock +1 -1
- data/README.md +8 -3
- data/lib/generators/statesman/mongoid_transition_generator.rb +27 -0
- data/lib/generators/statesman/templates/mongoid_transition_model.rb.erb +7 -0
- data/lib/statesman/adapters/mongoid.rb +1 -1
- data/lib/statesman/adapters/mongoid_queries.rb +29 -0
- data/lib/statesman_mongoid/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e026f69df2640019768b793fbbf588052ee15fca01cc1062d805ccaf0601bb35
|
4
|
+
data.tar.gz: 5ae0bea5ecc9b360244784191863a7003d974ac1f46d44f859ee860269d1f440
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 858083c254432838c46df2c32f1bd0421e6aa1cf7c9172bee49b7fda1abd87fef5cab3b3bec7ffcf5cc042b8b5706ad06a4f7eb26c46f60ee2b4facc81069f91
|
7
|
+
data.tar.gz: 9759390d1a84d8b898504f74dfd461eafde888e4accd6117bc5e9f939c53398e72616acbec32e4a1d44591262b56d4fc02161d701c854467d1d9ef2acd514e24
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
# Statesman Mongoid
|
2
2
|
|
3
|
-
|
3
|
+
Mongoid adapters for [Statesman](https://github.com/gocardless/statesman).
|
4
4
|
|
5
|
-
TODO: Restore
|
5
|
+
TODO: Restore & update specs
|
6
6
|
|
7
|
-
NOTE:
|
7
|
+
NOTE: Restored Statesman's mongoid support with some fixes & updates. This project is in an early stage, please report any bugs or missing features. Use at your own risk and feel free to contribute.
|
8
8
|
|
9
9
|
## Installation
|
10
10
|
|
@@ -33,6 +33,11 @@ Statesman::Adapters::MongoidTransition
|
|
33
33
|
Statesman::Adapters::MongoidQueries
|
34
34
|
```
|
35
35
|
|
36
|
+
Generator:
|
37
|
+
```
|
38
|
+
rails generate statesman:mongoid_transition Order OrderTransition
|
39
|
+
```
|
40
|
+
|
36
41
|
## Development
|
37
42
|
|
38
43
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Extracted from commit b9906ee1cf0ac6c1bbdd56c003ffe407c2f59833
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
require "generators/statesman/generator_helpers"
|
5
|
+
|
6
|
+
module Statesman
|
7
|
+
class MongoidTransitionGenerator < Rails::Generators::Base
|
8
|
+
include Statesman::GeneratorHelpers
|
9
|
+
|
10
|
+
desc "Create a Mongoid-based transition model with the required attributes"
|
11
|
+
|
12
|
+
argument :parent, type: :string, desc: "Your parent model name"
|
13
|
+
argument :klass, type: :string, desc: "Your transition model name"
|
14
|
+
|
15
|
+
source_root File.expand_path("templates", __dir__)
|
16
|
+
|
17
|
+
def create_model_file
|
18
|
+
template("mongoid_transition_model.rb.erb", model_file_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def collection_name
|
24
|
+
klass.underscore.pluralize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -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
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: statesman_mongoid
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- oz-tal
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: statesman
|
@@ -36,6 +36,8 @@ files:
|
|
36
36
|
- LICENSE.txt
|
37
37
|
- README.md
|
38
38
|
- Rakefile
|
39
|
+
- lib/generators/statesman/mongoid_transition_generator.rb
|
40
|
+
- lib/generators/statesman/templates/mongoid_transition_model.rb.erb
|
39
41
|
- lib/statesman/adapters/mongoid.rb
|
40
42
|
- lib/statesman/adapters/mongoid_queries.rb
|
41
43
|
- lib/statesman/adapters/mongoid_transition.rb
|