Skip to content

Commit 0bfdc0a

Browse files
committed
Add support for page-specific look configuration
1 parent ebbe215 commit 0bfdc0a

File tree

6 files changed

+93
-4
lines changed

6 files changed

+93
-4
lines changed

docs/customizing_dashboards.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,11 @@ fields/base/looks/customer_card/
664664
fields/base/looks/default/
665665
fields/base/
666666
```
667+
668+
You can also select a different look for each page by passing a Hash to `look` option.
669+
670+
```ruby
671+
ATTRIBUTE_TYPES = {
672+
created_at: Field::DateTime.with_options(look: {index: :relative})
673+
}
674+
```

lib/administrate/field/base.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,17 @@ def partial_prefixes
104104
end
105105

106106
def look
107-
(options.fetch(:look, :default).presence || :default).to_sym
107+
val = options.fetch(:look, :default).presence || :default
108+
case val
109+
when Symbol
110+
val
111+
when ::String
112+
val.to_sym
113+
when Hash
114+
val.fetch(page, :default).to_sym
115+
else
116+
:default
117+
end
108118
end
109119

110120
def required?

spec/example_app/app/dashboards/log_entry_dashboard.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ class LogEntryDashboard < Administrate::BaseDashboard
66
ATTRIBUTE_TYPES = {
77
id: Field::Number,
88
action: Field::String,
9-
logeable: Field::Polymorphic.with_options(classes: [Customer, ::Order])
9+
logeable: Field::Polymorphic.with_options(classes: [Customer, ::Order]),
10+
created_at: Field::DateTime.with_options(look: {index: :relative})
1011
}.freeze
1112

12-
COLLECTION_ATTRIBUTES = [:id] + ATTRIBUTES
13+
COLLECTION_ATTRIBUTES = [:id] + ATTRIBUTES + [:created_at]
1314
FORM_ATTRIBUTES = ATTRIBUTES
14-
SHOW_PAGE_ATTRIBUTES = ATTRIBUTES
15+
SHOW_PAGE_ATTRIBUTES = ATTRIBUTES + [:created_at]
1516

1617
def display_resource(resource)
1718
"#{resource.action}[#{safe_display_logeable(resource.logeable)}]"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<% if field.data %>
2+
<span title="<%= field.datetime %>">
3+
<%= time_ago_in_words(field.data) %>
4+
</span>
5+
<% end %>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
require "rails_helper"
2+
3+
RSpec.feature "DateTime relative look", type: :feature do
4+
let(:c1) { create(:customer, name: "John Petrucci") }
5+
let(:o1) { create(:order, customer: c1) }
6+
7+
it "renders the created_at field with a relative look on the index page" do
8+
create(:log_entry, action: "create", logeable: o1, created_at: 2.days.ago)
9+
10+
visit admin_log_entries_path
11+
12+
expect(page).to have_content("2 days")
13+
end
14+
15+
it "renders the created_at field with a default look on the show page" do
16+
log_entry = create(:log_entry, action: "create", logeable: o1, created_at: 3.hours.ago)
17+
formatted_date_time = Administrate::Field::DateTime.new(:created_at, log_entry.created_at, :show).datetime
18+
19+
visit admin_log_entry_path(log_entry)
20+
21+
expect(page).to have_content(formatted_date_time)
22+
end
23+
end

spec/lib/fields/base_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,34 @@
4646
end
4747
end
4848

49+
context "when the look option is a hash" do
50+
it "returns the partial prefixes based on the look option and page" do
51+
field = field_class.new(:attribute, nil, :show, look: {show: :custom_show, form: :custom_form})
52+
allow(field_class).to receive(:to_s).and_return("Administrate::Field::String")
53+
54+
prefixes = field.partial_prefixes
55+
56+
expect(prefixes).to eq([
57+
"fields/string/looks/custom_show", "fields/string/looks/default", "fields/string",
58+
"fields/base/looks/custom_show", "fields/base/looks/default", "fields/base"
59+
])
60+
end
61+
62+
context "when the page is not in the look option" do
63+
it "returns the partial prefixes based on the default look" do
64+
field = field_class.new(:attribute, nil, :show, look: {form: :custom_form})
65+
allow(field_class).to receive(:to_s).and_return("Administrate::Field::String")
66+
67+
prefixes = field.partial_prefixes
68+
69+
expect(prefixes).to eq([
70+
"fields/string/looks/default", "fields/string",
71+
"fields/base/looks/default", "fields/base"
72+
])
73+
end
74+
end
75+
end
76+
4977
context "when the look option is explicitly default" do
5078
it "returns the partial prefixes based on the field class" do
5179
field = field_class.new(:attribute, nil, :show, look: :default)
@@ -74,6 +102,20 @@
74102
end
75103
end
76104

105+
context "when the look option is an unexpected type (ex. Array)" do
106+
it "returns the partial prefixes based on the default look (instead of raising an error)" do
107+
field = field_class.new(:attribute, nil, :show, look: [])
108+
allow(field_class).to receive(:to_s).and_return("Administrate::Field::String")
109+
110+
prefixes = field.partial_prefixes
111+
112+
expect(prefixes).to eq([
113+
"fields/string/looks/default", "fields/string",
114+
"fields/base/looks/default", "fields/base"
115+
])
116+
end
117+
end
118+
77119
context "when the field class is multiply inherited" do
78120
it "returns the partial prefixes based on the field class" do
79121
second_level_field_class = Class.new(field_class)

0 commit comments

Comments
 (0)