From fcd3fc7da4f13b5c3d9b08bb4add1ed2510958fe Mon Sep 17 00:00:00 2001 From: Grayson Wright Date: Sat, 7 Nov 2015 13:33:59 -0800 Subject: [PATCH] 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 --- CHANGELOG.md | 11 +++++ app/views/fields/belongs_to/_form.html.erb | 9 ++-- app/views/fields/belongs_to/_index.html.erb | 5 ++- app/views/fields/belongs_to/_show.html.erb | 5 ++- app/views/fields/has_many/_form.html.erb | 12 ++---- app/views/fields/has_one/_form.html.erb | 2 +- app/views/fields/has_one/_index.html.erb | 5 ++- app/views/fields/has_one/_show.html.erb | 5 ++- app/views/fields/polymorphic/_index.html.erb | 2 +- app/views/fields/polymorphic/_show.html.erb | 4 +- lib/administrate/base_dashboard.rb | 4 ++ lib/administrate/fields/associative.rb | 25 +++++++++++ lib/administrate/fields/base.rb | 1 + lib/administrate/fields/belongs_to.rb | 18 +++++--- lib/administrate/fields/date_time.rb | 2 + lib/administrate/fields/deferred.rb | 2 + lib/administrate/fields/has_many.rb | 18 ++++---- lib/administrate/fields/has_one.rb | 5 +-- lib/administrate/fields/polymorphic.rb | 4 +- lib/administrate/page/form.rb | 2 +- lib/administrate/page/show.rb | 2 +- .../dashboard/templates/dashboard.rb.erb | 7 ++++ .../views/fields/has_one/_form_spec.rb | 37 +++++++++++++++++ .../views/fields/has_one/_index_spec.rb | 36 ++++++++++++++++ .../views/fields/has_one/_show_spec.rb | 41 +++++++++++++++++++ .../views/fields/polymorphic/_index_spec.rb | 9 +++- .../views/fields/polymorphic/_show_spec.rb | 16 ++++++-- spec/dashboards/customer_dashboard_spec.rb | 11 +++++ .../app/dashboards/customer_dashboard.rb | 4 ++ .../app/dashboards/line_item_dashboard.rb | 4 ++ .../app/dashboards/product_dashboard.rb | 4 ++ spec/example_app/app/models/concerns/.keep | 0 spec/example_app/app/models/customer.rb | 4 -- spec/example_app/app/models/line_item.rb | 4 -- spec/example_app/app/models/order.rb | 4 -- spec/example_app/app/models/product.rb | 4 -- spec/features/index_page_spec.rb | 2 +- spec/features/line_items_spec.rb | 10 ++--- spec/features/orders_form_spec.rb | 2 +- spec/features/orders_index_spec.rb | 4 +- spec/features/orders_show_spec.rb | 2 +- spec/features/show_page_spec.rb | 4 +- spec/lib/fields/belongs_to_spec.rb | 3 +- spec/lib/fields/deferred_spec.rb | 1 - spec/lib/fields/email_spec.rb | 1 - spec/lib/fields/has_many_spec.rb | 14 ++++--- spec/lib/fields/has_one_spec.rb | 20 --------- spec/lib/fields/image_spec.rb | 1 - spec/lib/fields/number_spec.rb | 1 - spec/lib/fields/polymorphic_spec.rb | 1 - spec/lib/fields/string_spec.rb | 1 - spec/lib/pages/form_spec.rb | 2 +- spec/lib/pages/show_spec.rb | 2 +- spec/support/dashboard_helpers.rb | 12 ++++++ 54 files changed, 299 insertions(+), 112 deletions(-) create mode 100644 lib/administrate/fields/associative.rb create mode 100644 spec/administrate/views/fields/has_one/_form_spec.rb create mode 100644 spec/administrate/views/fields/has_one/_index_spec.rb create mode 100644 spec/administrate/views/fields/has_one/_show_spec.rb delete mode 100644 spec/example_app/app/models/concerns/.keep create mode 100644 spec/support/dashboard_helpers.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index b428614f6e..3493c0a912 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,17 @@ ### Upcoming Release +* [#191] [CHANGE] Improve API for specifying how resources are displayed + across the dashboard. + * Models are now displayed with a sensible default - (e.g. "User #2") + * Users can define `ModelDashboard#display_resource(resource)` for custom + display behavior + * Users who have generated views for the following field types + may need to update them to take advantage of the new API: + * HasOne + * HasMany + * PolyMorphic + * BelongsTo * [#223] [FEATURE] Translation: Vietnamese * [#161] [FEATURE] Translation: Mandarin Chinese * [#196] [FEATURE] Translation: Taiwanese Mandarin diff --git a/app/views/fields/belongs_to/_form.html.erb b/app/views/fields/belongs_to/_form.html.erb index 71c6a16504..82c7b54f0a 100644 --- a/app/views/fields/belongs_to/_form.html.erb +++ b/app/views/fields/belongs_to/_form.html.erb @@ -17,9 +17,6 @@ that displays all possible records to associate with. %> <%= f.label field.permitted_attribute %> -<%= f.collection_select( - field.permitted_attribute, - field.candidate_records, - :id, - :to_s, -) %> +<%= f.select(field.permitted_attribute) do %> + <%= options_for_select(field.associated_resource_options) %> +<% end %> diff --git a/app/views/fields/belongs_to/_index.html.erb b/app/views/fields/belongs_to/_index.html.erb index 4b2a0cf527..5df755f723 100644 --- a/app/views/fields/belongs_to/_index.html.erb +++ b/app/views/fields/belongs_to/_index.html.erb @@ -16,5 +16,8 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to field.data, [Administrate::NAMESPACE, field.data] %> + <%= link_to( + field.display_associated_resource, + [Administrate::NAMESPACE, field.data], + ) %> <% end %> diff --git a/app/views/fields/belongs_to/_show.html.erb b/app/views/fields/belongs_to/_show.html.erb index 1b3efc8f96..df80d57ba3 100644 --- a/app/views/fields/belongs_to/_show.html.erb +++ b/app/views/fields/belongs_to/_show.html.erb @@ -16,5 +16,8 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to field.data, [Administrate::NAMESPACE, field.data] %> + <%= link_to( + field.display_associated_resource, + [Administrate::NAMESPACE, field.data], + ) %> <% end %> diff --git a/app/views/fields/has_many/_form.html.erb b/app/views/fields/has_many/_form.html.erb index 747dba1979..b07e05787e 100644 --- a/app/views/fields/has_many/_form.html.erb +++ b/app/views/fields/has_many/_form.html.erb @@ -21,12 +21,6 @@ and is augmented with [Selectize]. <%= f.label field.attribute_key %> -<%= f.collection_select( - field.attribute_key, - field.candidate_records, - :id, - :to_s, - {}, - { multiple: true }, -) -%> +<%= f.select(field.attribute_key, nil, {}, multiple: true) do %> + <%= options_for_select(field.associated_resource_options) %> +<% end %> diff --git a/app/views/fields/has_one/_form.html.erb b/app/views/fields/has_one/_form.html.erb index 9f7656ab34..eb11a9d2ba 100644 --- a/app/views/fields/has_one/_form.html.erb +++ b/app/views/fields/has_one/_form.html.erb @@ -17,6 +17,6 @@ so this partial renders a message to that effect. [1]: http://www.rubydoc.info/gems/administrate/Administrate/Field/HasOne %> -<%= f.label field.permitted_attribute %> +<%= f.label field.attribute %> <%= t("administrate.fields.has_one.not_supported") %> diff --git a/app/views/fields/has_one/_index.html.erb b/app/views/fields/has_one/_index.html.erb index 841ccc24dd..ccc86d99a4 100644 --- a/app/views/fields/has_one/_index.html.erb +++ b/app/views/fields/has_one/_index.html.erb @@ -16,5 +16,8 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to field.data, [Administrate::NAMESPACE, field.data] %> + <%= link_to( + field.display_associated_resource, + [Administrate::NAMESPACE, field.data], + ) %> <% end %> diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index 70f14d9b21..21017c9ac1 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -16,5 +16,8 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to field.data, [Administrate::NAMESPACE, field.data] %> + <%= link_to( + field.display_associated_resource, + [Administrate::NAMESPACE, field.data], + ) %> <% end %> diff --git a/app/views/fields/polymorphic/_index.html.erb b/app/views/fields/polymorphic/_index.html.erb index 3dcefad7a0..b26b370cfd 100644 --- a/app/views/fields/polymorphic/_index.html.erb +++ b/app/views/fields/polymorphic/_index.html.erb @@ -18,7 +18,7 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( - field.data.to_s, + field.display_associated_resource, polymorphic_path([:admin, field.data]) ) %> <% end %> diff --git a/app/views/fields/polymorphic/_show.html.erb b/app/views/fields/polymorphic/_show.html.erb index 1e94277ae9..4e362896c1 100644 --- a/app/views/fields/polymorphic/_show.html.erb +++ b/app/views/fields/polymorphic/_show.html.erb @@ -18,7 +18,7 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( - field.data.to_s, - polymorphic_path([:admin, field.data]) + field.display_associated_resource, + [:admin, field.data], ) %> <% end %> diff --git a/lib/administrate/base_dashboard.rb b/lib/administrate/base_dashboard.rb index 82befc4880..f883a75dd1 100644 --- a/lib/administrate/base_dashboard.rb +++ b/lib/administrate/base_dashboard.rb @@ -35,5 +35,9 @@ def show_page_attributes def collection_attributes self.class::COLLECTION_ATTRIBUTES end + + def display_resource(resource) + "#{resource.class} ##{resource.id}" + end end end diff --git a/lib/administrate/fields/associative.rb b/lib/administrate/fields/associative.rb new file mode 100644 index 0000000000..2df93ed23c --- /dev/null +++ b/lib/administrate/fields/associative.rb @@ -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 diff --git a/lib/administrate/fields/base.rb b/lib/administrate/fields/base.rb index 53456f850b..de2cbdfcfa 100644 --- a/lib/administrate/fields/base.rb +++ b/lib/administrate/fields/base.rb @@ -1,4 +1,5 @@ require_relative "deferred" +require "active_support/core_ext/string/inflections" module Administrate module Field diff --git a/lib/administrate/fields/belongs_to.rb b/lib/administrate/fields/belongs_to.rb index 3025b10c4d..a998f8f496 100644 --- a/lib/administrate/fields/belongs_to.rb +++ b/lib/administrate/fields/belongs_to.rb @@ -1,8 +1,8 @@ -require_relative "base" +require_relative "associative" module Administrate module Field - class BelongsTo < Field::Base + class BelongsTo < Associative def self.permitted_attribute(attr) :"#{attr}_id" end @@ -11,14 +11,20 @@ def permitted_attribute self.class.permitted_attribute(attribute) end - def candidate_records - Object.const_get(associated_class_name).all + def associated_resource_options + candidate_resources.map do |resource| + [display_candidate_resource(resource), resource.id] + end end private - def associated_class_name - options.fetch(:class_name, attribute.to_s.camelcase) + def candidate_resources + associated_class.all + end + + def display_candidate_resource(resource) + associated_dashboard.display_resource(resource) end end end diff --git a/lib/administrate/fields/date_time.rb b/lib/administrate/fields/date_time.rb index eabc48483d..6291e8c3af 100644 --- a/lib/administrate/fields/date_time.rb +++ b/lib/administrate/fields/date_time.rb @@ -1,3 +1,5 @@ +require_relative "base" + module Administrate module Field class DateTime < Base diff --git a/lib/administrate/fields/deferred.rb b/lib/administrate/fields/deferred.rb index eb1785488f..b5a4ed5f9a 100644 --- a/lib/administrate/fields/deferred.rb +++ b/lib/administrate/fields/deferred.rb @@ -1,3 +1,5 @@ +require "active_support/core_ext/module/delegation" + module Administrate module Field class Deferred diff --git a/lib/administrate/fields/has_many.rb b/lib/administrate/fields/has_many.rb index 8e0a58ab2f..28bd028349 100644 --- a/lib/administrate/fields/has_many.rb +++ b/lib/administrate/fields/has_many.rb @@ -1,9 +1,9 @@ -require_relative "base" +require_relative "associative" require "administrate/page/collection" module Administrate module Field - class HasMany < Field::Base + class HasMany < Associative DEFAULT_LIMIT = 5 def self.permitted_attribute(attribute) @@ -18,8 +18,10 @@ def attribute_key permitted_attribute.keys.first end - def candidate_records - Object.const_get(associated_class_name).all + def associated_resource_options + candidate_resources.map do |resource| + [display_candidate_resource(resource), resource.id] + end end def limit @@ -40,12 +42,12 @@ def more_than_limit? private - def associated_dashboard - Object.const_get("#{associated_class_name}Dashboard").new + def candidate_resources + associated_class.all end - def associated_class_name - options.fetch(:class_name, attribute.to_s.singularize.camelcase) + def display_candidate_resource(resource) + associated_dashboard.display_resource(resource) end end end diff --git a/lib/administrate/fields/has_one.rb b/lib/administrate/fields/has_one.rb index 495b29f5e0..19bed2cbf5 100644 --- a/lib/administrate/fields/has_one.rb +++ b/lib/administrate/fields/has_one.rb @@ -1,9 +1,8 @@ -require_relative "base" -require_relative "belongs_to" +require_relative "associative" module Administrate module Field - class HasOne < BelongsTo + class HasOne < Associative def self.permitted_attribute(attr) attr end diff --git a/lib/administrate/fields/polymorphic.rb b/lib/administrate/fields/polymorphic.rb index c32d4c5f70..8f11de6970 100644 --- a/lib/administrate/fields/polymorphic.rb +++ b/lib/administrate/fields/polymorphic.rb @@ -1,8 +1,8 @@ -require_relative "base" +require_relative "associative" module Administrate module Field - class Polymorphic < Base + class Polymorphic < Associative end end end diff --git a/lib/administrate/page/form.rb b/lib/administrate/page/form.rb index 1b5bfdaee0..6a0fa96346 100644 --- a/lib/administrate/page/form.rb +++ b/lib/administrate/page/form.rb @@ -17,7 +17,7 @@ def attributes end def page_title - resource.to_s + dashboard.display_resource(resource) end protected diff --git a/lib/administrate/page/show.rb b/lib/administrate/page/show.rb index d2ef938c59..ab3da87d8a 100644 --- a/lib/administrate/page/show.rb +++ b/lib/administrate/page/show.rb @@ -11,7 +11,7 @@ def initialize(dashboard, resource) attr_reader :resource def page_title - resource.to_s + dashboard.display_resource(resource) end def attributes diff --git a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb index bdb31b1e61..d45443cd72 100644 --- a/lib/generators/administrate/dashboard/templates/dashboard.rb.erb +++ b/lib/generators/administrate/dashboard/templates/dashboard.rb.erb @@ -40,4 +40,11 @@ class <%= class_name %>Dashboard < Administrate::BaseDashboard end.join("\n") %> ] + + # Overwrite this method to customize how <%= file_name.pluralize.humanize.downcase %> are displayed + # across all pages of the admin dashboard. + # + # def display_resource(<%= file_name %>) + # "<%= class_name %> ##{<%= file_name %>.id}" + # end end diff --git a/spec/administrate/views/fields/has_one/_form_spec.rb b/spec/administrate/views/fields/has_one/_form_spec.rb new file mode 100644 index 0000000000..cbae5cf276 --- /dev/null +++ b/spec/administrate/views/fields/has_one/_form_spec.rb @@ -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 diff --git a/spec/administrate/views/fields/has_one/_index_spec.rb b/spec/administrate/views/fields/has_one/_index_spec.rb new file mode 100644 index 0000000000..acc8c0f61c --- /dev/null +++ b/spec/administrate/views/fields/has_one/_index_spec.rb @@ -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 = "#{product.name}" + expect(rendered.strip).to eq(expected) + end + end +end diff --git a/spec/administrate/views/fields/has_one/_show_spec.rb b/spec/administrate/views/fields/has_one/_show_spec.rb new file mode 100644 index 0000000000..cc9575c10a --- /dev/null +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -0,0 +1,41 @@ +require "rails_helper" +require "administrate/fields/has_one" + +describe "fields/has_one/_show", type: :view do + context "without an associated records" do + it "displays nothing" do + has_one = instance_double( + "Administrate::Field::HasOne", + display_associated_resource: "", + data: nil, + ) + + render( + partial: "fields/has_one/show.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", + display_associated_resource: product.name, + data: product, + ) + + render( + partial: "fields/has_one/show.html.erb", + locals: { field: has_one }, + ) + + expected = "#{product.name}" + expect(rendered.strip).to eq(expected) + end + end +end diff --git a/spec/administrate/views/fields/polymorphic/_index_spec.rb b/spec/administrate/views/fields/polymorphic/_index_spec.rb index 2706ab7cb8..3265d4855a 100644 --- a/spec/administrate/views/fields/polymorphic/_index_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_index_spec.rb @@ -17,15 +17,20 @@ context "with an associated record" do it "renders a link to the record" do product = create(:product) - polymorphic = double(data: product) product_path = polymorphic_path([:admin, product]) + polymorphic = instance_double( + "Administrate::Field::Polymorphic", + data: product, + display_associated_resource: product.name, + ) render( partial: "fields/polymorphic/index.html.erb", locals: { field: polymorphic }, ) - expect(rendered.strip).to eq("#{product}") + expected = "#{product.name}" + expect(rendered.strip).to eq(expected) end end end diff --git a/spec/administrate/views/fields/polymorphic/_show_spec.rb b/spec/administrate/views/fields/polymorphic/_show_spec.rb index 9fbebb7b8f..6d872372d0 100644 --- a/spec/administrate/views/fields/polymorphic/_show_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_show_spec.rb @@ -1,9 +1,14 @@ require "rails_helper" +require "administrate/fields/polymorphic" describe "fields/polymorphic/_show", type: :view do context "without an associated records" do it "displays nothing" do - polymorphic = double(data: nil) + polymorphic = instance_double( + "Administrate::Field::Polymorphic", + display_associated_resource: "", + data: nil, + ) render( partial: "fields/polymorphic/show.html.erb", @@ -17,15 +22,20 @@ context "with an associated record" do it "renders a link to the record" do product = create(:product) - polymorphic = double(data: product) product_path = polymorphic_path([:admin, product]) + polymorphic = instance_double( + "Administrate::Field::Polymorphic", + display_associated_resource: product.name, + data: product, + ) render( partial: "fields/polymorphic/show.html.erb", locals: { field: polymorphic }, ) - expect(rendered.strip).to eq("#{product}") + expected = "#{product.name}" + expect(rendered.strip).to eq(expected) end end end diff --git a/spec/dashboards/customer_dashboard_spec.rb b/spec/dashboards/customer_dashboard_spec.rb index b496a10b19..681a320bf5 100644 --- a/spec/dashboards/customer_dashboard_spec.rb +++ b/spec/dashboards/customer_dashboard_spec.rb @@ -23,4 +23,15 @@ to eq(Field::Number.with_options(prefix: "$", decimals: 2)) end end + + describe "#display_resource" do + it "returns the customer's name" do + customer = double(name: "Example Customer") + dashboard = CustomerDashboard.new + + rendered_resource = dashboard.display_resource(customer) + + expect(rendered_resource).to eq(customer.name) + end + end end diff --git a/spec/example_app/app/dashboards/customer_dashboard.rb b/spec/example_app/app/dashboards/customer_dashboard.rb index cff5aec14b..f62d2c01c9 100644 --- a/spec/example_app/app/dashboards/customer_dashboard.rb +++ b/spec/example_app/app/dashboards/customer_dashboard.rb @@ -14,4 +14,8 @@ class CustomerDashboard < Administrate::BaseDashboard COLLECTION_ATTRIBUTES = ATTRIBUTE_TYPES.keys SHOW_PAGE_ATTRIBUTES = ATTRIBUTE_TYPES.keys - [:name] FORM_ATTRIBUTES = [:name, :email, :email_subscriber] + + def display_resource(customer) + customer.name + end end diff --git a/spec/example_app/app/dashboards/line_item_dashboard.rb b/spec/example_app/app/dashboards/line_item_dashboard.rb index bef8e20f82..8676495e79 100644 --- a/spec/example_app/app/dashboards/line_item_dashboard.rb +++ b/spec/example_app/app/dashboards/line_item_dashboard.rb @@ -21,4 +21,8 @@ class LineItemDashboard < Administrate::BaseDashboard COLLECTION_ATTRIBUTES = ATTRIBUTES + [:total_price] SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + [:total_price] FORM_ATTRIBUTES = ATTRIBUTES + + def display_resource(line_item) + "Line Item #%04d" % line_item.id + end end diff --git a/spec/example_app/app/dashboards/product_dashboard.rb b/spec/example_app/app/dashboards/product_dashboard.rb index 927825f823..8d13d7ebd4 100644 --- a/spec/example_app/app/dashboards/product_dashboard.rb +++ b/spec/example_app/app/dashboards/product_dashboard.rb @@ -20,4 +20,8 @@ class ProductDashboard < Administrate::BaseDashboard COLLECTION_ATTRIBUTES = ATTRIBUTES FORM_ATTRIBUTES = ATTRIBUTES SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + + def display_resource(resource) + resource.name + end end diff --git a/spec/example_app/app/models/concerns/.keep b/spec/example_app/app/models/concerns/.keep deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spec/example_app/app/models/customer.rb b/spec/example_app/app/models/customer.rb index 054c77ea40..bfc796ad14 100644 --- a/spec/example_app/app/models/customer.rb +++ b/spec/example_app/app/models/customer.rb @@ -7,8 +7,4 @@ class Customer < ActiveRecord::Base def lifetime_value orders.map(&:total_price).reduce(0, :+) end - - def to_s - name - end end diff --git a/spec/example_app/app/models/line_item.rb b/spec/example_app/app/models/line_item.rb index 923759ca23..8828a1e697 100644 --- a/spec/example_app/app/models/line_item.rb +++ b/spec/example_app/app/models/line_item.rb @@ -7,10 +7,6 @@ class LineItem < ActiveRecord::Base validates :unit_price, presence: true validates :quantity, presence: true - def to_s - "Line Item #%04d" % id - end - def total_price unit_price * quantity end diff --git a/spec/example_app/app/models/order.rb b/spec/example_app/app/models/order.rb index 999eb1fc61..327db20923 100644 --- a/spec/example_app/app/models/order.rb +++ b/spec/example_app/app/models/order.rb @@ -10,10 +10,6 @@ class Order < ActiveRecord::Base validates :address_state, presence: true validates :address_zip, presence: true - def to_s - "Order ##{id}" - end - def total_price line_items.map(&:total_price).reduce(0, :+) end diff --git a/spec/example_app/app/models/product.rb b/spec/example_app/app/models/product.rb index 97f5b55e40..a65382cbe9 100644 --- a/spec/example_app/app/models/product.rb +++ b/spec/example_app/app/models/product.rb @@ -6,10 +6,6 @@ class Product < ActiveRecord::Base validates :slug, uniqueness: true validate :valid_slug - def to_s - name - end - def name=(value) self.slug = value.to_s.parameterize super(value) diff --git a/spec/features/index_page_spec.rb b/spec/features/index_page_spec.rb index 84913d2d70..aa82d1d40f 100644 --- a/spec/features/index_page_spec.rb +++ b/spec/features/index_page_spec.rb @@ -17,7 +17,7 @@ visit admin_customers_path click_row_for(customer) - expect(page).to have_header(customer.to_s) + expect(page).to have_header(displayed(customer)) expect(page).to have_content(customer.name) expect(page).to have_content(customer.email) end diff --git a/spec/features/line_items_spec.rb b/spec/features/line_items_spec.rb index 6d0ebdd7a4..b2c5b1e3ae 100644 --- a/spec/features/line_items_spec.rb +++ b/spec/features/line_items_spec.rb @@ -8,7 +8,7 @@ expect(page).to have_header("Line Items") expect(page).to have_content(line_item.unit_price) - expect(page).to have_content(line_item.product.to_s) + expect(page).to have_content(displayed(line_item.product)) end it "links to the line item show page", :js do @@ -17,9 +17,9 @@ visit admin_line_items_path click_row_for(line_item) - expect(page).to have_header(line_item.to_s) - expect(page).to have_content(line_item.to_s) - expect(page).to have_content(line_item.product.to_s) + expect(page).to have_header(displayed(line_item)) + expect(page).to have_content(displayed(line_item)) + expect(page).to have_content(displayed(line_item.product)) end it "links to the edit page" do @@ -28,7 +28,7 @@ visit admin_line_items_path click_on "Edit" - expect(page).to have_header("Edit #{line_item}") + expect(page).to have_header("Edit #{displayed(line_item)}") end it "links to the new page" do diff --git a/spec/features/orders_form_spec.rb b/spec/features/orders_form_spec.rb index ffbeb10c4b..8396f4393a 100644 --- a/spec/features/orders_form_spec.rb +++ b/spec/features/orders_form_spec.rb @@ -52,7 +52,7 @@ def find_option(associated_model, field_id) field = find("#order_" + field_id) - field.find("option", text: associated_model.to_s) + field.find("option", text: displayed(associated_model)) end end diff --git a/spec/features/orders_index_spec.rb b/spec/features/orders_index_spec.rb index ea5b5ac283..6544fde9b0 100644 --- a/spec/features/orders_index_spec.rb +++ b/spec/features/orders_index_spec.rb @@ -14,7 +14,7 @@ order = create(:order) visit admin_orders_path - click_on(order.customer.to_s) + click_on(displayed(order.customer)) expect(page).to have_header(order.customer.name) end @@ -25,7 +25,7 @@ visit admin_orders_path click_row_for(order) - expect(page).to have_header(order.to_s) + expect(page).to have_header(displayed(order)) expect(page).to have_link(order.customer.name) end diff --git a/spec/features/orders_show_spec.rb b/spec/features/orders_show_spec.rb index 10ce8444cf..aa1cb2ba04 100644 --- a/spec/features/orders_show_spec.rb +++ b/spec/features/orders_show_spec.rb @@ -17,6 +17,6 @@ visit admin_order_path(line_item.order) click_row_for(line_item) - expect(page).to have_header(line_item.to_s) + expect(page).to have_header(displayed(line_item)) end end diff --git a/spec/features/show_page_spec.rb b/spec/features/show_page_spec.rb index 13f734ef63..8700eb0882 100644 --- a/spec/features/show_page_spec.rb +++ b/spec/features/show_page_spec.rb @@ -39,7 +39,7 @@ click_row_for(order) - expect(page).to have_header(order.to_s) + expect(page).to have_header(displayed(order)) end it "link-ifies the email" do @@ -56,6 +56,6 @@ visit admin_customer_path(customer) click_on "Edit" - expect(page).to have_header("Edit #{customer}") + expect(page).to have_header("Edit #{displayed(customer)}") end end diff --git a/spec/lib/fields/belongs_to_spec.rb b/spec/lib/fields/belongs_to_spec.rb index d516e16b43..8f2993f36f 100644 --- a/spec/lib/fields/belongs_to_spec.rb +++ b/spec/lib/fields/belongs_to_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/belongs_to" require "support/constant_helpers" require "support/field_matchers" @@ -29,7 +28,7 @@ association = Administrate::Field::BelongsTo. with_options(class_name: "Foo") field = association.new(:customers, [], :show) - candidates = field.candidate_records + candidates = field.associated_resource_options expect(Foo).to have_received(:all) expect(candidates).to eq([]) diff --git a/spec/lib/fields/deferred_spec.rb b/spec/lib/fields/deferred_spec.rb index e627334d0e..41d8d9ae6c 100644 --- a/spec/lib/fields/deferred_spec.rb +++ b/spec/lib/fields/deferred_spec.rb @@ -1,4 +1,3 @@ -require "rails_helper" require "administrate/fields/deferred" require "administrate/fields/string" diff --git a/spec/lib/fields/email_spec.rb b/spec/lib/fields/email_spec.rb index a91d5286d4..3936f1e68a 100644 --- a/spec/lib/fields/email_spec.rb +++ b/spec/lib/fields/email_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/email" describe Administrate::Field::Email do diff --git a/spec/lib/fields/has_many_spec.rb b/spec/lib/fields/has_many_spec.rb index 9ee2639e85..c06d591133 100644 --- a/spec/lib/fields/has_many_spec.rb +++ b/spec/lib/fields/has_many_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/has_many" require "support/constant_helpers" require "support/mock_relation" @@ -18,12 +17,17 @@ describe "#associated_collection" do it "returns an index page for the dashboard of the associated attribute" do - orders = [] - field = Administrate::Field::HasMany.new(:orders, orders, :show) + begin + WidgetDashboard = Class.new + widgets = [] + field = Administrate::Field::HasMany.new(:widgets, widgets, :show) - page = field.associated_collection + page = field.associated_collection - expect(page).to be_instance_of(Administrate::Page::Collection) + expect(page).to be_instance_of(Administrate::Page::Collection) + ensure + remove_constants :WidgetDashboard + end end end diff --git a/spec/lib/fields/has_one_spec.rb b/spec/lib/fields/has_one_spec.rb index 11f1ea7f5f..c79ac7541a 100644 --- a/spec/lib/fields/has_one_spec.rb +++ b/spec/lib/fields/has_one_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/has_one" require "support/constant_helpers" @@ -14,23 +13,4 @@ expect(path).to eq("/fields/has_one/#{page}") end end - - describe "class_name option" do - it "determines what dashboard is used to present the association" do - begin - Foo = Class.new - allow(Foo).to receive(:all).and_return([]) - - association = Administrate::Field::HasOne. - with_options(class_name: "Foo") - field = association.new(:customers, [], :show) - candidates = field.candidate_records - - expect(Foo).to have_received(:all) - expect(candidates).to eq([]) - ensure - remove_constants :Foo - end - end - end end diff --git a/spec/lib/fields/image_spec.rb b/spec/lib/fields/image_spec.rb index 1e8b2b6d88..bb33f9d8f1 100644 --- a/spec/lib/fields/image_spec.rb +++ b/spec/lib/fields/image_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/image" describe Administrate::Field::Image do diff --git a/spec/lib/fields/number_spec.rb b/spec/lib/fields/number_spec.rb index cc310026c9..937a1c135e 100644 --- a/spec/lib/fields/number_spec.rb +++ b/spec/lib/fields/number_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/number" require "support/field_matchers" diff --git a/spec/lib/fields/polymorphic_spec.rb b/spec/lib/fields/polymorphic_spec.rb index 44560238e8..455d5afe96 100644 --- a/spec/lib/fields/polymorphic_spec.rb +++ b/spec/lib/fields/polymorphic_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/polymorphic" require "support/field_matchers" diff --git a/spec/lib/fields/string_spec.rb b/spec/lib/fields/string_spec.rb index 928ea0c84b..282a66c025 100644 --- a/spec/lib/fields/string_spec.rb +++ b/spec/lib/fields/string_spec.rb @@ -1,4 +1,3 @@ -require "spec_helper" require "administrate/fields/string" require "support/field_matchers" diff --git a/spec/lib/pages/form_spec.rb b/spec/lib/pages/form_spec.rb index 4fd1f4a20b..8860b23661 100644 --- a/spec/lib/pages/form_spec.rb +++ b/spec/lib/pages/form_spec.rb @@ -3,7 +3,7 @@ describe Administrate::Page::Form do describe "#page_title" do it "is the value of the resource's key attribute" do - customer = double(to_s: "Worf") + customer = double(name: "Worf") page = Administrate::Page::Form.new(CustomerDashboard.new, customer) expect(page.page_title).to eq("Worf") diff --git a/spec/lib/pages/show_spec.rb b/spec/lib/pages/show_spec.rb index e1f264fb6d..c7a0380251 100644 --- a/spec/lib/pages/show_spec.rb +++ b/spec/lib/pages/show_spec.rb @@ -3,7 +3,7 @@ describe Administrate::Page::Show do describe "#page_title" do it "is the stringified resource" do - customer = double(to_s: "Worf") + customer = double(name: "Worf") page = Administrate::Page::Show.new(CustomerDashboard.new, customer) expect(page.page_title).to eq("Worf") diff --git a/spec/support/dashboard_helpers.rb b/spec/support/dashboard_helpers.rb new file mode 100644 index 0000000000..0ca913b14d --- /dev/null +++ b/spec/support/dashboard_helpers.rb @@ -0,0 +1,12 @@ +module DashboardHelpers + def displayed(resource) + (resource.class.to_s + "Dashboard"). + constantize. + new. + display_resource(resource) + end +end + +RSpec.configure do |config| + config.include DashboardHelpers +end