Skip to content

snmmaurya/enum-transition

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gem Version

Enum::Transition

Active Record ENUM based state machine. Whether you can control enum transation from one state to another state. Suppose you have an user model and you want to once user has been rejected he can't be verified, You can achive this job easily using this library.

Installation

Add this line to your application's Gemfile:

gem 'enum-transition'

And then execute:

$ bundle

Or install it yourself as:

$ gem install enum-transition

Usage

# You have an user model and you want to specify status transitions: initial to verified, initial to rejected and rejected to initial


class User
  include Enum::Transition

  enum status: {initial: 0, verified: 1, rejected: 2]

  # Specify enum columns
  enum_columns [:status]

  # Specify (allow state transition)
  enum_transitions :initial, [:verified, :rejected]
  enum_transitions :rejected, [:initial]
end

user = User.create(status: :initial)


# IF user status is initial
user.verified! # Okay
user.rejected! # Okay

# IF user status is verified
user.rejected! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from verified to rejected
user.initial! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from verified to initial

# IF user status is rejected
user.initial! # Okay
user.verified! # ActiveRecord::RecordInvalid: Validation failed: Status can't be changed from rejected to verified

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/snmmaurya/enum-transition. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

About

Active Record ENUM based state machine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published