workflow_status 0.0.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2df972219e574cb89cf577ba655fd1260c314d24
4
+ data.tar.gz: 8ce1351b6256830e613438bf88e4bf53b4660680
5
+ SHA512:
6
+ metadata.gz: 565a25f461d728033869d4123ca72e4586e1747287cc607535b4717df1f02881e4e24149b4e7b0e0dd60d82e19d4d2006b596ce0750d684292fbe98ae93184aa
7
+ data.tar.gz: a77d58db868410a1328a695a85cc1e688a3d453355396a80cf04f17cf92c191bfad66ef3bb5b495d1722664734edaa6b26f41d6c46af93b330cd0c21ba1145c6
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in workflow_status.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 John Friel
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # WorkflowStatus
2
+
3
+ When building a content management system, it's often useful to model the stages a user goes through when working on a bit of content. Much like how a new feature is not immediately ready to be committed to a git repo, a user will often want to start working on a new page or product as a 'draft' before she is ready to publish it to a live site. And it is also often useful to give users a way to 'trash' certain items without actually deleting the records from the database.
4
+
5
+ WorkflowStatus provides some convenient methods for modeling such a workflow with an ActiveRecord attribute called, unsurprisingly, 'workflow_status.' Imagine you have a Page model in your app. With workflow_status, you can call:
6
+
7
+ ```
8
+ Page.published
9
+ Page.first.published?
10
+ Page.last.trashed?
11
+ Page.unpublished
12
+ etc.
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ gem 'workflow_status'
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install workflow_status
28
+
29
+ ## Usage
30
+
31
+ Create a migration to add 'workflow_status' to your model.
32
+
33
+ ```
34
+ class AddWorkflowStatusToPage < ActiveRecord::Migration
35
+ def change
36
+ add_column :pages, :workflow_status, :integer, limit: 2, null: false, default: 0
37
+ end
38
+ end
39
+ ```
40
+
41
+
42
+ Then add WorkflowStatus to your model:
43
+
44
+ ```
45
+ class Page < ActiveRecord::Base
46
+
47
+ extend WorkflowStatus
48
+
49
+ # ...
50
+
51
+ end
52
+ ```
53
+
54
+ And that's it! You can now call all of the following:
55
+
56
+ ```
57
+ Page.published
58
+ Page.unpublished
59
+ Page.trashed
60
+ Page.first.published?
61
+ Page.last.trashed?
62
+ Page.find(3).unpublished?
63
+ ```
64
+
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it ( https://github.com/johnfriel/workflow_status/fork )
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,30 @@
1
+ module WorkflowStatus::InstanceMethods
2
+
3
+ def workflow_status
4
+ code = read_attribute :workflow_status
5
+ workflow_status_map.invert.fetch(code)
6
+ end
7
+
8
+ def workflow_status=(status_symbol)
9
+ value = workflow_status_map.fetch(status_symbol.to_sym)
10
+ write_attribute :workflow_status, value
11
+ end
12
+
13
+ def published?
14
+ workflow_status == :published
15
+ end
16
+
17
+ def unpublished?
18
+ workflow_status == :unpublished
19
+ end
20
+
21
+ def trashed?
22
+ workflow_status == :trashed
23
+ end
24
+
25
+ private
26
+
27
+ def workflow_status_map
28
+ WorkflowStatus::WORKFLOW_STATUS_MAP
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module WorkflowStatus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,48 @@
1
+ require "workflow_status/version"
2
+ require "workflow_status/instance_methods"
3
+
4
+ module WorkflowStatus
5
+ WORKFLOW_STATUS_MAP = {
6
+ unpublished: 0,
7
+ published: 1,
8
+ trashed: 2
9
+ }
10
+
11
+ class << self
12
+ def extended(extender)
13
+ extender.send :include, InstanceMethods
14
+ end
15
+ end
16
+
17
+ def workflow_statuses
18
+ workflow_status_map.keys
19
+ end
20
+
21
+ def unpublished
22
+ where(workflow_status: workflow_status_code(:unpublished))
23
+ end
24
+
25
+ def published
26
+ where(workflow_status: workflow_status_code(:published))
27
+ end
28
+
29
+ def trashed
30
+ where(workflow_status: workflow_status_code(:trashed))
31
+ end
32
+
33
+
34
+ # for Rails form select helper
35
+ def status_options_for_select
36
+ workflow_status_map.to_a
37
+ end
38
+
39
+ private
40
+
41
+ def workflow_status_code(text_value)
42
+ workflow_status_map.fetch text_value.to_sym
43
+ end
44
+
45
+ def workflow_status_map
46
+ WORKFLOW_STATUS_MAP
47
+ end
48
+ end
@@ -0,0 +1,118 @@
1
+ require 'workflow_status'
2
+
3
+ describe WorkflowStatus::InstanceMethods do
4
+
5
+ before :each do
6
+ class DummyClass
7
+ extend WorkflowStatus
8
+ end
9
+
10
+ @class = DummyClass
11
+ @obj = DummyClass.new
12
+ end
13
+
14
+ it 'adds a method for retreiving workflow_status' do
15
+ @obj.should respond_to(:workflow_status)
16
+ end
17
+
18
+ it 'adds a method for setting workflow_status' do
19
+ @obj.should respond_to(:'workflow_status=')
20
+ end
21
+
22
+
23
+ ##########
24
+ # Getter #
25
+ ##########
26
+ describe '#workflow_status' do
27
+ it "returns :published for an actual attr of 1" do
28
+ obj = @class.new
29
+ obj.stub(:read_attribute).with(:workflow_status) { 1 }
30
+
31
+ obj.workflow_status.should be(:published)
32
+ end
33
+
34
+ it "returns :unpublished for an actual attr of 0" do
35
+ obj = @class.new
36
+ obj.stub(:read_attribute).with(:workflow_status) { 0 }
37
+
38
+ obj.workflow_status.should be(:unpublished)
39
+ end
40
+
41
+ it "returns :trashed for an actual attr of 2" do
42
+ obj = @class.new
43
+ obj.stub(:read_attribute).with(:workflow_status) { 2 }
44
+
45
+ obj.workflow_status.should be(:trashed)
46
+ end
47
+ end
48
+
49
+
50
+ ##########
51
+ # Setter #
52
+ ##########
53
+ describe '#workflow_status=' do
54
+ it 'writes 0 to workflow_status for :unpublished' do
55
+ obj = @class.new
56
+
57
+ obj.should_receive(:write_attribute).with(:workflow_status, 0)
58
+ obj.workflow_status = :unpublished
59
+ end
60
+
61
+ it 'writes 1 to workflow_status for :published' do
62
+ obj = @class.new
63
+
64
+ obj.should_receive(:write_attribute).with(:workflow_status, 1)
65
+ obj.workflow_status = :published
66
+ end
67
+
68
+ it 'writes 2 to workflow_status for :trashed' do
69
+ obj = @class.new
70
+
71
+ obj.should_receive(:write_attribute).with(:workflow_status, 2)
72
+ obj.workflow_status = :trashed
73
+ end
74
+ end
75
+
76
+
77
+
78
+
79
+
80
+ describe '#published?' do
81
+ it "checks if workflow_status is :published" do
82
+ obj1 = @class.new
83
+ obj2 = @class.new
84
+
85
+ obj1.stub(:workflow_status) { :published }
86
+ obj2.stub(:workflow_status) { 'foo' }
87
+
88
+ obj1.published?.should be true
89
+ obj2.published?.should be false
90
+ end
91
+ end
92
+
93
+ describe '#unpublished?' do
94
+ it "checks if workflow_status is :unpublished" do
95
+ obj1 = @class.new
96
+ obj2 = @class.new
97
+
98
+ obj1.stub(:workflow_status) { :unpublished }
99
+ obj2.stub(:workflow_status) { 'bar' }
100
+
101
+ obj1.unpublished?.should be true
102
+ obj2.unpublished?.should be false
103
+ end
104
+ end
105
+
106
+ describe '#trashed?' do
107
+ it "checks if workflow_status is :trashed" do
108
+ obj1 = @class.new
109
+ obj2 = @class.new
110
+
111
+ obj1.stub(:workflow_status) { :trashed }
112
+ obj2.stub(:workflow_status) { 'quux' }
113
+
114
+ obj1.trashed?.should be true
115
+ obj2.trashed?.should be false
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,48 @@
1
+ require 'workflow_status'
2
+
3
+ describe WorkflowStatus do
4
+
5
+ before :each do
6
+ @klass = Class.new do
7
+ extend WorkflowStatus
8
+ end
9
+ end
10
+
11
+ it 'adds a class method for returning status names as symbols' do
12
+ @klass.workflow_statuses.should include(:unpublished)
13
+ @klass.workflow_statuses.should include(:published)
14
+ @klass.workflow_statuses.should include(:trashed)
15
+ end
16
+
17
+ # workflow_status 0
18
+ describe '#unpublished' do
19
+ it 'sends an ActiveRecord query for the correct workflow_status' do
20
+ @klass.should_receive(:where).with(workflow_status: 0)
21
+ @klass.unpublished
22
+ end
23
+ end
24
+
25
+ # workflow_status 1
26
+ describe '#published' do
27
+ it 'sends an ActiveRecord query for the correct workflow_status' do
28
+ @klass.should_receive(:where).with(workflow_status: 1)
29
+ @klass.published
30
+ end
31
+ end
32
+
33
+ # workflow_status 2
34
+ describe '#trashed' do
35
+ it 'sends an ActiveRecord query for the correct workflow_status' do
36
+ @klass.should_receive(:where).with(workflow_status: 2)
37
+ @klass.trashed
38
+ end
39
+ end
40
+
41
+ describe '#status_options_for_select' do
42
+ it 'returns an array of options suitable for Rails form select helper' do
43
+ @klass.status_options_for_select.should include([:unpublished, 0])
44
+ @klass.status_options_for_select.should include([:published, 1])
45
+ @klass.status_options_for_select.should include([:trashed, 2])
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'workflow_status/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "workflow_status"
8
+ spec.version = WorkflowStatus::VERSION
9
+ spec.authors = ["John Friel"]
10
+ spec.email = ["friel.john@gmail.com"]
11
+ spec.summary = "For CMS workflows that allow drafts, publishing and trashing, provides convenient, human-readable methods for an integer based ActiveRecord attribute"
12
+ spec.description = <<DESC
13
+ For CMS workflows that allow drafts, publishing and trashing, provides convenient, human-readable methods for an integer based ActiveRecord attribute
14
+ DESC
15
+ spec.homepage = ""
16
+ spec.license = "MIT"
17
+
18
+ spec.files = `git ls-files -z`.split("\x0")
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "~> 2.11"
26
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: workflow_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - John Friel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.11'
55
+ description: |
56
+ For CMS workflows that allow drafts, publishing and trashing, provides convenient, human-readable methods for an integer based ActiveRecord attribute
57
+ email:
58
+ - friel.john@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/workflow_status.rb
70
+ - lib/workflow_status/instance_methods.rb
71
+ - lib/workflow_status/version.rb
72
+ - spec/lib/instance_methods_spec.rb
73
+ - spec/workflow_status_spec.rb
74
+ - workflow_status.gemspec
75
+ homepage: ''
76
+ licenses:
77
+ - MIT
78
+ metadata: {}
79
+ post_install_message:
80
+ rdoc_options: []
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ required_rubygems_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 2.2.2
96
+ signing_key:
97
+ specification_version: 4
98
+ summary: For CMS workflows that allow drafts, publishing and trashing, provides convenient,
99
+ human-readable methods for an integer based ActiveRecord attribute
100
+ test_files:
101
+ - spec/lib/instance_methods_spec.rb
102
+ - spec/workflow_status_spec.rb