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.
gem 'acts_in_sequence', git: 'https://github.com/bigbinary/acts_in_sequence'bundle installbin/rails generate migration AddSequenceTo{MODEL_NAME} sequence:integerBy default, acts_in_sequence assumes the records sequence is stored in sequence column of type integer.
class Task < ActiveRecord::Base
acts_in_sequence
endThis 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
endYou can scope on any column ( Yeah, thats right! )
class Task < ActiveRecord::Base
acts_in_sequence scope :name
endWith :column_name we can use custom column names instead of using sequence.
class Task < ActiveRecord::Base
acts_in_sequence column_name: :display_order
endWhen 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, 5After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec 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.