Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use base_class instead of superclass in parent_class_name #56

Merged
merged 4 commits into from Jan 31, 2017

Conversation

brchristian
Copy link
Contributor

I was having a problem supporting polymorphic models in my application, and traced it back to this line.

If there are multiple levels of inheritance, this will go back one level -- but only exactly one.

For instance, let's say I have

Fruit
Fruit::Apple
Fruit::Apple::Fuji

When I follow a Fruit, it's stored in the db as follower_type = "Fruit".
When I follow a Fruit::Apple, it's also stored in the db as follower_type = "Fruit".
But, when I follow a Fruit, it's stored in the db as follower_type = "Fruit::Apple". This causes all kinds of things to break in my app.

Using base_class instead of superclass appears to give the intended behavior here.

I believe this PR may also address the issues described in #8.

I believe this may also be a more general solution for the use of abstract classes than the one given in #42, as the docs for the base_class method say "Returns the class descending directly from ActiveRecord::Base, or an abstract class" (my emphasis). That said, I haven't looked into it more deeply than that.

@bricesanchez
Copy link

Any news about this PR? i need it in my app :)

@amoludage
Copy link
Contributor

Hi @bricesanchez I think the @Owners of this repository is not maintaining this repository, because I have seen lots of PR's pending into this repo and no one is replying on them.

So if you want to use this functionality into your app you can add this https://github.com/brchristian/acts_as_follower/tree/patch-1 forked repository into your Gemfile.

Example below

gem "acts_as_follower", github: "brchristian/patch-1"

Thanks.

@jeremylynch
Copy link

@jcoyne any chance we could get this merged? Its necessary for Rails 5 compatibility. Thanks

@jcoyne
Copy link
Collaborator

jcoyne commented Dec 16, 2016

@mrjlynch Sorry, I'm no longer maintaining this as my employer has stopped using it.

@jcoyne
Copy link
Collaborator

jcoyne commented Dec 16, 2016

Is there a test for this behavior? Does it work under Rails 4 as well as Rails 5?

@brchristian
Copy link
Contributor Author

brchristian commented Dec 24, 2016

@jcoyne This area of the code was modified when #76 was merged, so I would need to look at the changes and rebase. I’ll work on getting a test and follow up. Should be compatible with both Rails 4 and 5.

@brchristian
Copy link
Contributor Author

@jcoyne In my opinion, #76 introduced a large amount of complexity which is actually not necessary, for either Rails 5-compatibility or for STI-compatibility. As discussed in #42, the base_class method handles abstract classes elegantly, and so no extra shenanigans are necessary when dealing with the ApplicationRecord abstract class introduced in Rails 5.

My suggested PR here is based on the idea that supporting "custom parent classes" is not something acts_as_follower needs to do by default (I have a hard time thinking of a use case for this), and that base_class handles all of the Rails 5 issues and the issues discussed in #42 and in this thread. The upshot is a dramatically simpler codebase, compatible with Rails 4 and 5, and with no STI hiccups.

I have rebased this branch on the current master, and following the "red, green, refactor" pattern have included:

  1. a failing test (845b273)
  2. a patch applying base_class to fix the test (81833d0)
  3. a refactor that simplifies to the minimal changes needed to make the tests pass (48b22a6)

Let me know if there’s anything else I can do to help out here, or any issues I’ve overlooked. Cheers!

@jeremylynch
Copy link

Hi @jcoyne, any chance we could get this merged? Thanks

Copy link
Collaborator

@jcoyne jcoyne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Just a few issues about changing the public API without depreciations

yield @configuration if block_given?
end

def self.method_missing(method_name, *args, &block)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not backward compatible. This method should be retained and a deprecation should be emitted

@@ -6,25 +6,5 @@ module ActsAsFollower
autoload :FollowerLib, 'acts_as_follower/follower_lib'
autoload :FollowScopes, 'acts_as_follower/follow_scopes'

def self.setup
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is not backward compatible. This method should be retained and a deprecation should be emitted

@brchristian
Copy link
Contributor Author

@jcoyne Restored the custom_parent_classes functionality and added deprecation warnings. 👍

@brchristian
Copy link
Contributor Author

@jcoyne Anything else needed in this branch, or we good to merge?

@jcoyne
Copy link
Collaborator

jcoyne commented Jan 31, 2017

@brchristian thanks for the reminder. Good work.

@jcoyne jcoyne merged commit 72dfdff into tcocca:master Jan 31, 2017
@brchristian brchristian deleted the patch-1 branch January 31, 2017 21:32
@brchristian
Copy link
Contributor Author

Welcome! Cheers!

@brchristian
Copy link
Contributor Author

@johnmpotter I saw that you forked this PR branch of my repo. Now that it is merged, it looks like you can get back on to the master branch here if you like!

@johnmpotter
Copy link

@brchristian Awesome, that makes life easier, thanks for your work on this! 😃

@PelagicDev
Copy link

PelagicDev commented Feb 1, 2017

Just to be clear, this PR now deprecates the use of user.following_users & user.following_by_type('Book'); is this correct?

Going forward, we should avoid using these methods?

@brchristian
Copy link
Contributor Author

@jegodwin The deprecated behavior is simply the use of explicit custom "parent classes" via things like

ActsAsFollower.custom_parent_classes = [CustomRecord]

or

ActsAsFollower.setup do |c|
  c.custom_parent_classes = [CustomRecord]
end

Your examples of following_users and following_by_type should be unaffected by this PR.

@PelagicDev
Copy link

@brchristian okay, got it. I was seeing the deprecation warning in the rails server logs whenever I called the following_users and following_by_type('Model') methods. So, I thought that it was referring to those methods.

Thanks for clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants