acts_as_starable
This gem is based on the structure, functionality, and features of the acts_as_follower gem. It provides the files and methods necessary to enable one ActiveRecord model to star, or to be starred, by another.
Compatibility
acts_as_starable was developed on and tested against Rails ~>4.0.0. Your mileage will vary on prior versions. It is fairly dependent on both Rails and ActiveRecord but there shouldn't be too much trouble getting it to work on any Rack based application. If anyone is interested in contributing support for Rails versions prior to 4.0.0 please see the contributing section below.
Installation
Add this line to your Gemfile:
gem 'acts_as_starable'And then execute:
$ bundleRun the generator:
$ rails generate acts_as_starableThis will generate a migration file as well as a model called Star.
Run the migration:
$ rails db:migrateUsage
There are three parts to enabling and using this gem. First you have to enable the required functionality on the models that you would like to be able to star other models or those that can be starred. After that you can use the methods defined in the ActsAsStarable::Starer module to work with a model that is able to star other models; or those defined in the ActsAsStarable::Starable module to work with models that are going to be starred by other models.
Setup
To enable one model to star other models, add the acts_as_starer method to that model:
class User < ActiveRecord::Base
...
acts_as_starer
...
endTo enable one model to be starred by other models, add the acts_as_starable method to that model:
class Band < ActiveRecord::Base
...
acts_as_starable
...
endOnce this functionality has been defined you can use the methods in each module to query your models for the state of the stars in your application.
acts_as_starer methods
To have one object star another:
# grab a starer and a starable
user = User.first
band = Band.first
# Create a record for the user as the starer and the band as the starable
user.star(band)To remove the previously created star:
user.unstar(band)To find out if a starer model has starred a starable model:
user.starred?(band)To get a count of stars created by a starer model:
user.stars_countTo get a collection of star objects from the Star table by type:
user.stars_by_type('Band')
# also accepts ActiveRecord options
user.stars_by_type('Band', limit: 5)To get a collection of the actual starable model records by type:
user.starred_by_type('Band')
# also accepts ActiveRecord options
user.starred_by_type('Band', limit: 5)To get a collection of all star objects from the Star table:
user.all_stars
# also accepts ActiveRecord options
user.all_stars(limit: 5)To get a collection of all of the actual starable model records:
user.all_starred
# also accepts ActiveRecord options
user.all_starred(limit: 5)acts_as_starable methods
To find out if a starable model has been starred by a starer model:
band.starred_by?(user)To get a count of the number of times a starable model has been starred:
band.starings_countTo get a collection of star objects from the Star table by type:
band.starings_by_type('User')
# also accepts ActiveRecord options
band.starings_by_type('User', limit: 5)To get a collection of the actual starer model records by type:
band.starers_by_type('User')
# also accepts ActiveRecord options
band.starers_by_type('User', limit: 5)To get a collection of all star objects from the Star table:
band.all_starings
# also accepts ActiveRecord options
band.all_starings(limit: 5)To get a collection of all of the actual starer model records:
band.all_starers
# also accepts ActiveRecord options
band.all_starers(limit: 5)Tests
Testing works as usual:
git clone https://github.com/littlstar/acts_as_starable.git
cd acts_as_starable
bundle install
rakeContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
Attribution
I can only take a small amount of credit for the creation of this gem. Its structure, functionality, and features are based in large part on the excellent work of @tcocca (and other contributors) on the acts_as_follower gem.