workflow-activerecord 4.1.6 → 4.1.7
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 410db62b32e62adf3377dcecdbf9d6c42dda109a1fbb63a0cabc379ba8291bc1
|
4
|
+
data.tar.gz: ad1b249687e8b7f082a213b19e229eee2bd7e6530e4696aa8f253029d68e273a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 836f6c60d6db535c399567ec63c4193bf5bb57c7e366d64489d4fb3c3d51de369e8803131a7eb78f5ef14b48a996893c200a8e8d0aaf42e781d9eb05b75d4f01
|
7
|
+
data.tar.gz: b3680f266639313d3c2ffa8861730cf2f975ee022b1c6a385593270a4029c0eafdf9d689e614620881075f96a5045e6f13b28c0b3abacd978e9c76a009ee3cca
|
data/README.md
CHANGED
@@ -1,7 +1,5 @@
|
|
1
|
-
[![Version
|
2
|
-
](https://
|
3
|
-
[![Build Status
|
4
|
-
](https://travis-ci.org/geekq/workflow-activerecord.svg)](https://travis-ci.org/geekq/workflow-activerecord)
|
1
|
+
[![Version](https://img.shields.io/gem/v/workflow-activerecord.svg)](https://rubygems.org/gems/workflow-activerecord)
|
2
|
+
[![Build Status](https://travis-ci.org/geekq/workflow-activerecord.svg)](https://travis-ci.org/geekq/workflow-activerecord)
|
5
3
|
[![Code Climate](https://codeclimate.com/github/geekq/workflow-activerecord/badges/gpa.svg)](https://codeclimate.com/github/geekq/workflow-activerecord)
|
6
4
|
[![Test Coverage](https://codeclimate.com/github/geekq/workflow-activerecord/badges/coverage.svg)](https://codeclimate.com/github/geekq/workflow-activerecord/coverage)
|
7
5
|
|
@@ -190,6 +188,11 @@ You can have a look at an advanced [`on_transition`][] example in
|
|
190
188
|
Changelog
|
191
189
|
---------
|
192
190
|
|
191
|
+
### New in the version 4.1.7
|
192
|
+
|
193
|
+
* gh-9 refactor the implementation to a single file, deprecate `require
|
194
|
+
'workflow_activerecord'` (with the underscore)
|
195
|
+
|
193
196
|
### New in the version 4.1.6
|
194
197
|
|
195
198
|
* gh-3, gh-5 allow automatic require of workflow-activerecord - no need for explicit `require` anymore
|
@@ -1 +1,72 @@
|
|
1
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'workflow'
|
3
|
+
require 'workflow/specification'
|
4
|
+
|
5
|
+
module WorkflowActiverecord
|
6
|
+
def self.included(klass)
|
7
|
+
klass.send :include, ::Workflow
|
8
|
+
klass.send :include, InstanceMethods
|
9
|
+
klass.send :extend, Scopes
|
10
|
+
klass.before_validation :write_initial_state
|
11
|
+
end
|
12
|
+
|
13
|
+
module InstanceMethods
|
14
|
+
def load_workflow_state
|
15
|
+
read_attribute(self.class.workflow_column)
|
16
|
+
end
|
17
|
+
|
18
|
+
# On transition the new workflow state is immediately saved in the
|
19
|
+
# database.
|
20
|
+
def persist_workflow_state(new_value)
|
21
|
+
# Rails 3.1 or newer
|
22
|
+
update_column self.class.workflow_column, new_value
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Motivation: even if NULL is stored in the workflow_state database column,
|
28
|
+
# the current_state is correctly recognized in the Ruby code. The problem
|
29
|
+
# arises when you want to SELECT records filtering by the value of initial
|
30
|
+
# state. That's why it is important to save the string with the name of the
|
31
|
+
# initial state in all the new records.
|
32
|
+
def write_initial_state
|
33
|
+
write_attribute self.class.workflow_column, current_state.to_s
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# This module will automatically generate ActiveRecord scopes based on workflow states.
|
38
|
+
# The name of each generated scope will be something like `with_<state_name>_state`
|
39
|
+
#
|
40
|
+
# Examples:
|
41
|
+
#
|
42
|
+
# Article.with_pending_state # => ActiveRecord::Relation
|
43
|
+
# Payment.without_refunded_state # => ActiveRecord::Relation
|
44
|
+
#`
|
45
|
+
# Example above just adds `where(:state_column_name => 'pending')` or
|
46
|
+
# `where.not(:state_column_name => 'pending')` to AR query and returns
|
47
|
+
# ActiveRecord::Relation.
|
48
|
+
module Scopes
|
49
|
+
def self.extended(object)
|
50
|
+
class << object
|
51
|
+
alias_method :workflow_without_scopes, :workflow unless method_defined?(:workflow_without_scopes)
|
52
|
+
alias_method :workflow, :workflow_with_scopes
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def workflow_with_scopes(&specification)
|
57
|
+
workflow_without_scopes(&specification)
|
58
|
+
states = workflow_spec.states.values
|
59
|
+
|
60
|
+
states.each do |state|
|
61
|
+
define_singleton_method("with_#{state}_state") do
|
62
|
+
where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
|
63
|
+
end
|
64
|
+
|
65
|
+
define_singleton_method("without_#{state}_state") do
|
66
|
+
where.not("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
|
2
|
-
require 'workflow'
|
3
|
-
require 'workflow/specification'
|
4
|
-
require 'workflow_activerecord/adapters/active_record'
|
1
|
+
warn <<HERE
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
DEPRECATED: `require 'workflow_activerecord'` is obsolete. Usually you can just
|
4
|
+
remove the require line. In Rails, adding gem to Gemfile should be enough.
|
5
|
+
`bundle.require` will take care. If you need to require explicitely, use
|
6
|
+
workflow-activerecord (with dash) instead. The module with underscore will be
|
7
|
+
deleted on 4.2 release."
|
8
|
+
|
9
|
+
HERE
|
10
|
+
|
11
|
+
require_relative 'workflow-activerecord'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: workflow-activerecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.1.
|
4
|
+
version: 4.1.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Dobriakov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: workflow
|
@@ -142,9 +142,8 @@ files:
|
|
142
142
|
- LICENSE
|
143
143
|
- README.md
|
144
144
|
- lib/workflow-activerecord.rb
|
145
|
+
- lib/workflow-activerecord/version.rb
|
145
146
|
- lib/workflow_activerecord.rb
|
146
|
-
- lib/workflow_activerecord/adapters/active_record.rb
|
147
|
-
- lib/workflow_activerecord/version.rb
|
148
147
|
homepage: https://github.com/geekq/workflow-activerecord
|
149
148
|
licenses:
|
150
149
|
- MIT
|
@@ -1,71 +0,0 @@
|
|
1
|
-
module WorkflowActiverecord
|
2
|
-
module Adapter
|
3
|
-
module ActiveRecord
|
4
|
-
def self.included(klass)
|
5
|
-
klass.send :include, Adapter::ActiveRecord::InstanceMethods
|
6
|
-
klass.send :extend, Adapter::ActiveRecord::Scopes
|
7
|
-
klass.before_validation :write_initial_state
|
8
|
-
end
|
9
|
-
|
10
|
-
module InstanceMethods
|
11
|
-
def load_workflow_state
|
12
|
-
read_attribute(self.class.workflow_column)
|
13
|
-
end
|
14
|
-
|
15
|
-
# On transition the new workflow state is immediately saved in the
|
16
|
-
# database.
|
17
|
-
def persist_workflow_state(new_value)
|
18
|
-
# Rails 3.1 or newer
|
19
|
-
update_column self.class.workflow_column, new_value
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
# Motivation: even if NULL is stored in the workflow_state database column,
|
25
|
-
# the current_state is correctly recognized in the Ruby code. The problem
|
26
|
-
# arises when you want to SELECT records filtering by the value of initial
|
27
|
-
# state. That's why it is important to save the string with the name of the
|
28
|
-
# initial state in all the new records.
|
29
|
-
def write_initial_state
|
30
|
-
write_attribute self.class.workflow_column, current_state.to_s
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
# This module will automatically generate ActiveRecord scopes based on workflow states.
|
35
|
-
# The name of each generated scope will be something like `with_<state_name>_state`
|
36
|
-
#
|
37
|
-
# Examples:
|
38
|
-
#
|
39
|
-
# Article.with_pending_state # => ActiveRecord::Relation
|
40
|
-
# Payment.without_refunded_state # => ActiveRecord::Relation
|
41
|
-
#`
|
42
|
-
# Example above just adds `where(:state_column_name => 'pending')` or
|
43
|
-
# `where.not(:state_column_name => 'pending')` to AR query and returns
|
44
|
-
# ActiveRecord::Relation.
|
45
|
-
module Scopes
|
46
|
-
def self.extended(object)
|
47
|
-
class << object
|
48
|
-
alias_method :workflow_without_scopes, :workflow unless method_defined?(:workflow_without_scopes)
|
49
|
-
alias_method :workflow, :workflow_with_scopes
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def workflow_with_scopes(&specification)
|
54
|
-
workflow_without_scopes(&specification)
|
55
|
-
states = workflow_spec.states.values
|
56
|
-
|
57
|
-
states.each do |state|
|
58
|
-
define_singleton_method("with_#{state}_state") do
|
59
|
-
where("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
|
60
|
-
end
|
61
|
-
|
62
|
-
define_singleton_method("without_#{state}_state") do
|
63
|
-
where.not("#{table_name}.#{self.workflow_column.to_sym} = ?", state.to_s)
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
71
|
-
end
|