I am a doctoral researcher in computer science at ETH ZΓΌrich.
Feel free to reach out to me through [email protected].
Adds Favorite, Follow, Vote, etc. functionality to ActiveRecord models
License: MIT License
I am a doctoral researcher in computer science at ETH ZΓΌrich.
Feel free to reach out to me through [email protected].
Hi there,
I'm trying to use this gem in my project and I noticed a strange behavior in the Favoritor
methods.
I could favorite
a favoritable item successfully but I couldn't get the favorites_count
, remove_favorite
or favorited?
results.
after some checking, I found that the scope string is saved "favorite" in the database but when trying to select from the Favorite table you pass the string "Favorite" which means you'll get 0 results as the strings are not identical.
Thank you.
Allow for caching of counters in the database table of acts_as_favoritable
.
Describe the bug
#favorited_by? and #favorited? are not working for me. I believe it is because the scope is being capitalized somewhere within #favorited_by? and # favorited?.
I'm using the default scope. If I change the default scope to anything else, it also gets capitalized. When I change the default scope to a capitalized word, the methods work as expected.
Screenshots
If applicable, add screenshots to help explain your problem.
Additional context
This might be related to #32
Current tests produce 38 failures.
Take a look at the test log.
Ruby version: 2.3.3
Some guy create a cool gem: https://github.com/geoffreylitt/simple_recommender
The simple use is like this, with an has_many
with through
class Book < ActiveRecord::Base
has_many :likes
has_many :users, through: :likes
include SimpleRecommender::Recommendable
similar_by :users
end
As described in #79
Walkthrough
Awesome gem so far, enjoyed using it to implement a favorite
functionality which works great. Right now, I'd like to add a scope for verify
functionality. Adding the scope to verify
works but when I check if it's been favorited with verify
scope or try to remove_favorite
with the verify scope, I get the following error:
NoMethodError: undefined method
+' for :verify:Symbol`
Any ideas?
I saw #26 and tried to use this gem 'acts_as_favoritor', github: 'jonhue/acts_as_favoritor'
in the gemfile instead to get the latest version but niet.
Screenshot
See attached π
Expected behavior
Expected behaviour was to receive a true
or false
if user has been verified and to be able to remove verification.
Local env:
Hi,
So in 2.1.2 favorited_by_type
used to return an active record relationship object and we could do stuff like User.favorited_by_type('Product', scopes: [:foo]).not_deleted.enabled
. How do we do this now ?
Now it's returning an array and i didn't see this in the breaking changes changelog AFAIK
Also README says you can pass :limit
and :order
parameters https://github.com/jonhue/acts_as_favoritor#acts_as_favoritor-methods, but i think that got killed as well.
I filled this as a bug but might a documentation update depending your intentions.
Breaking changes:
blocks
to blocked
for favoritablesblocks
to blocked_by
and blocked?
to blocked_by?
for favoritorsremove_favorite
to unfavorite
descending
and recent
class methods:all
scope2b5250a [#107] Release workflow (#109)
87fc798 [#106] CI workflow (#108)
234883d Update rspec-rails to version 3.9.0 (#105)
0740858 Update factory_bot to version 5.1.1 (#104)
0505216 Bump rubyzip from 1.2.2 to 1.3.0 in /spec/support/rails_app (#103)
a30188b Update rubocop: 0.74.0 β 0.75.0 (major) (#102)
80ed33f Update rubocop-rspec to version 1.36.0 (#101)
505fd70 Update factory_bot to version 5.1.0 (#100)
d3cdd59 Update all of rails: 5.2.3 β 6.0.0 (major) (#96)
dc8d7aa Bump nokogiri from 1.10.3 to 1.10.4 in /spec/support/rails_app (#97)
b5ad5d8 Update nokogiri to version 1.10.4 (#95)
862bb61 Update rubocop-rspec to version 1.35.0 (#94)
403c64f Update rubocop-rspec to version 1.34.1 (#93)
79514db Update rubocop-rspec to version 1.34.0 (#92)
fb8366b Update rubocop to version 0.73.0 (#91)
8c02b60 Update rubocop to version 0.72.0 (#90)
d41bcc7 Update rubocop to version 0.71.0 (#89)
8979bc8 Update rubocop to version 0.70.0 (#88)
f55f9ba [#85] Fix auto merge (#86)
remove_favorite
raise NoMethodError when cache is enabled and method called on object that is not liked
tv - acts_as_favoritable
u - acts_as_favoritor
To Reproduce
u.favorite(tv) # Ok
u.remove_favorite(tv) # Ok
u.remove_favorite(tv) # Fail
NoMethodError: undefined method `-' for nil:NilClass
gem 0.2.1
Remove the todos in .rubocop_todo.yml
.
This is simply not working for me. Console seems to say that favorites are created, but there's nothing then when I try to retrieve it.
`Created database 'db/test.sqlite3'
-- create_table("favorites", {:force=>:cascade})
-> 0.0625s
-- create_table("resources", {:force=>:cascade})
-> 0.0318s
-- create_table("users", {:force=>:cascade})
-> 0.0307s
-- create_table("favorites", {:force=>:cascade})
-> 0.0693s
-- create_table("resources", {:force=>:cascade})
-> 0.0274s
-- create_table("users", {:force=>:cascade})
-> 0.0322s
ec2-user:~/environment/knowhow (adding-save) $ rails console
Running via Spring preloader in process 6072
Loading development environment (Rails 5.2.0)
resource = Resource.first
Resource Load (0.2ms) SELECT "resources".* FROM "resources" ORDER BY "resources"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<Resource id: 1, title: "WaniKani", url: "https://www.wanikani.com/", content: "Learn over 2000 Japanese Kanji over the course of ...", created_at: "2018-06-10 20:05:29", updated_at: "2018-06-10 20:05:29">
user = User.first
User Load (0.1ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, email: "[email protected]", created_at: "2018-06-10 20:05:52", updated_at: "2018-06-10 20:05:52">
user.favorite resource
Favorite Load (0.2ms) SELECT "favorites".* FROM "favorites" WHERE "favorites"."favoritor_id" = ? AND "favorites"."favoritor_type" = ? AND "favorites"."favoritable_id" = ? AND "favorites"."favoritable_type" = ? AND "favorites"."scope" = ? ORDER BY "favorites"."id" ASC LIMIT ? [["favoritor_id", 1], ["favoritor_type", "User"], ["favoritable_id", 1], ["favoritable_type", "Resource"], ["scope", "favorite"], ["LIMIT", 1]]
(0.1ms) begin transaction
Resource Load (0.1ms) SELECT "resources".* FROM "resources" WHERE "resources"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
Favorite Create (2.2ms) INSERT INTO "favorites" ("favoritable_type", "favoritable_id", "favoritor_type", "favoritor_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["favoritable_type", "Resource"], ["favoritable_id", 1], ["favoritor_type", "User"], ["favoritor_id", 1], ["created_at", "2018-06-10 20:06:21.177285"], ["updated_at", "2018-06-10 20:06:21.177285"]]
(7.9ms) commit transaction
=> #<Favorite id: 1, favoritable_type: "Resource", favoritable_id: 1, favoritor_type: "User", favoritor_id: 1, scope: "favorite", blocked: false, created_at: "2018-06-10 20:06:21", updated_at: "2018-06-10 20:06:21">
user.favorited? resource
(0.3ms) SELECT COUNT() FROM "favorites" WHERE "favorites"."blocked" = ? AND "favorites"."scope" = ? AND "favorites"."favoritor_id" = ? AND "favorites"."favoritor_type" = ? AND "favorites"."favoritable_id" = ? AND "favorites"."favoritable_type" = ? [["blocked", 0], ["scope", "Favorite"], ["favoritor_id", 1], ["favoritor_type", "User"], ["favoritable_id", 1], ["favoritable_type", "Resource"]]
=> false
resource.favoritors_count
(0.2ms) SELECT COUNT() FROM "favorites" WHERE "favorites"."favoritable_id" = ? AND "favorites"."favoritable_type" = ? AND "favorites"."blocked" = ? AND "favorites"."scope" = ? [["favoritable_id", 1], ["favoritable_type", "Resource"], ["blocked", 0], ["scope", "Favorite"]]
=> 0
I'm having trouble removing this gem from my project as it was not working for me. After removing from my gemfile and bundling, and destroying the model, database migrations no longer work and throw me this erro
NameError: uninitialized constant ActsAsFavoritor /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/active_support.rb:43:in
load_missing_constant'
/home/ec2-user/environment/knowhow/config/initializers/acts_as_favoritor.rb:1:in <main>' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in
load'
/usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:50:in load' /home/ec2-user/environment/knowhow/config/environment.rb:5:in
require' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in
block in require_with_bootsnap_lfi'register' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:20:in
require_with_bootsnap_lfi'require' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:21:in
require'block in require_with_bootsnap_lfi' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/loaded_features_index.rb:65:in
register'require_with_bootsnap_lfi' /usr/local/rvm/gems/ruby-2.4.1/gems/bootsnap-1.3.0/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:29:in
require'<top (required)>' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in
load'call' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in
call'run' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in
<top (required)>'load' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in
<top (required)>'require' /home/ec2-user/environment/knowhow/bin/spring:15:in
<top (required)>'load' bin/rails:3:in
'For any model with acts_as_favoritable
included, and with a user model that has acts_as_favoritor
included, I get a "Validation failed: Favoritable must exist" under all circumstances, regardless of the user or model selected. The objects are persisted, and this problem happens in the console. I've tried remove everything else from either model in case there were conflicts but the same happens.
I'm using the repo's latest, and Rails 5.2.3. I'm not sure where to go from here!
Is your feature request related to a problem? Please describe.
I enable caching feature after my app already runs on production for some time. Because of that, the caching value doesn't reflect the true number
Describe the solution you'd like
A method/rake task to recalculate all caching values
Describe the bug
This method returns false when it really is true.
acts_as_favoritor/lib/acts_as_favoritor/favoritor.rb
Lines 22 to 41 in fe2c648
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Should return true if the favoritor has favorited the favoritable object.
Additional context
If you swap the logic out with acts_as_follower's code at https://github.com/tcocca/acts_as_follower/blob/master/lib/acts_as_follower/follower.rb#L20 and reboot your server, it returns true as expected.
def favorited?(favoritable)
0 < Favorite.unblocked.for_favoritor(self).for_favoritable(favoritable).count
end
There aren't any new features in this release.
Breaking changes:
:scope
option was renamed to :scopes
*_count
methodsRefactor code samples.
NoMethodError (undefined method '+' for nil:NilClass)
irb(main):040:0> begin
irb(main):041:1> User.first.favorite Product.last
irb(main):042:1> rescue => e
irb(main):043:1> puts e.backtrace
irb(main):044:1> raise e
irb(main):045:1> end
NoMethodError (undefined method `+' for nil:NilClass)
/Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/acts_as_favoritor-b84d096e75c6/lib/acts_as_favoritor/favoritor.rb:125:in `favorite'
/Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/acts_as_favoritor-b84d096e75c6/lib/acts_as_favoritor/favoritor_lib.rb:50:in `validate_scopes'
/Users/username/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/acts_as_favoritor-b84d096e75c6/lib/acts_as_favoritor/favoritor.rb:105:in `favorite'
[10] pry(main)> ActsAsFavoritor::VERSION
=> "4.0.0"
[11] pry(main)> ActsAsFavoritor.configuration.default_scope
=> :favorite
[12] pry(main)> ActsAsFavoritor.configuration.cache
=> true
[13] pry(main)> User.last.favorite Product.last
NameError: undefined local variable or method `scope' for #<User:0x00007fac321e9cf0>
from /Users/foo/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activemodel-5.2.3/lib/active_model/attribute_methods.rb:430:in `method_missing'
I think something is up here:
https://github.com/jonhue/acts_as_favoritor/blob/master/lib/acts_as_favoritor/favoritor.rb#L133
[10] pry(main)> ActsAsFavoritor::VERSION
=> "4.0.0"
[11] pry(main)> ActsAsFavoritor.configuration.default_scope
=> :favorite
[12] pry(main)> ActsAsFavoritor.configuration.cache
=> true
[13] pry(main)> User.last.favorite Product.last
NameError: undefined local variable or method `favoritable' for #<User:0x00007ff366a70b00>
from /Users/foo/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activemodel-5.2.3/lib/active_model/attribute_methods.rb:430:in `method_missing'
It does not works with Rails 5.2 as it use ApplicationRecord
as base class for models instead of ActiveRecord::Base
. So I get undefined method 'relation_delegate_class'
error when use it with my models. But it works good with models derived from ActiveRecord::Base. I really do not know how to fix that
When the scopes
parameter is set, the result should be returned in the form of a hash.
When the scopes
parameter is not set, the result should be returned as a literal using the scope
argument (defaults to default scope).
Breaking changes:
user.favorited?(book, scopes: [:purchase])
) now returns a hash as a result (e.g. { purchase: true }
). Use user.favorited?(book, scope: :purchase)
to receive the same result as in earlier versions.4c52e71 [#136] Allow either a single scope
or multiple scopes
to be given (#137)
af354c1 Update rspec-rails to version 3.9.1 (#134)
cc02985 Update rubocop to version 0.80.1 (#132)
811f8b9 Update rubocop to version 0.80.0 (#131)
59ac112 Update rubocop-rspec to version 1.38.1 (#130)
56f1912 Update rubocop-rspec to version 1.38.0 (#129)
288a463 Update nokogiri to version 1.10.8 (#128)
137c981 Update rubocop to version 0.79.0 (#127)
1fe95d2 Update all of rails to version 6.0.2.1 (#125)
f6a0f33 Update rubocop-rspec to version 1.37.1 (#121)
dc05526 Update sqlite3 to version 1.4.2 (#124)
683055a Update rack to version 2.0.8 (#122)
b3d15bb Update rubocop: 0.77.0 β 0.78.0 (major) (#126)
cdea494 Update all of rails to version 6.0.2 (#120)
b02fc85 Update rubocop-rspec: 1.36.0 β 1.37.0 (minor) (#117)
1d400f4 Bump puma from 3.12.1 to 3.12.2 in /spec/support/rails_app (#119)
7e83a1d Update rubocop to version 0.77.0 (#118)
53c4c3c Update all of rails: 6.0.0 β 6.0.1 (patch) (#115)
5fb9b55 Bump loofah from 2.2.3 to 2.3.1 in /spec/support/rails_app (#116)
cb8014a Update rubocop to version 0.76.0 (#114)
0149f09 Update loofah to version 2.3.1 (#113)
90854e1 Update rubocop to version 0.75.1 (#112)
user.follow(second_user)
instead of user.favorite(second_user, scope: [:follow])
Describe the bug
First thanks for your OSS contribution !
I'm trying to make it work with cache enabled (cannot repro the bug with cache disabled) (Using upstream version 442e2cd752684f180b712b8abeedaab22ab40e6c
)
When doing
current_user.favorite @product
The following exceptions is raised: undefined method [] for nil:NilClass
.
acts_as_favoritor-442e2cd75268/lib/acts_as_favoritor/favoritor.rb:122:in `favorite'
acts_as_favoritor-442e2cd75268/lib/acts_as_favoritor/favoritor_lib.rb:50:in `validate_scopes'
acts_as_favoritor-442e2cd75268/lib/acts_as_favoritor/favoritor.rb:102:in `favorite'
This stacktrace is referencing this line: https://github.com/jonhue/acts_as_favoritor/blob/master/lib/acts_as_favoritor/favoritor.rb#L122
Here are my models:
class User < ApplicationRecord
acts_as_favoritor
end
class Product < ApplicationRecord
acts_as_favoritable
end
Here is my cache migration:
class AddFavoritesCacheToUserAndProductTable < ActiveRecord::Migration[5.2]
def change
add_column :users, :favoritor_score, :text
add_column :users, :favoritor_total, :text
add_column :products, :favoritable_score, :text
add_column :products, :favoritable_total, :text
end
end
Looks like this favoritor_score
is never initialized ?
Regards
A declarative, efficient, and flexible JavaScript library for building user interfaces.
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. πππ
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google β€οΈ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.