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

Display helpful message if attribute_name missing #251

Closed
wants to merge 6 commits into from
Closed

Display helpful message if attribute_name missing #251

wants to merge 6 commits into from

Conversation

5minpause
Copy link
Contributor

Problem

If a field in COLLECTION_ATTRIBUTES has no key in ATTRIBUTE_TYPES either because I misspelled it or I forget to add it to ATTRIBUTE_TYPES I will get a undefined method new for nil:NilClass in Page::Base

Solution

Don't access hash values directly but use fetch. If the attribute is not found the block raises an error with a helpful error message.

Closes #246

# Problem
If a field in `COLLECTION_ATTRIBUTES` has no key in `ATTRIBUTE_TYPES` either because I misspelled it or I forget to add it to `ATTRIBUTE_TYPES` I will get a undefined method new for `nil:NilClass` in `Page::Base`

# Solution
Catches the `NoMethodError` and reraises it with a proper message.

references #246
# Problem
If a field in `COLLECTION_ATTRIBUTES` has no key in `ATTRIBUTE_TYPES` either because I misspelled it or I forget to add it to `ATTRIBUTE_TYPES` I will get a undefined method new for `nil:NilClass` in `Page::Base`

# Solution
Don't access hash values directly but use `fetch`. If the attribute is not found the block raises an error with a helpful error message.

references #246
@5minpause
Copy link
Contributor Author

Thanks for your feedback @monkbroc and @Graysonwright.
I modified the commit and opened this pull request.

Do we need tests for this? Would this be a feature test? I am asking for suggestions.

@monkbroc
Copy link
Contributor

@5minpause if you give me commit access to the repository https://github.com/ikuseiGmbH/administrate I'll write a test and push it to this pull request.

@c-lliope c-lliope added this to the v0.1.2 milestone Nov 18, 2015
@5minpause
Copy link
Contributor Author

@monkbroc I added you. I am curious what you'll do. Looking forward to learning something new. 😀

fields = dashboard.attribute_types_for(:name, :email)
expect(fields).to match(
name: Administrate::Field::String,
email: Administrate::Field::Email

Choose a reason for hiding this comment

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

Put a comma after the last parameter of a multiline method call.

@monkbroc
Copy link
Contributor

After looking at this more, it seems that the concern of attributes existing in a dashboard belongs in the dashboard class (surprise!).

It's easier to test the behavior there (I added 4 unit tests for the dashboard class).

My change also decreases the knowledge collaborators like page classes have to know about the internals of the dashboard class (attribute_types is a hash I can use fetch and slice on).

Regarding Hound: have you thought of making your line length a little longer? 80 seems rather short for Ruby. I've had better luck with 90 or 100.

This decreases the knowledge that collaborators need about the internal structure of the dashboard class.
@c-lliope
Copy link
Contributor

Whoa, this looks great!

it "raises an exception" do
dashboard = CustomerDashboard.new
expect { dashboard.attribute_types_for(:name, :foo) }.
to raise_error /Attribute foo not in CustomerDashboard/
Copy link
Contributor

Choose a reason for hiding this comment

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

How would you feel about extracting a method here?

expect { dashboard.attribute_types_for(:name, :foo) }.
  to raise_error(message_for("foo", "CustomerDashboard"))


def message_for(attribute, dashboard_class)
  "Attribute #{attribute} could not be found in #{dashboard_class}::ATTRIBUTE_TYPES"
end

Note: I also slightly changed the wording of the error message in that snippet, I think it reads a bit better.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree about the wording. That's what I had originally but the Hound was against it since it's 95 characters long 😆

I'll put it back

@c-lliope
Copy link
Contributor

Left a small comment, but this looks good to merge after that.

@@ -24,6 +24,50 @@
end
end


Choose a reason for hiding this comment

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

Trailing whitespace detected.

@@ -24,6 +24,49 @@
end
end

def missing_attribute_message(attribute, dashboard_class)
"Attribute #{attribute} could not be found in #{dashboard_class}::ATTRIBUTE_TYPES"

Choose a reason for hiding this comment

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

Line is too long. [86/80]

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm ok ignoring hound for this line.

@c-lliope
Copy link
Contributor

Left one more small comment (in reply to hound above), then this is good to go! Thanks!

@monkbroc
Copy link
Contributor

Done.

@c-lliope
Copy link
Contributor

Thanks! Merged in as d212265.

@c-lliope c-lliope closed this Nov 19, 2015
c-lliope added a commit that referenced this pull request Dec 9, 2015
Changes:

```
* [#251] [FEATURE] Raise a helpful error when an attribute is missing from
  `ATTRIBUTE_TYPES`
* [#298] [FEATURE] Support ActiveRecord model I18n translations
* [#312] [FEATURE] Add a `nil` option to `belongs_to` form fields
* [#231] [UI] Fix layout issue on show page where a long label next to an empty
  value would cause following fields on the page to be mis-aligned.
* [#309] [UI] Fix layout issue in datetime pickers where months and years
  would not wrap correctly.
* [#306] [UI] Wrap long text lines (on word breaks) on show pages
* [#214] [UI] Improve header layout when there is a long page title
* [#198] [UI] Improve spacing around bottom link in sidebar
* [#206] [UI] Left-align checkboxes in boolean form fields
* [#315] [UI] Remove the `IDS` suffix for `HasMany` form field labels
* [#259] [BUGFIX] Make installation generator more robust
  by ignoring dynamically generated, unnamed models
* [#243] [BUGFIX] Fix up a "Show" button on the edit page that was not using the
  `display_resource` method.
* [#248] [BUGFIX] Improve polymorphic relationship's dashboard class detection.
* [#247] [BUGFIX] Populate `has_many` and `belongs_to` select boxes
  with the current value of the relationship.
* [#217] [I18n] Dutch
* [#263] [I18n] Swedish
* [#272] [I18n] Danish
* [#270] [I18n] Don't apologize about missing relationship support.
* [#237] [I18n] Fix broken paths for several I18n files (de, es, fr, pt-BR, vi).
* [#266] [OPTIM] Save a few database queries by using cached counts
```
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.

4 participants