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
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ def attribute_types
self.class::ATTRIBUTE_TYPES
end

def attribute_type_for(attribute_name)
attribute_types.fetch(attribute_name) do
fail "Attribute #{attribute_name} not in #{self.class}::ATTRIBUTE_TYPES"
end
end

def attribute_types_for(*attribute_names)
Copy link
Contributor

Choose a reason for hiding this comment

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

same here for the splat operator.

attribute_names.each_with_object({}) do |name, attributes|
attributes[name] = attribute_type_for(name)
end
end

def form_attributes
self.class::FORM_ATTRIBUTES
end
Expand Down
6 changes: 2 additions & 4 deletions lib/administrate/page/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ def resource_name

def attribute_field(dashboard, resource, attribute_name, page)
value = get_attribute_value(resource, attribute_name)

dashboard.
attribute_types[attribute_name].
new(attribute_name, value, page)
field = dashboard.attribute_type_for(attribute_name)
field.new(attribute_name, value, page)
end

def get_attribute_value(resource, attribute_name)
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/page/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def attributes_for(resource)
end

def attribute_types
dashboard.attribute_types.slice(*attribute_names)
dashboard.attribute_types_for(*attribute_names)
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's get rid of the splat operator here (*), and pass the array directly.

end

def ordered_html_class(attr)
Expand Down
39 changes: 39 additions & 0 deletions spec/dashboards/customer_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,45 @@
end
end

describe "#attribute_type_for" do
context "for an existing attribute" do
it "returns the attribute field" do
dashboard = CustomerDashboard.new
field = dashboard.attribute_type_for(:name)
expect(field).to eq Administrate::Field::String
end
end

context "for a non-existent attribute" do
it "raises an exception" do
dashboard = CustomerDashboard.new
expect { dashboard.attribute_type_for(:foo) }.
to raise_error /Attribute foo not in CustomerDashboard/
end
end
end

describe "#attribute_types_for" do
context "for existing attributes" do
it "returns the attribute fields" do
dashboard = CustomerDashboard.new
fields = dashboard.attribute_types_for(:name, :email)
expect(fields).to match(
name: Administrate::Field::String,
email: Administrate::Field::Email,
)
end
end

context "for one non-existent attribute" do
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

end
end
end

describe "#display_resource" do
it "returns the customer's name" do
customer = double(name: "Example Customer")
Expand Down