Skip to content
Open
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
8 changes: 8 additions & 0 deletions docs/customizing_dashboards.md
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
```
12 changes: 11 additions & 1 deletion lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
7 changes: 4 additions & 3 deletions spec/example_app/app/dashboards/log_entry_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)}]"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<% if field.data %>
<span title="<%= field.datetime %>">
<%= time_ago_in_words(field.data) %>
</span>
<% end %>
23 changes: 23 additions & 0 deletions spec/example_app/spec/features/date_time_relative_look_spec.rb
Original file line number Diff line number Diff line change
@@ -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
42 changes: 42 additions & 0 deletions spec/lib/fields/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down