Code Monkey home page Code Monkey logo

acts_as_favoritor's Introduction

Hello, I'm Jonas

I am a doctoral researcher in computer science at ETH ZΓΌrich.

Feel free to reach out to me through [email protected].

acts_as_favoritor's People

Contributors

7200rpm avatar criccomini avatar dependabot[bot] avatar depfu[bot] avatar djaircarvalho avatar graysonchen avatar jbpfran avatar jonhue avatar kwent avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

acts_as_favoritor's Issues

Issue with scope string

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.

scope capitalized within #favorited_by? and # favorited?

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.

fav-scope-bug

Additional context
This might be related to #32

Undefined method '+' for :scope:Symbol

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 πŸ‘‡

screenshot 2019-03-08 at 4 40 02 pm

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:

  • OS: MacOS Mojave 10.14.3
  • Browser: Chrome
  • Version 72.0.3626.119

favorited_by_type and return type

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.

Release 4.0.0

Breaking changes:

  • changing how blocking/unblocking works
    • only favoritables can block favoritors now
    • renaming blocks to blocked for favoritables
    • renaming blocks to blocked_by and blocked? to blocked_by? for favoritors
  • renaming remove_favorite to unfavorite
  • removing descending and recent class methods
  • removing the special handling of the :all scope

Release 4.0.3

2b5250a [#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 an error when cache is enabled

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

No favoritors/favorited? returns False after inserting Favorite.

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

Can't uninstall this gem properly.

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

'
/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' /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'
/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' /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'
/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' /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'
/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' /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'
/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' /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'
/home/ec2-user/environment/knowhow/bin/rails:9:in <top (required)>' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in load'
/usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in call' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in call'
/usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/client.rb:30:in run' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/bin/spring:49:in <top (required)>'
/usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in load' /usr/local/rvm/gems/ruby-2.4.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in <top (required)>'
/home/ec2-user/environment/knowhow/bin/spring:15:in require' /home/ec2-user/environment/knowhow/bin/spring:15:in <top (required)>'
bin/rails:3:in load' bin/rails:3:in '
Tasks: TOP => db:reset => db:drop => db:load_config => environment`

"Validation failed: Favoritable must exist"

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!

Recalculate caching value

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

`#favorited?` does not work

Describe the bug
This method returns false when it really is true.

# Returns true if this instance has favorited the object passed as an
# argument.
def favorited?(favoritable, options = {})
if options.key?(:multiple_scopes) == false
options[:parameter] = favoritable
validate_scopes(__method__, options)
elsif options[:multiple_scopes]
results = {}
options[:scope].each do |scope|
results[scope] = Favorite.unblocked.send(scope + '_list')
.for_favoritor(self)
.for_favoritable(favoritable).count
.positive?
end
results
else
Favorite.unblocked.send(options[:scope] + '_list').for_favoritor(self)
.for_favoritable(favoritable).count.positive?
end
end

To Reproduce
Steps to reproduce the behavior:

  1. Setup as normal and use the method
    showterm example: http://showterm.io/7e6c76d697f66da491df5#

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

Release 3.0.0

There aren't any new features in this release.

Breaking changes:

  • :scope option was renamed to :scopes
  • removed *_count methods

Caching not working correctly

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'

Validation failed: Favoritor must exist

Screenshot from 2019-05-18 10-31-23

Hello. I have this problem and I do not know how to fix it. In the past, it worked smoothly but now something has happened.
In the Profile model, I have acts_as_favoritor and in the Job model, I have acts_as_favoritable. Both current_profile and job is not nil or something.

Thanks

undefined local variable or method `scope' when cache is used

[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

Undefined local variable or method `favoritable' when using caching

[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'

Not working with Rails 5.2

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

Release 5.0.0

Breaking changes:

  • Passing an array containing a single scope to a method (e.g. 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)

undefined method `[]' for nil:NilClass when caching enabled

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.