百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
A

acts_as_favoritor

> 编程语言
开源

为 ActiveRecord 模型添加收藏、关注、投票等功能

345 stars0 点赞4 次浏览
访问官网GitHub

工具介绍

为 ActiveRecord 模型添加收藏、关注、投票等功能

acts_as_favoritor

acts_as_favoritor is a Rubygem to allow any ActiveRecord model to associate any other model including the option for multiple relationships per association with scopes.

You are able to differentiate followers, favorites, watchers, votes and whatever else you can imagine through a single relationship. This is accomplished by a double polymorphic relationship on the Favorite model. There is also built in support for blocking/un-blocking favorite records as well as caching.

This Medium article gives a good introduction to this gem.

Installation

You can add acts_as_favoritor to your Gemfile with:

ruby
gem 'acts_as_favoritor'

And then run:

$ bundle install

Or install it yourself as:

$ gem install acts_as_favoritor

If you always want to be up to date fetch the latest from GitHub in your Gemfile:

ruby
gem 'acts_as_favoritor', github: 'jonhue/acts_as_favoritor'

Now run the generator:

$ rails g acts_as_favoritor

To wrap things up, migrate the changes into your database:

$ rails db:migrate

Usage

Setup

Add acts_as_favoritable to the models you want to be able to get favorited:

ruby
class User  { favorite: true, watching: false }
user.favorited?(book, scopes: [:favorite]) # => { favorite: true }
user.favorited?(book, scope: :favorite) # => true

acts_as_favoritor also provides some handy scopes for you to call on the Favorite model:

ruby
# Returns all `Favorite` records where `scope` is `my_scope`
Favorite.send("#{my_scope}_list")

## Examples
### Returns all `Favorite` records where `scope` is `favorites`
Favorite.favorite_list
### Returns all `Favorite` records where `scope` is `watching`
Favorite.watching_list

Caching

When you set the option cache in config/initializers/acts_as_favoritor.rb to true, you are able to cache the amount of favorites/favoritables an instance has regarding a scope.

For that you need to add some database columns:

acts_as_favoritor

ruby
add_column :users, :favoritor_score, :text
add_column :users, :favoritor_total, :text

acts_as_favoritable

ruby
add_column :users, :favoritable_score, :text
add_column :users, :favoritable_total, :text
add_column :books, :favoritable_score, :text
add_column :books, :favoritable_total, :text

Caches are stored as hashes with scopes as keys:

ruby
user.favoritor_score # => { favorite: 1 }
user.favoritor_total # => { favorite: 1, watching: 1 }
second_user.favoritable_score # => { follow: 1 }
book.favoritable_score # => { favorite: 1 }

Note: Only scopes who have favorites are included.

acts_as_favoritor makes it even simpler to access cached values:

ruby
user.favoritor_favorite_cache # => 1
second_user.favoritable_follow_cache # => 1
book.favoritable_favorite_cache # => 1

Note: These methods are available for every scope you are using.

The total counts all favorites that were recorded, while the score factors in favorites that were removed. In most use cases the score is the most useful.

Configuration

You can configure acts_as_favoritor by passing a block to configure. This can be done in config/initializers/acts_as_favoritor.rb:

ruby
ActsAsFavoritor.configure do |config|
  config.default_scope = :follow
end

default_scope Specify your default scope. Takes a string. Defaults to :favorite. Learn more about scopes here.

cache Whether acts_as_favoritor uses caching or not. Takes a boolean. Defaults to false. Learn more about caching here.

Development

To start development you first have to fork this repository and locally clone your fork.

Install the projects dependencies by running:

$ bundle install

Testing

Tests are written with RSpec and can be found in /spec.

To run tests:

$ bundle exec rspec

To run RuboCop:

$ bundle exec rubocop

Contributing

We warmly welcome everyone who is intersted in contributing. Please reference our contributing guidelines and our Code of Conduct.

Releases

Here you can find details on all past releases. Unreleased breaking changes that are on the current main can be found here.

acts_as_favoritor follows Semantic Versioning 2.0 as defined at http://semver.org. Reference our security policy.

Publishing

  1. Review breaking changes and deprecations in CHANGELOG.md.
  2. Change the gem version in lib/acts_as_favoritor/version.rb.
  3. Reset CHANGELOG.md.
  4. Create a pull request to merge the changes into main.
  5. After the pull request was merged, create a new release listing the breaking changes and commits on main since the last release.
  6. The release workflow will publish the gem to RubyGems.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Rubyacts-asfavoritesrubygem

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言