-
Notifications
You must be signed in to change notification settings - Fork 336
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
Allow customizing the model name #154
base: master
Are you sure you want to change the base?
Allow customizing the model name #154
Conversation
Why do you need your own Activity class? What does it give you? |
This is the current contents of the class, more or less: class Activity < ::PublicActivity::Activity
def self.unread
where(read: false)
end
def unread?
not read?
end
def created_on
created_at.to_date
end
def mark_read!
update_attributes!(read: true)
end
end The Even if we put these particular extensions aside, having the activity model external and unmodifiable makes it hard to find a place to fit this kind of logic. The library can't provide an interface for all use cases. And while for a lot of things it's possible to extend them from the outside (it's not necessary to use |
Hi Andrew! That is an interesting idea and the best approach in my opinion, too. 👍 Though one thing I see missing is we must make sure the custom Model inherits from PublicActivity::Activity. I can already see people forgetting this and destroying the functionality. Not quite sure where we would add that restriction in the code. Any ideas? |
Unless you need different versions of Activity in one app (one with
Exactly why I'm reluctant about all of this. |
@farnoy has a valid point, too. This is how I personally extend the https://github.com/pokonski/public_activity/wiki/%5BHow-to%5D-Use-custom-fields-on-Activity#setup |
Yeah, that's true. It would make sense to add a hook somewhere to check if the model One alternative would be to provide a
Yes, and as I mentioned, I'm not a fan of this approach. It means that application code with potentially non-trivial business logic (methods that you would call often) is stuck in an initializer, outside of the A more practical problem is that putting it there means any changes require a restart of the server, as opposed to changes to classes in I think an interesting parallel could be made with devise. While it does provide tons of functionality to the Naturally, this will be a maintentance burden to you as owners of the project, so I'd understand if you reject the PR on these grounds. I don't feel it's a very risky change (in fact, I was very happy with how easy it was to make), but that's for you to decide. We can use our own fork for now and see how it goes. |
I think code extending the Activity class can be stored in app/models, or p_a can be adjusted to allow for that. Also, when you inherit from us and we change something internal, you're doomed aswell. This would obviously be a dependency. If your requirements are far higher than what we provide, then I'd recommend either forking this project or just taking parts into your app. Your remark about devise is interesting indeed, but there's inherent complexity from that approach and I'm not sure if we want to dive into that. We provide two modules of functionality that you can inherit: I think our approach is okay for what we'd like to stay a simple library and complicating this just isn't our desire. |
I don't agree. People forked a lot just to add some basic attributes and that was a bad way. That's why we added custom attributes documentation so people don't feel forking is the only way. And forks get out of date very fast.
We haven't really consulted this ;) So it is your vision.
I agree that class_eval is more of a hack than a solution. Also a valid point about generating your own Activity model, like devise does. This idea I really like. Though it would require adding generators for that, so new users don't have to type it by hand. |
If that's possible, that would be a reasonable solution to my issues with it. I'm not sure if autoloading could work, though, I think rails really expects you to define a class in the appropriately named file. Still, I haven't tried. I'd definitely accept a working solution along these lines instead of mine.
Yes, devise is a much more complex project and I'm sure they pay for the complexity in terms of maintenance costs. I mentioned it mostly for the sake of the mental model.
Yes, you're right. That's one reason it might make sense to have a |
@AndrewRadev sorry for no reply for over six months, we are definitely implementing this one way or the other because you had a good idea. We are in the process of rewriting p_a for 2.0 release which will be backwards incompatible and will allow for easier customization. Which is something your PR can help with. |
No worries, I know how hard it is to keep up with maintenance of an open source project. Let me know if there's anything I can help out with. |
I just wanted to make PR like this and found this one. Hope this option will be available in the future, cause it'll be really great to use own model with other features provided by gem. |
@pokonski I'm working on an app that needed to customize I like @AndrewRadev suggestion here and wanted to ask if this pull request could be merged. If you are interested in this feature but the PR needs some revisions, I'd be happy to look into it. Thanks! |
The
Activity
model comes from the gem, which means it's not very convenient to customize. I'd rather have a domain model in my app that I can extend. It's still somewhat possible to plug custom extensions with an initializer that monkey-patches the::PublicActivity::Activity
class, but this doesn't work well with autoloading and it has to be placed in a different location than all other models.The change needed to support this is actually quite simple, the string
::PublicActivity::Activity
is replaced with a config option. This should provide perfect backwards compatibility and a new hook to plug in your own class (in my case,class ::Activity < ::PublicActivity::Activity
) that is a full-fledged AR model, owned by the app.Let me know if you'd like me to add more tests or documentation, I wasn't sure how far I should go in that direction.