Skip to content

s/find_by_slug/friendly_find!/ and use everywhere#482

Merged
glebm merged 4 commits intomasterfrom
friendly-find
Nov 20, 2016
Merged

s/find_by_slug/friendly_find!/ and use everywhere#482
glebm merged 4 commits intomasterfrom
friendly-find

Conversation

@glebm
Copy link
Copy Markdown
Collaborator

@glebm glebm commented Nov 19, 2016

This is to avoid confusion with the Rails dynamic finder methods

This is to avoid confusion with the Rails dynamic finder methods
Copy link
Copy Markdown
Collaborator

@timdiggins timdiggins left a comment

Choose a reason for hiding this comment

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

Makes sense, but some small comments (which are not 100% part of this PR)

end

# Returns the `@messageboard` instance variable.
# If `@messageboard` is not set, it first sets it to the topic with the slug or ID given by
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

docs seem odd here -- "topic" ?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Oops, copy-paste accident. Fixed.

# `params[:messageboard_id]`.
#
# @return [Thredded::Messageboard]
# @raise [Thredded::Errors::MessageboardNotFound] if the topic with the given slug does not exist.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

... "messageboard" ... ?

Also why are we using MessageboardNotFound rather than the more normal (but less specific) ActiveRecord::NotFound -- can see it's a style across different models, but wondering what it gives us

Copy link
Copy Markdown
Collaborator Author

@glebm glebm Nov 19, 2016

Choose a reason for hiding this comment

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

We're a custom error so that they can be handled it in a custom way by Thredded or main app. Currently the default Thredded behaviour is to render the "not found" template with exception.message for these custom errors (

rescue_from Thredded::Errors::MessageboardNotFound,
Thredded::Errors::PrivateTopicNotFound,
Thredded::Errors::TopicNotFound,
Thredded::Errors::UserNotFound do |exception|
@error = exception
@message = exception.message
render template: 'thredded/error_pages/not_found', status: :not_found
end
). Maybe there is a better way to do this?

Copy link
Copy Markdown
Member

@jayroh jayroh Nov 20, 2016

Choose a reason for hiding this comment

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

Also why are we using MessageboardNotFound rather than the more normal (but less specific) ActiveRecord::NotFound -- can see it's a style across different models, but wondering what it gives us

@timdiggins - The generic exception is fine but there was a time when I wrote this that I wanted a specific error message to show up with regards to a messageboard not existing.

rescue ActiveRecord::RecordNotFound
raise Thredded::Errors::MessageboardNotFound
@messageboard ||= begin
fail Thredded::Errors::MessageboardNotFound unless params[:messageboard_id].presence
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Bit of a nit pick, but I feel a bit uncomfortable with this style (hard to read) (previous version was odd too IMO).

Also -- not sure why, but the behaviour seems to change in this PR. Previously it was not raising MessageboardNotFound when messageboard_id empty, now it is...

Instead maybe extract the check to a required_messageboard_id or messageboard_id! (?!) method which does params[:messageboard_id].presence || fail Thredded::Errors::MessageboardNotFound Will make this method super simple

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

So, currently, messageboard, topic, and private_topic all fail if there is no messageboard. The previous behaviour was not correct and not consistent with topic and private_topic.

But actually, I've just tried calling friendly.find(nil) and it raises ActiveRecord::NotFound, so the check is not necessary! Removed.

@timdiggins
Copy link
Copy Markdown
Collaborator

@glebm regarding the errors: Feel like it would be less surprising if the errors extended (were specializations) of ActiveRecord::NotFound. However that's not part of this PR which is all good. Could look at that later if we care enough.

@timdiggins timdiggins closed this Nov 20, 2016
@glebm glebm reopened this Nov 20, 2016
@glebm glebm merged commit dcd4051 into master Nov 20, 2016
@glebm glebm deleted the friendly-find branch November 20, 2016 08:59
@glebm glebm added this to the v0.9.0 milestone Nov 24, 2016
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.

3 participants