Sequenceable
Adds sequencing to the database records.
Often we want to keep a record sequence
/order
in the database. This gem takes care of adding sequence (auto-incremented) to newly added records.
Tough its not just auto-increment, you can do scoped sequencing and more with this gem.
Usage
Install
gem 'sequenceable'
bundle
Create Migration
bin/rails generate migration AddSequenceTo{MODEL_NAME} sequence:integer
Enable Sequencing
By default, acts_in_sequence
assumes the records sequence is stored in sequence
column of type integer
.
class Task < ActiveRecord::Base
acts_in_sequence
end
:scope
This attribute allows us to track sequencing with a scope.
class TodoList < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
acts_in_sequence scope: :todo_list
belongs_to :todo_list
end
You can scope on any column ( Yeah, thats right! )
class Task < ActiveRecord::Base
acts_in_sequence scope :name
end
:column_name
With :column_name
we can use custom column names instead of using sequence
.
class Task < ActiveRecord::Base
acts_in_sequence column_name: :display_order
end
:default_order
When sequencing is applied, records will be sorted with ASC
sequence by default. Use default_order
attribute to change it when you need to.
Use scope without_sequence_order
when you want to remove the default ordering.
class Task < ActiveRecord::Base
acts_in_sequence
end
class Item < ActiveRecord::Base
acts_in_sequence default_order: :desc
end
Task.all # => 1, 2, 3, 4, 5
Item.all # => 5, 4, 3, 2, 1
Item.without_sequence_order.all # => 1, 2, 3, 4, 5
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
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/sandip-mane/sequenceable. 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.
Code of Conduct
Everyone interacting in the Sequenceable project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.