has_state_machine 0.1.1 → 0.3.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7a7e45d8de5f1bfdeb758a8d6b04668cceb84ef0fe41add93277237b48e0a75f
4
- data.tar.gz: 1dcb7de655710c843ccd1a8521c7760bcbcde34e0c64324744a5e2dc2a2047d7
3
+ metadata.gz: 524c408a87a46086495361e76507c6d1eb963acc6d72664c3faf0c8c913fa19f
4
+ data.tar.gz: 992299b87230582800a63afdf47bd573003481f74e3d521de414d8e20c783bf4
5
5
  SHA512:
6
- metadata.gz: 99bc942766e3741de93c38ba9fdd06300bb70ba688853b171525517a41c7830a56b144c49b31e129f5055dc3cb2b3d11901cf72cddaa0515df0095f973fc5332
7
- data.tar.gz: 23a277ebd986ea496608e51a68a60d0696ae0366443605bc936bcce7481b0ff25ab9f66eeb004017d8dc34bbf320de837764482eb413c74acc582c649e06bcf6
6
+ metadata.gz: a49dd7b572ba738ce5c29db2bb9f50100595a47353aa28b8bfb0e814340381202ac2db180efd42dfe9aea691088170eaf7ea266b1da0fc3a4d30ca287c266e44
7
+ data.tar.gz: 4a6ab1dfca389dcd4eac9d05a826d28cdbe145329dc078370327e76a07a2c8993d5fb470d981717ebec58c70c811501e447d20931d7cee0af8e8109132af6e41
data/README.md CHANGED
@@ -5,6 +5,14 @@
5
5
 
6
6
  HasStateMachine uses ruby classes to make creating a finite state machine for your ActiveRecord models a breeze.
7
7
 
8
+ ## Contents
9
+
10
+ - [Installation](#installation)
11
+ - [Usage](#usage)
12
+ - [Changelog](https://github.com/encampment/has_state_machine/blob/master/CHANGELOG.md)
13
+ - [Contributing](#contributing)
14
+ - [License](#license)
15
+
8
16
  ## Installation
9
17
  Add this line to your application's Gemfile:
10
18
 
@@ -70,11 +78,15 @@ module Workflow
70
78
  # There are callbacks for running logic before and after
71
79
  # a transition occurs.
72
80
  before_transition do
73
- Rails.logger.info "Post is being archived\n"
81
+ Rails.logger.info "== Post is being archived ==\n"
74
82
  end
75
83
 
76
84
  after_transition do
77
- Rails.logger.info "Post has been archived\n"
85
+ Rails.logger.info "== Post has been archived ==\n"
86
+
87
+ # You can access the previous state of the object in
88
+ # after_transition callbacks as well.
89
+ Rails.logger.info "== Transitioned from #{previous_state} ==\n"
78
90
  end
79
91
  end
80
92
  end
@@ -91,10 +103,31 @@ post.status # => "draft"
91
103
  post.title = "Foobar"
92
104
  post.status.transition_to(:published) # => true
93
105
  post.status # => "published"
106
+
107
+ post.status.transition_to(:archived)
108
+ # == Post is being archived ==
109
+ # == Post has been archived ==
110
+ # == Transitioned from published ==
111
+ # => true
94
112
  ```
95
113
 
96
114
  ## Contributing
97
- Coming Soon
115
+
116
+ Anyone is encouraged to help improve this project. Here are a few ways you can help:
117
+
118
+ - [Report bugs](https://github.com/encampment/has_state_machine/issues)
119
+ - Fix bugs and [submit pull requests](https://github.com/encampment/has_state_machine/pulls)
120
+ - Write, clarify, or fix documentation
121
+ - Suggest or add new features
122
+
123
+ To get started with development:
124
+
125
+ ```
126
+ git clone https://github.com/encampment/has_state_machine.git
127
+ cd has_state_machine
128
+ bundle install
129
+ bundle exec rake test
130
+ ```
98
131
 
99
132
  ## License
100
133
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -75,6 +75,14 @@ module HasStateMachine
75
75
  possible_transitions.include? desired_state
76
76
  end
77
77
 
78
+ ##
79
+ # Helper method for grabbing the previous state of the object after
80
+ # it has been transitioned to the new state. Useful in
81
+ # after_transition blocks
82
+ def previous_state
83
+ object.previous_changes[object.state_attribute]&.first
84
+ end
85
+
78
86
  def state_instance(desired_state)
79
87
  klass = "#{object.workflow_namespace}::#{desired_state.to_s.classify}".safe_constantize
80
88
  klass&.new(object)
@@ -55,7 +55,9 @@ module HasStateMachine
55
55
  # @example Retreiving a users published posts
56
56
  # > Post.published.where(user: user)
57
57
  # #=> [#<Post>]
58
- scope state, -> { where("#{state_attribute} = ?", state) }
58
+ if defined?(ActiveRecord) && (self < ActiveRecord::Base)
59
+ scope state, -> { where("#{state_attribute} = ?", state) }
60
+ end
59
61
 
60
62
  ##
61
63
  # Defines boolean helpers to determine if the active state matches
@@ -76,7 +78,7 @@ module HasStateMachine
76
78
  # Getter for the current state of the model based on the configured state
77
79
  # attribute.
78
80
  def current_state
79
- self[state_attribute]
81
+ attributes.with_indifferent_access[state_attribute]
80
82
  end
81
83
 
82
84
  ##
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HasStateMachine
4
- VERSION = "0.1.1"
4
+ VERSION = "0.3.3"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_state_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Hargett
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-10-30 00:00:00.000000000 Z
12
+ date: 2021-01-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails