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

More helpful error message when instantiating an abstract class #9474

Merged
merged 1 commit into from
Mar 7, 2013

Conversation

HonoreDB
Copy link
Contributor

Calling a literal ActiveRecord::Base.new raises NoMethodError,
since it ends up calling Class.abstract_class? which does not exist.
Similarly, instantiating an actual abstract class hits the database,
when conventionally it should immediately throw NotImplementedError.

ActiveRecord::Base can't be made abstract without breaking many,
many things, so check for it separately.

@senny
Copy link
Member

senny commented Feb 28, 2013

can you write two test cases to verify that AR::Base cant be instantiated and that an abstract class can't be instantiated?

Also we would need a CHANGELOG entry explaining the change.

@schneems
Copy link
Member

schneems commented Mar 3, 2013

@HonoreDB comment-ors don't get notified when you push, you have to reply to the thread for them to know. @senny care to take a second look?

@@ -1,5 +1,9 @@
## Rails 4.0.0 (unreleased) ##

* Throw NotImplementedError when trying to instantiate Base or an abstract class.
Copy link
Member

Choose a reason for hiding this comment

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

just a nitpick but it would be nice to put the constant name inside .

@senny
Copy link
Member

senny commented Mar 3, 2013

added a few minor comments.

@HonoreDB
Copy link
Contributor Author

HonoreDB commented Mar 3, 2013

Thanks, comments addressed in HonoreDB@f128e79.

def test_initialize_abstract_class
assert_raises(NotImplementedError) do
FirstAbstractClass.new
end
Copy link
Member

Choose a reason for hiding this comment

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

you should assign the exception to check the message:

e = assert_raises(XX) {}

assert_equals "<expected>", e.message

@senny
Copy link
Member

senny commented Mar 3, 2013

the PR does no longer cleanly apply to master. Can you push a rebased version?

Calling a literal ActiveRecord::Base.new raises NoMethodError,
since it ends up calling Class.abstract_class? which does not exist.
Similarly, instantiating an actual abstract class hits the database,
when conventionally it should immediately throw NotImplementedError.

ActiveRecord::Base can't be made abstract without breaking many,
many things, so check for it separately.
@HonoreDB
Copy link
Contributor Author

HonoreDB commented Mar 3, 2013

Rebased, tests now check the error message in addition to the type.

@senny
Copy link
Member

senny commented Mar 3, 2013

@HonoreDB amazing! thanks for the fast update. 💛

@carlosantoniodasilva @rafaelfranca can you take a final look?

rafaelfranca added a commit that referenced this pull request Mar 7, 2013
More helpful error message when instantiating an abstract class

Conflicts:
	activerecord/CHANGELOG.md
@rafaelfranca rafaelfranca merged commit 53f18f2 into rails:master Mar 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants