-
Hey, I've run into a little design snag that I'd love to get some advice on. I'll explain my situation real quick for some context.
And this notification controller which I'm unsure about.
A user can create/follow (meaning they get notifications for) a Page and a Post and will get notifications for any replies to their comments. This means a comment can be created 3 different ways, and should have a different url and message for each one. I am currently switching on these cases within the controller. Does this sound like a solid plan? I considered having different controllers for each Notification case (e.g. NestedCommentNotification, PageCommentNotification, PostCommentNotification). It seems like that would then add a lot of unnecessary complexity when trying to get total notification counts and marking notifications as read, and a lot of repetition. I've also considered I may be stretching the responsibilities of the Comment model too thin trying to apply it to both Pages and Posts, but the discussion requirements were the exact same for each case so it made sense to simply use the Comment model in both cases. I was wondering if anyone else has run into something similar and has any insight. I haven't completely built out the functionality above due to my concerns, but the example below should have enough to display what I'm talking about above. class CommentNotification < Noticed::Base
deliver_by :database
param :comment
# haven't applied the switch functionality here yet but you get the idea that I would be switching on each case
# to produce/use a different message.
def message
comment = params[:comment]
if comment.is_nested
t(".message", parent_user_display_name: params[:comment].parent.user.display_name)
end
end
def url
comment = params[:comment]
case comment.commentable
when Post
post = comment.commentable
return post_path(post.id)
when Page
page = comment.commentable
return wiki_page_path(page.wiki.id, page.id)
end
end
end |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Great question! You can really set it up however you like. For polymorphic comments, I've actually setup namespaced controllers and include shared functionality with a concern. This makes it so you can branch out as necessary if you want comments to work slightly different for one model than another. Most of the time I don't override anything though. In your There should be a way to override class Page
def to_param
"/wikis/#{wiki.id}/pages/#{id}"
end
end That would allow you to treat all the def url
url_for([params[:comment].commentable])
end |
Beta Was this translation helpful? Give feedback.
Great question! You can really set it up however you like.
For polymorphic comments, I've actually setup namespaced controllers and include shared functionality with a concern. This makes it so you can branch out as necessary if you want comments to work slightly different for one model than another. Most of the time I don't override anything though.
In your
url
method, the only non-standard thing I see is thewiki_page_path
that makes that conditional required.There should be a way to override
to_param
or something on your Page model to automatically include the Wiki ID. I believe something like this should work:Th…