statesman_mongoid 0.3.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: 150c388e7dc52a4d1cd5f7dc8aec635bb70d254ecbab0928e151463ac9ca43d1
4
- data.tar.gz: 4db6ce07d5bca4cb8bb8f60cd07e0165072d23c977b1b165e7358e150eb580e5
3
+ metadata.gz: e026f69df2640019768b793fbbf588052ee15fca01cc1062d805ccaf0601bb35
4
+ data.tar.gz: 5ae0bea5ecc9b360244784191863a7003d974ac1f46d44f859ee860269d1f440
5
5
  SHA512:
6
- metadata.gz: 172df6b8fe89efababe619552ed5391765b7e18f38998f7cebf7fa00861a05a770436659b23c0ce4f59435643ac32ae5d1f8eb48bcb3a6d19fb01f68a7e65424
7
- data.tar.gz: 15460af6a52050507a9d8443ab696ce89a74a9bca276351ee78decaaed5279f476eac3d0ba3f8e6104cf4b5884100b5a43dccf7d4af59d4282048527e523cf55
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.3.0)
4
+ statesman_mongoid (0.4.1)
5
5
  statesman (~> 10.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Statesman Mongoid
2
2
 
3
- Restored mongoid adapters for [Statesman](https://github.com/gocardless/statesman).
3
+ Mongoid adapters for [Statesman](https://github.com/gocardless/statesman).
4
4
 
5
- TODO: Restore generators and test units
5
+ TODO: Restore & update specs
6
6
 
7
- NOTE: This is an early salvage of the dropped mongoid support with a refitted queries adapter, a couple of fixes and some manual testing, use at your own risk and feel free to contribute.
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
@@ -0,0 +1,7 @@
1
+ class <%= klass %>
2
+ include Statesman::Adapters::MongoidTransition
3
+
4
+ index({ sort_key: 1 })
5
+
6
+ belongs_to :<%= parent %><%= class_name_option %>, index: true
7
+ end
@@ -1,4 +1,4 @@
1
- # Restored mongoid support, untouched
1
+ # Restored mongoid support with feature parity
2
2
  # Extracted from commit b9906ee1cf0ac6c1bbdd56c003ffe407c2f59833
3
3
 
4
4
  module Statesman
@@ -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.3.0"
4
+ VERSION = "0.4.1"
5
5
  end
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.3.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-09 00:00:00.000000000 Z
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