https://github.com/patorash/simple_group
simple_group is able to add group function to ActiveRecord.
https://github.com/patorash/simple_group
activerecord ruby
Last synced: 9 months ago
JSON representation
simple_group is able to add group function to ActiveRecord.
- Host: GitHub
- URL: https://github.com/patorash/simple_group
- Owner: patorash
- License: mit
- Created: 2015-04-10T07:26:01.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2025-06-19T04:53:39.000Z (about 1 year ago)
- Last Synced: 2025-08-21T18:26:28.759Z (10 months ago)
- Topics: activerecord, ruby
- Language: Ruby
- Size: 65.4 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# simple_group
simple_group gem is able to add group function to ActiveRecord.
[](https://github.com/patorash/simple_group/actions/workflows/test.yml)
[](https://codeclimate.com/github/patorash/simple_group)
[](https://codeclimate.com/github/patorash/simple_group)
## Installation
### Rails 5.0+
Add this line to your application's Gemfile:
```ruby
gem 'simple_group'
```
And then execute:
$ bundle install
### Database Migrations
simple_group uses a simple_group_combinations table to store all group information.
To genarate and run the migration just use.
$ bin/rails g simple_group:migration
$ bin/rake db:migrate
## Usage
### Group item model
Animal model
```ruby
class Animal < ActiveRecord::Base
groupable
validates :name, presence: true
end
```
Fruit model
```ruby
class Fruit < ActiveRecord::Base
groupable
validates :name, presence: true
end
```
### Group model
```ruby
class MyGroup < ActiveRecord::Base
add_group_function
validates :name, presence: true
validates :user, presence: true
end
```
### Example
```ruby
user = User.create(name: 'Taro')
group = MyGroup.create(name: 'Animals', user: user)
# group add a dog
dog = Animal.create(name: 'dog')
group.add(dog)
# cat add to group
cat = Animal.create(name: 'cat')
cat.add_to_group(group)
# group remove a dog
group.remove(dog)
# item list in group
group.group_items
# Only animal
group.for_type(Animal)
# Only fruit
group.for_type(Fruit)
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
1. Fork it ( https://github.com/patorash/simple_group/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create a new Pull Request