Skip to content

Commit

Permalink
ActiveAdmin for workflow gem
Browse files Browse the repository at this point in the history
  • Loading branch information
kwent committed Jun 6, 2019
1 parent 078f0d8 commit 91493c0
Show file tree
Hide file tree
Showing 32 changed files with 259 additions and 217 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ spec/dummy/.sass-cache
Gemfile.lock
.DS_Store
*.gem
.ruby-version
13 changes: 10 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
script: xvfb-run bundle exec rspec spec
rvm:
- 1.9.3
- 2.0.0
- 2.6.3
addons:
chrome: stable
before_install:
- gem update --system
- gem --version
- gem install bundler
before_script:
- psql -c 'create database aa_state_machine_test;' -U postgres
- psql -c 'create database aa_workflow_test;' -U postgres
- cp spec/dummy/config/database.travis.yml spec/dummy/config/database.yml
- RAILS_ENV=test cd spec/dummy && bundle exec rake db:setup
- cd ../../
env:
- RAILS_VERSION=6.0.0.rc1
- RAILS_VERSION=5.2.3
- RAILS_VERSION=5.1.7
- RAILS_VERSION=5.0.7.2
- RAILS_VERSION=4.2.11.1
13 changes: 8 additions & 5 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

# Declare your gem's dependencies in active_admin-state_machine.gemspec.
# Declare your gem's dependencies in active_admin_workflow.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec
Expand All @@ -14,14 +14,17 @@ gemspec
# gem 'debugger'

group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
gem 'uglifier', '>= 1.0.3'
gem 'jquery-rails', '~> 2.3.0'
gem 'sass-rails'
gem 'coffee-rails'
gem 'uglifier'
gem 'jquery-rails'
end

group :development, :test do
gem 'pry'
end

gem 'rails'
gem "sqlite3", '~> 1.3.6'
gem 'cancan'
gem 'workflow-activerecord', '>= 4.1', '< 6.0'
54 changes: 31 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
# ActiveAdmin-StateMachine
# ActiveAdmin-Workflow

This gem provides an easy integration between the awesome [state_machine](https://github.com/pluginaweek/state_machine) and [active_admin](https://github.com/gregbell/active_admin) gems.
[![Build Status](https://travis-ci.org/kwent/active_admin_workflow.svg?branch=master)](https://travis-ci.org/kwent/active_admin_workflow)

This gem provides an easy integration between the awesome [workflow](https://github.com/geekq/workflow) and [active_admin](https://github.com/gregbell/active_admin) gems.

## Features

This gem provides you with a DSL extension to ActiveAdmin that allows you to easily define action item buttons for your resource to transition through your state_machine.
This gem provides you with a DSL extension to ActiveAdmin that allows you to easily define action item buttons for your resource to transition through your workflow.

* Integration with ActiveAdmin authorization adapater layer - the button won't show if the user isn't authorized
* Provide custom permission to authorize against
* Provide confirmation message to prompt user before performing
* Button only shown if state_machine can transition
* Button only shown if workflow can transition
* Localizable button titles and flash messages
* Pass a block to customize the behavior

## Installation

This gem requires both `active_admin` and `state_machine`. Add the gem to your Gemfile to get started and bundle.
This gem requires both `active_admin` and `workflow`. Add the gem to your Gemfile to get started and bundle.

```ruby
gem 'active_admin-state_machine'
gem 'active_admin_workflow'
```

## Usage

You will need to define a state_machine in a model, and have that model registered as a resource with ActiveAdmin.
You will need to define a workflow in a model, and have that model registered as a resource with ActiveAdmin.

**A basic model**

```
# app/models/post.rb
class Post < ActiveRecord::Base
attr_accessible :body, :status, :title
include WorkflowActiverecord
validates :title, presence: true, uniqueness: true
validates :body, presence: true
Expand All @@ -39,13 +41,15 @@ class Post < ActiveRecord::Base
REVIEWED = 'reviewed'
PUBLISHED = 'published'
state_machine :status, initial: DRAFT do
event :peer_review do
transition DRAFT => REVIEWED
workflow do
state DRAFT do
event :peer_review, transitions_to: REVIEWED
end
event :publish do
transition REVIEWED => PUBLISHED
state REVIEWED do
event :publish, transitions_to: PUBLISHED
end
state PUBLISHED do
event :archive, transitions_to: ARCHIVED
end
end
end
Expand All @@ -55,7 +59,7 @@ end

```
ActiveAdmin.register Post do
state_action :peer_review
state_action :publish
Expand All @@ -72,8 +76,8 @@ end

In the above example, the `state_action` method is defined by this gem and provides you with the following functionality:

1. The `:peer_review` is assumed to be an event in your state_machine definition on the Post model.
2. Calling `state_action` will add an action item button to your ActiveAdmin resource on the `#show` page, if the user is authorized to perform this action via the ActiveAdmin authorization adapter, and if the resource itself returns true to `#can_peer_review?`, which is provided by default with this event in state_machine.
1. The `:peer_review` is assumed to be an event in your workflow definition on the Post model.
2. Calling `state_action` will add an action item button to your ActiveAdmin resource on the `#show` page, if the user is authorized to perform this action via the ActiveAdmin authorization adapter, and if the resource itself returns true to `#can_peer_review?`, which is provided by default with this event in workflow.
3. Clicking the button will call `#peer_review!` on the resource, and redirect you back to `smart_resource_url`.
4. Fully localizable, provide translations for `"posts.peer_review.label"`for the button title and `"posts.peer_review.flash.success` for the flash message when completed.
5. You can pass a block to customize this behavior.
Expand All @@ -82,9 +86,9 @@ In the above example, the `state_action` method is defined by this gem and provi

```
ActiveAdmin.register Post do
state_action :peer_review
# Block to be called when submitted
state_action :publish do
PostPublicationService.publish!(resource)
Expand Down Expand Up @@ -112,25 +116,29 @@ ActiveAdmin.register Post do
# Lookup 'posts.peer_review.prompt'
state_action :peer_review, confirm: :true
# Pass proc
state_action :peer_review, confirm: ->{ I18n.t("posts.peer_review.confirm") }
end
```

### Using without state_machine model
### Using without workflow model

If you really want to use without a state_machine on your model, you essentially just need to provide the following methods, contining with our `:peer_review` example:
If you really want to use without a workflow on your model, you essentially just need to provide the following methods, contining with our `:peer_review` example:

```
class Post < ActiveRecord::Base
def can_peer_review?
author.present? && !published?
end
def peer_review!
self.published_at = Time.now
save!
end
end
```

## Credits

Based on the amazing work of @macfanatic: https://github.com/macfanatic/active_admin-state_machine
34 changes: 0 additions & 34 deletions active_admin-state_machine.gemspec

This file was deleted.

38 changes: 38 additions & 0 deletions active_admin_workflow.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "active_admin/workflow/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "active_admin_workflow"
s.version = ActiveAdmin::Workflow::VERSION
s.authors = ["Quentin Rousseau"]
s.email = ["contact@quent.in"]
s.homepage = "https://github.com/kwent/active_admin_workflow"
s.summary = "Provides easy DSL integration between ActiveAdmin & workflow"
s.description = "Provides easy DSL integration between ActiveAdmin & workflow"
s.license = "MIT"

s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.md"]
s.test_files = Dir["spec/**/*"]

s.required_ruby_version = ">= 2.2"

s.add_dependency "railties", ">= 4.2"
s.add_dependency "activeadmin", ">= 1.0.0"
s.add_dependency "workflow", ">= 1.0.0"

s.add_development_dependency "rake"
s.add_development_dependency "sqlite3"
s.add_development_dependency "pg"
s.add_development_dependency "rspec-rails"
s.add_development_dependency "shoulda-matchers"
s.add_development_dependency "puma"
s.add_development_dependency "capybara"
s.add_development_dependency "selenium-webdriver"
s.add_development_dependency "webdrivers"
s.add_development_dependency "database_cleaner"
s.add_development_dependency "factory_bot_rails"
s.add_development_dependency "devise"
end
6 changes: 0 additions & 6 deletions lib/active_admin/state_machine.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/active_admin/state_machine/version.rb

This file was deleted.

6 changes: 6 additions & 0 deletions lib/active_admin/workflow.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'active_admin'
require 'workflow'

require 'active_admin/workflow/dsl'

ActiveAdmin::DSL.send :include, ActiveAdmin::Workflow::DSL
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module ActiveAdmin
module StateMachine
module Workflow
module DSL

#
# Easily tie into a state_machine action
# Easily tie into a workflow action
#
# @param [Symbol] state machine event, ie: :publish
# @param [Hash] options
Expand Down
5 changes: 5 additions & 0 deletions lib/active_admin/workflow/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ActiveAdmin
module Workflow
VERSION = "0.1.0"
end
end
6 changes: 1 addition & 5 deletions spec/dummy/app/models/admin_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ class AdminUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
devise :database_authenticatable,
:recoverable, :rememberable, :trackable, :validatable

# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
end
1 change: 0 additions & 1 deletion spec/dummy/app/models/category.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
class Category < ActiveRecord::Base
attr_accessible :name
has_many :posts
end
23 changes: 10 additions & 13 deletions spec/dummy/app/models/post.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Post < ActiveRecord::Base
attr_accessible :body, :status, :title
include WorkflowActiverecord

belongs_to :category

Expand All @@ -11,21 +11,18 @@ class Post < ActiveRecord::Base
PUBLISHED = 'published'
ARCHIVED = 'archived'

state_machine :status, initial: DRAFT do
event :peer_review do
transition DRAFT => REVIEWED
workflow do
state DRAFT do
event :peer_review, transitions_to: REVIEWED
end

event :publish do
transition REVIEWED => PUBLISHED
state REVIEWED do
event :publish, transitions_to: PUBLISHED
end

event :archive do
transition any - ARCHIVED => ARCHIVED
state PUBLISHED do
event :archive, transitions_to: ARCHIVED
end

event :reopen do
transition any - DRAFT => DRAFT
state ARCHIVED do
event :reopen, transitions_to: DRAFT
end
end
end
9 changes: 9 additions & 0 deletions spec/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
Loading

0 comments on commit 91493c0

Please sign in to comment.