rails-workflow 1.4.13 → 1.4.14

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
  SHA1:
3
- metadata.gz: 30ada397c29addd3b7b628691797ac590f7214d0
4
- data.tar.gz: 0e7375ee4380363fb96ac04c3f7e3d62608e9ed4
3
+ metadata.gz: 17c522c16b567a5a0e909a8b37e46b43dcf46f96
4
+ data.tar.gz: b585c04ac5f95851b43d537cdf785b480e51aaec
5
5
  SHA512:
6
- metadata.gz: 349149ca15104eb4a9aef2f48d3090a48434b60e0842f6be06de9bece667c8119d6f0f220e5f28f70f8898d0db70b2a85278108b73d91e9815b8fe1c69932086
7
- data.tar.gz: ad54fca54b8276f34badbc4e80c6ff2e72bca982ed501b1cc5b1d9dfeafc283206db9a8eb748ef8d3663cb62d39a415774fe9d6e042323f1ccab732ef9b15ed5
6
+ metadata.gz: dca5ec08aaf2013e28b68e94d73b14feb136f631c9ed0e1b89cb66c904a6d8b33e0771fcfe5a5cd6b5868d12f9a848b14bb79118c8d46b4bb7b3bde9b79e07d2
7
+ data.tar.gz: f594cc082be1139d73a79652ff6779d74ec4ecb470e78d143e8032fe0ec7cac2e3f0d71c9917481bc2b89ba62e6abdcf534d489357c71a1180c625f630ed172f
@@ -8,7 +8,7 @@ module Workflow
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  included do
11
- prepend RevalidateOnlyAfterAttributesChange
11
+ prepend RevalidateOnlyAfterAttributesChange if self < ::ActiveRecord::Base
12
12
  end
13
13
 
14
14
  ###
@@ -21,11 +21,7 @@ module Workflow
21
21
  #
22
22
  def method_missing(method, *args, &block)
23
23
  if method.to_s =~ /^transitioning_(from|to|via_event)_([\w_-]+)\?$/
24
- class_eval "
25
- def #{method}
26
- transitioning? direction: '#{$LAST_MATCH_INFO[1]}', state: '#{$LAST_MATCH_INFO[2]}'
27
- end
28
- "
24
+ define_transitioning_method(method, *$LAST_MATCH_INFO[1..2])
29
25
  send method
30
26
  else
31
27
  super
@@ -108,7 +104,7 @@ module Workflow
108
104
  end
109
105
 
110
106
  def write_attribute(attr_name, value)
111
- @changed_since_validation = true
107
+ @changed_since_validation = true unless attr_name.to_sym == :workflow_state
112
108
  super
113
109
  end
114
110
  end
@@ -131,6 +127,22 @@ module Workflow
131
127
 
132
128
  private
133
129
 
130
+ def persist_workflow_state(new_state)
131
+ old_state = current_state.name
132
+ return_val = super
133
+ return return_val if valid?
134
+ super(old_state)
135
+ false
136
+ end
137
+
138
+ def define_transitioning_method(method, direction, state)
139
+ class_eval do
140
+ define_method method do
141
+ transitioning? direction: direction, state: state
142
+ end
143
+ end
144
+ end
145
+
134
146
  def transitioning?(direction:, state:)
135
147
  state = state.to_sym
136
148
  return false unless transition_context
@@ -10,6 +10,9 @@ module Workflow
10
10
  else
11
11
  if Adapters.const_defined?(:ActiveRecord) && self < ::ActiveRecord::Base
12
12
  include Adapters::ActiveRecord
13
+ end
14
+
15
+ if included_modules.map(&:name).include?('ActiveModel::Validations')
13
16
  include ActiveRecordValidations
14
17
  end
15
18
 
@@ -89,7 +89,7 @@ module Workflow
89
89
  # @return [nil]
90
90
  def state(name, tags: [], **meta, &events)
91
91
  name = name.to_sym
92
- new_state = Workflow::State.new(name, @states.length, tags: tags, **meta)
92
+ new_state = Workflow::State.new(name, @states, tags: tags, **meta)
93
93
  @initial_state ||= new_state
94
94
  @states << new_state
95
95
  new_state.instance_eval(&events) if block_given?
@@ -145,7 +145,7 @@ module Workflow
145
145
 
146
146
  module StateTagHelpers
147
147
  def initial?
148
- sequence.zero?
148
+ all_states.index(self).zero?
149
149
  end
150
150
 
151
151
  def terminal?
@@ -17,11 +17,11 @@ module Workflow
17
17
  # @api private
18
18
  # For creating {Workflow::State} objects please see {Specification#state}
19
19
  # @param [Symbol] name Name of the state being created. Must be unique within its workflow.
20
- # @param [Fixnum] sequence Sort location among states on this workflow.
20
+ # @param [Array] all_states All states defined for this workflow, used for sorting.
21
21
  # @param [Hash] meta Optional metadata for this state.
22
- def initialize(name, sequence, tags: [], **meta)
22
+ def initialize(name, all_states, tags: [], **meta)
23
23
  @name = name.to_sym
24
- @sequence = sequence
24
+ @all_states = all_states
25
25
  @events = []
26
26
  @tags = [tags].flatten.uniq
27
27
  unless @tags.reject { |t| t.is_a? Symbol }
@@ -93,6 +93,12 @@ module Workflow
93
93
  nil
94
94
  end
95
95
 
96
+ class << self
97
+ def beyond?(other)
98
+ -> { current_state > other }
99
+ end
100
+ end
101
+
96
102
  private def check_can_add_transition!(name, to: nil)
97
103
  raise Errors::DualEventDefinitionError if to && block_given?
98
104
 
@@ -111,19 +117,20 @@ module Workflow
111
117
  # Overloaded comparison operator. Workflow states are sorted according to the order
112
118
  # in which they were defined.
113
119
  #
114
- # @param [Workflow::State] other state to be compared against.
120
+ # @param [Workflow::State] other state or Symbol name to be compared against.
115
121
  # @return [Integer]
116
122
  def <=>(other)
117
- raise Errors::StateComparisonError, other unless other.is_a?(State)
118
- sequence <=> other.send(:sequence)
123
+ if other.is_a?(Symbol)
124
+ other = all_states.find { |state| state.name == other }
125
+ end
126
+ all_states.index(self) <=> all_states.index(other)
119
127
  end
120
128
 
121
129
  private
122
130
 
123
131
  # @api private
124
- # @!attribute [r] sequence
125
- # @return [Fixnum] The position of this state within the
126
- # order it was defined for in its workflow.
127
- attr_reader :sequence
132
+ # @!attribute [r] all_states
133
+ # @return [Array] All states defined on my workflow. Used for sorting.
134
+ attr_reader :all_states
128
135
  end
129
136
  end
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Workflow
3
- VERSION = '1.4.13'
3
+ VERSION = '1.4.14'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-workflow
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.13
4
+ version: 1.4.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tyler Gannon