Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bigbinary/acts_in_sequence
Adds sequencing to the database records.
https://github.com/bigbinary/acts_in_sequence
Last synced: 3 months ago
JSON representation
Adds sequencing to the database records.
- Host: GitHub
- URL: https://github.com/bigbinary/acts_in_sequence
- Owner: bigbinary
- License: mit
- Created: 2020-07-14T12:43:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-30T18:49:10.000Z (almost 3 years ago)
- Last Synced: 2024-06-03T18:59:24.547Z (5 months ago)
- Language: Ruby
- Size: 20.5 KB
- Stars: 4
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# ActsInSequence
[![bigbinary](https://circleci.com/gh/bigbinary/acts_in_sequence/tree/master.svg?style=shield)](https://app.circleci.com/pipelines/github/bigbinary)
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](#scope) and more with this gem.
## Usage
### Install
```ruby
gem 'acts_in_sequence', git: 'https://github.com/bigbinary/acts_in_sequence'
``````ruby
bundle install
```### Create Migration
```ruby
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`.
```ruby
class Task < ActiveRecord::Base
acts_in_sequence
end
```#### Scope
This attribute allows us to track sequencing with a scope.```ruby
class TodoList < ActiveRecord::Base
has_many :tasks
endclass Task < ActiveRecord::Base
acts_in_sequence scope: :todo_listbelongs_to :todo_list
end
```You can scope on any column ( Yeah, thats right! )
```ruby
class Task < ActiveRecord::Base
acts_in_sequence scope :name
end
```#### Custom column name
With `:column_name` we can use custom column names instead of using `sequence`.```ruby
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.```ruby
class Task < ActiveRecord::Base
acts_in_sequence
endclass Item < ActiveRecord::Base
acts_in_sequence default_order: :desc
endTask.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 `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](https://rubygems.org).