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

Consistent custom dashboards #2184

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def dashboard_from_resource(resource_name)

def model_from_resource(resource_name)
dashboard = dashboard_from_resource(resource_name)
dashboard.try(:model) || resource_name.to_sym
dashboard.try(:model) || resource_name.singularize.to_sym
end

def display_resource_name(resource_name, opts = {})
Expand Down
9 changes: 4 additions & 5 deletions docs/adding_controllers_without_related_model.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ Sometimes you may want to add a custom controller that has no resource
related to it (for example for a statistics page).

To do that, you must define an `index` route, as only controllers with index
routes are displayed in the sidebar and then add a custom dashboard:
routes are displayed in the sidebar, and then add a custom dashboard:

```erb
# app/views/admin/stats/index.html.erb
# app/views/admin/stats/index.html.erb

<div style="padding: 20px">
<div>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The example is copied straight from spec/example_app/app/views/admin/stats/index.html.erb. I have removed the padding because it's not relevant here.

<h1>Stats</h1>
<br>
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></p>
<br>
<p><b>Total Orders:</b> <%= @stats[:order_count] %></p>
</div>
```
Expand All @@ -25,7 +24,7 @@ routes are displayed in the sidebar and then add a custom dashboard:
require "administrate/custom_dashboard"

class StatDashboard < Administrate::CustomDashboard
resource "Stats" # used by administrate in the views
resource "Stat" # used by administrate in the views
end
```

Expand Down
4 changes: 2 additions & 2 deletions lib/administrate/custom_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class CustomDashboard

class << self
def resource_name(_opts)
named_resource.pluralize.titleize
@named_resource.pluralize.titleize
end

def resource(resource_name)
define_singleton_method(:named_resource) { resource_name }
@named_resource = resource_name
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change is not strictly necessary, but I felt that the current code was needlessly complicated and could be simplified like this.

end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/dashboards/stat_dashboard.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "administrate/custom_dashboard"

class StatDashboard < Administrate::CustomDashboard
resource "Stats"
resource "Stat"
end
2 changes: 2 additions & 0 deletions spec/example_app/app/policies/stat_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class StatPolicy < ApplicationPolicy
end
2 changes: 0 additions & 2 deletions spec/example_app/app/policies/stats_policy.rb

This file was deleted.

5 changes: 2 additions & 3 deletions spec/example_app/app/views/admin/stats/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<div style="padding: 20px">
<h1>Stats</h1>
<br>
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></h1>
<br>
<p><b>Total Orders:</b> <%= @stats[:order_count] %></h1>
<p><b>Total Customers:</b> <%= @stats[:customer_count] %></p>
<p><b>Total Orders:</b> <%= @stats[:order_count] %></p>
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed markup.

</div>