diff --git a/docs/customizing_dashboards.md b/docs/customizing_dashboards.md index 0f0516b9b8..e95b70665a 100644 --- a/docs/customizing_dashboards.md +++ b/docs/customizing_dashboards.md @@ -664,3 +664,11 @@ fields/base/looks/customer_card/ fields/base/looks/default/ fields/base/ ``` + +You can also select a different look for each page by passing a Hash to `look` option. + +```ruby +ATTRIBUTE_TYPES = { + created_at: Field::DateTime.with_options(look: {index: :relative}) +} +``` diff --git a/lib/administrate/field/base.rb b/lib/administrate/field/base.rb index 7b7f0375d6..3a52325a8b 100644 --- a/lib/administrate/field/base.rb +++ b/lib/administrate/field/base.rb @@ -104,7 +104,17 @@ def partial_prefixes end def look - (options.fetch(:look, :default).presence || :default).to_sym + val = options.fetch(:look, :default).presence || :default + case val + when Symbol + val + when ::String + val.to_sym + when Hash + val.fetch(page, :default).to_sym + else + :default + end end def required? diff --git a/spec/example_app/app/dashboards/log_entry_dashboard.rb b/spec/example_app/app/dashboards/log_entry_dashboard.rb index cf69369e68..0c750f4056 100644 --- a/spec/example_app/app/dashboards/log_entry_dashboard.rb +++ b/spec/example_app/app/dashboards/log_entry_dashboard.rb @@ -6,12 +6,13 @@ class LogEntryDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number, action: Field::String, - logeable: Field::Polymorphic.with_options(classes: [Customer, ::Order]) + logeable: Field::Polymorphic.with_options(classes: [Customer, ::Order]), + created_at: Field::DateTime.with_options(look: {index: :relative}) }.freeze - COLLECTION_ATTRIBUTES = [:id] + ATTRIBUTES + COLLECTION_ATTRIBUTES = [:id] + ATTRIBUTES + [:created_at] FORM_ATTRIBUTES = ATTRIBUTES - SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + [:created_at] def display_resource(resource) "#{resource.action}[#{safe_display_logeable(resource.logeable)}]" diff --git a/spec/example_app/app/views/fields/date_time/looks/relative/_index.html.erb b/spec/example_app/app/views/fields/date_time/looks/relative/_index.html.erb new file mode 100644 index 0000000000..74fa858e7f --- /dev/null +++ b/spec/example_app/app/views/fields/date_time/looks/relative/_index.html.erb @@ -0,0 +1,5 @@ +<% if field.data %> + + <%= time_ago_in_words(field.data) %> + +<% end %> diff --git a/spec/example_app/spec/features/date_time_relative_look_spec.rb b/spec/example_app/spec/features/date_time_relative_look_spec.rb new file mode 100644 index 0000000000..a16bd376f0 --- /dev/null +++ b/spec/example_app/spec/features/date_time_relative_look_spec.rb @@ -0,0 +1,23 @@ +require "rails_helper" + +RSpec.feature "DateTime relative look", type: :feature do + let(:c1) { create(:customer, name: "John Petrucci") } + let(:o1) { create(:order, customer: c1) } + + it "renders the created_at field with a relative look on the index page" do + create(:log_entry, action: "create", logeable: o1, created_at: 2.days.ago) + + visit admin_log_entries_path + + expect(page).to have_content("2 days") + end + + it "renders the created_at field with a default look on the show page" do + log_entry = create(:log_entry, action: "create", logeable: o1, created_at: 3.hours.ago) + formatted_date_time = Administrate::Field::DateTime.new(:created_at, log_entry.created_at, :show).datetime + + visit admin_log_entry_path(log_entry) + + expect(page).to have_content(formatted_date_time) + end +end diff --git a/spec/lib/fields/base_spec.rb b/spec/lib/fields/base_spec.rb index ee30f78caa..f6553d1e11 100644 --- a/spec/lib/fields/base_spec.rb +++ b/spec/lib/fields/base_spec.rb @@ -46,6 +46,34 @@ end end + context "when the look option is a hash" do + it "returns the partial prefixes based on the look option and page" do + field = field_class.new(:attribute, nil, :show, look: {show: :custom_show, form: :custom_form}) + allow(field_class).to receive(:to_s).and_return("Administrate::Field::String") + + prefixes = field.partial_prefixes + + expect(prefixes).to eq([ + "fields/string/looks/custom_show", "fields/string/looks/default", "fields/string", + "fields/base/looks/custom_show", "fields/base/looks/default", "fields/base" + ]) + end + + context "when the page is not in the look option" do + it "returns the partial prefixes based on the default look" do + field = field_class.new(:attribute, nil, :show, look: {form: :custom_form}) + allow(field_class).to receive(:to_s).and_return("Administrate::Field::String") + + prefixes = field.partial_prefixes + + expect(prefixes).to eq([ + "fields/string/looks/default", "fields/string", + "fields/base/looks/default", "fields/base" + ]) + end + end + end + context "when the look option is explicitly default" do it "returns the partial prefixes based on the field class" do field = field_class.new(:attribute, nil, :show, look: :default) @@ -74,6 +102,20 @@ end end + context "when the look option is an unexpected type (ex. Array)" do + it "returns the partial prefixes based on the default look (instead of raising an error)" do + field = field_class.new(:attribute, nil, :show, look: []) + allow(field_class).to receive(:to_s).and_return("Administrate::Field::String") + + prefixes = field.partial_prefixes + + expect(prefixes).to eq([ + "fields/string/looks/default", "fields/string", + "fields/base/looks/default", "fields/base" + ]) + end + end + context "when the field class is multiply inherited" do it "returns the partial prefixes based on the field class" do second_level_field_class = Class.new(field_class)