-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve resource display API & default behavior
Problem: Administrate relies on `#to_s` to display resources throughout the system. In order to get Administrate working correctly, developers must define `#to_s` on all models that will be displayed in the admin dashboard. This process is not documented, can be confusing, and gets in the way of a zero-configuration dashboard. Solution: Add `Administrate::BaseDashboard#display_resource`, which takes a resource and returns a sensible string representation. Devs can overwrite this method on a per-dashboard basis to customize how their resources are displayed. Add generated comment for `display_resource`, that looks like: ``` # Overwrite this method to customize how line items are displayed # across all pages of the admin dashboard. # # def display_resource(line_item) # "LineItem ##{line_item.id}" # end ``` Minor changes: Extract `Administrate::Field::Associative` as a superclass, which contains logic for looking up associated dashboards and associated models' classes. Fix PR suggestions
- Loading branch information
Showing
54 changed files
with
299 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require_relative "base" | ||
|
||
module Administrate | ||
module Field | ||
class Associative < Base | ||
def display_associated_resource | ||
associated_dashboard.display_resource(data) | ||
end | ||
|
||
protected | ||
|
||
def associated_dashboard | ||
"#{associated_class_name}Dashboard".constantize.new | ||
end | ||
|
||
def associated_class | ||
associated_class_name.constantize | ||
end | ||
|
||
def associated_class_name | ||
options.fetch(:class_name, attribute.to_s.singularize.camelcase) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require_relative "base" | ||
|
||
module Administrate | ||
module Field | ||
class DateTime < Base | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
require "active_support/core_ext/module/delegation" | ||
|
||
module Administrate | ||
module Field | ||
class Deferred | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
require_relative "base" | ||
require_relative "associative" | ||
|
||
module Administrate | ||
module Field | ||
class Polymorphic < Base | ||
class Polymorphic < Associative | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ def attributes | |
end | ||
|
||
def page_title | ||
resource.to_s | ||
dashboard.display_resource(resource) | ||
end | ||
|
||
protected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require "rails_helper" | ||
require "administrate/fields/has_one" | ||
|
||
describe "fields/has_one/_form", type: :view do | ||
it "displays the field name" do | ||
has_one = instance_double( | ||
"Administrate::Field::HasOne", | ||
attribute: "Commentable", | ||
) | ||
|
||
render( | ||
partial: "fields/has_one/form.html.erb", | ||
locals: { field: has_one, f: form_builder }, | ||
) | ||
|
||
expect(rendered.strip).to include("Commentable") | ||
end | ||
|
||
it "does not display a form" do | ||
has_one = instance_double( | ||
"Administrate::Field::HasOne", | ||
attribute: "Commentable", | ||
) | ||
|
||
render( | ||
partial: "fields/has_one/form.html.erb", | ||
locals: { field: has_one, f: form_builder }, | ||
) | ||
|
||
expect(rendered). | ||
to include(t("administrate.fields.has_one.not_supported")) | ||
end | ||
|
||
def form_builder | ||
double("Form Builder", label: "Commentable") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
require "rails_helper" | ||
|
||
describe "fields/has_one/_index", type: :view do | ||
context "without an associated records" do | ||
it "displays nothing" do | ||
has_one = double(data: nil) | ||
|
||
render( | ||
partial: "fields/has_one/index.html.erb", | ||
locals: { field: has_one }, | ||
) | ||
|
||
expect(rendered.strip).to eq("") | ||
end | ||
end | ||
|
||
context "with an associated record" do | ||
it "renders a link to the record" do | ||
product = create(:product) | ||
product_path = polymorphic_path([:admin, product]) | ||
has_one = instance_double( | ||
"Administrate::Field::HasOne", | ||
data: product, | ||
display_associated_resource: product.name, | ||
) | ||
|
||
render( | ||
partial: "fields/has_one/index.html.erb", | ||
locals: { field: has_one }, | ||
) | ||
|
||
expected = "<a href=\"#{product_path}\">#{product.name}</a>" | ||
expect(rendered.strip).to eq(expected) | ||
end | ||
end | ||
end |
Oops, something went wrong.