Skip to content

Commit

Permalink
Merge pull request #27 from goosys/wip/issue-14
Browse files Browse the repository at this point in the history
Wip/issue 14
  • Loading branch information
goosys authored Jul 21, 2024
2 parents 3503362 + b5b121d commit 90ceb35
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ gem "rake", "~> 13.0"
gem 'refile', require: 'refile/rails', github: 'vitalinfo/refile'
gem 'refile-mini_magick'

gem "sprockets-rails"
gem "administrate", github: "goosys/administrate", branch: "feature/contextualize"

group :development, :test do
gem "rspec", "~> 3.0"
gem 'rspec-rails'
Expand Down
11 changes: 8 additions & 3 deletions app/views/administrate/application/_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,27 @@ to display a collection of resources in an HTML table.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
%>

<table aria-labelledby="<%= table_title %>">
<table aria-labelledby="<%= table_title %>" data-controller="table" data-action="click->table#visitDataUrl keydown->table#visitDataUrl">
<thead>
<tr>
<% collection_presenter.attribute_types.each do |attr_name, attr_type| %>
<th class="cell-label
cell-label--<%= attr_type.html_class %>
cell-label--<%= collection_presenter.ordered_html_class(attr_name) %>
cell-label--<%= "#{collection_presenter.resource_name}_#{attr_name}" %>"
cell-label--<%= "#{collection_presenter.resource_name}_#{attr_name}" %>
cell-span--<%= [:id,:status].include?(attr_name) ? attr_name : 'normal' %>"
scope="col"
aria-sort="<%= sort_order(collection_presenter.ordered_html_class(attr_name)) %>">
<%= link_to(sanitized_order_params(page, collection_field_name).merge(
collection_presenter.order_params_for(attr_name, key: collection_field_name)
)) do %>
<%= t(
"helpers.label.#{collection_presenter.resource_name}.#{attr_name}",
default: resource_class.human_attribute_name(attr_name).titleize,
default: [
:"activerecord.attributes.#{resource_class.name.downcase}.#{collection_field_name}/#{attr_name}",
:"activerecord.attributes.#{collection_presenter.resource_name}.#{attr_name}",
resource_class.human_attribute_name(attr_name).titleize
]
) %>
<% if collection_presenter.ordered_by?(attr_name) %>
<span class="cell-label__sort-indicator cell-label__sort-indicator--<%= collection_presenter.ordered_html_class(attr_name) %>">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% [existing_action?(collection_presenter.resource_name, :edit),
existing_action?(collection_presenter.resource_name, :destroy)].count(true).times do %>
<th scope="col"></th>
<th scope="col" class="cell-span--actions"></th>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
class: "action-edit button button--info",
) if accessible_action?(resource, :edit) %></td>
<% end %>

<% if existing_action?(collection_presenter.resource_name, :destroy) %>
<td><%= link_to(
<td><%= button_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "text-color-red",
class: "action-destroy button button--danger",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
data: { turbo_confirm: t("administrate.actions.confirm") }
) if accessible_action?(resource, :destroy) %></td>
<% end %>
2 changes: 1 addition & 1 deletion app/views/administrate/application/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ and renders all form fields for a resource's editable attributes.
<% end -%>

<div class="form-actions">
<%= f.submit %>
<%= f.submit class: "button button--update" %>
</div>
<% end %>
5 changes: 3 additions & 2 deletions app/views/administrate/application/_index_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
<div>
<%= link_to(
t(
"administrate.actions.new_resource",
"administrate.actions.new_button",
default: [:'administrate.actions.new_resource'],
name: display_resource_name(page.resource_name, singular: true).downcase
),
[:new, namespace, page.resource_path.to_sym],
class: "button",
class: "button button--info",
) if accessible_action?(new_resource, :new) %>
</div>

Expand Down
11 changes: 8 additions & 3 deletions app/views/administrate/application/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ It displays a header, and renders the `_form` partial to do the heavy lifting.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form
%>

<% content_for(:title) { t("administrate.actions.edit_resource", name: page.page_title) } %>
<% content_for(:title) do %>
<%= display_resource_name(page.resource_name) + ' > ' + t(
"administrate.actions.edit_resource",
name: page.page_title
) %>
<% end %>

<header class="main-content__header">
<h1 class="main-content__page-title">
Expand All @@ -24,9 +29,9 @@ It displays a header, and renders the `_form` partial to do the heavy lifting.

<div>
<%= link_to(
t("administrate.actions.show_resource", name: page.page_title),
t("administrate.actions.show_button", name: page.page_title, default: [:'administrate.actions.show_resource']),
[namespace, page.resource],
class: "button",
class: "button button--cancel",
) if accessible_action?(page.resource, :show) %>
</div>

Expand Down
6 changes: 3 additions & 3 deletions app/views/administrate/application/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ to do the heavy lifting.
%>

<% content_for(:title) do %>
<%= t(
<%= display_resource_name(page.resource_name) + ' > ' + t(
"administrate.actions.new_resource",
name: display_resource_name(page.resource_name).titleize
name: display_resource_name(page.resource_name, singular: true).titleize
) %>
<% end %>

Expand All @@ -28,7 +28,7 @@ to do the heavy lifting.
</h1>

<div>
<%= link_to t("administrate.actions.back"), :back, class: "button" %>
<%= link_to t("administrate.actions.back"), :back, class: "button button--cancel" %>
</div>

<%= content_for(:header_last) %>
Expand Down
15 changes: 10 additions & 5 deletions app/views/administrate/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ as well as a link to its edit page.
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Show
%>

<% content_for(:title) { t("administrate.actions.show_resource", name: page.page_title) } %>
<% content_for(:title) do %>
<%= display_resource_name(page.resource_name) + ' > ' + t(
"administrate.actions.show_resource",
name: page.page_title
) %>
<% end %>

<header class="main-content__header">
<h1 class="main-content__page-title">
Expand All @@ -25,17 +30,17 @@ as well as a link to its edit page.

<div>
<%= link_to(
t("administrate.actions.edit_resource", name: page.page_title),
t("administrate.actions.edit_button", name: page.page_title, default: [:'administrate.actions.edit_resource']),
[:edit, namespace, page.resource],
class: "button",
class: "button button--info",
) if accessible_action?(page.resource, :edit) %>

<%= link_to(
<%= button_to(
t("administrate.actions.destroy"),
[namespace, page.resource],
class: "button button--danger",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
data: { turbo_confirm: t("administrate.actions.confirm") }
) if accessible_action?(page.resource, :destroy) %>
</div>

Expand Down
2 changes: 2 additions & 0 deletions config/locales/administrate.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ en:
application:
home:
title: Dashboard
title:
show_resource: "%{model_name} %{resource_name}"
1 change: 1 addition & 0 deletions lib/administrate/mistybird.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative "patches/administrate_datetime_default"
# require_relative "patches/administrate_view_generator"
require_relative "patches/dashboard_name_option_to_nested_has_many"
require_relative "patches/default_display_resource_in_dashboard"
require_relative "patches/enable_skip_option_in_nested_has_many"
require_relative "patches/pass_parent_resource_to_nested_has_many"
require_relative "patches/role_aware_resource_resolver"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require "administrate/base_dashboard"

Administrate::BaseDashboard.prepend (
Module.new {
def display_resource(resource)
["##{resource.id}", resource.try(:name) || resource.try(:title)].compact.join(' ')
end
}
)
41 changes: 41 additions & 0 deletions spec/administrate/base_dashboard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,45 @@ module Library; module Book; end; end
remove_constants :Library
end
end

describe "#display_resource" do
context "when the resource has a name attribute" do
it "returns the id and name of the resource" do
class CustomerDashboard < Administrate::BaseDashboard; end
customer = double(id: 1, name: "Example Customer")
dashboard = CustomerDashboard.new

rendered_resource = dashboard.display_resource(customer)
expect(rendered_resource).to eq("#1 Example Customer")
ensure
remove_constants :CustomerDashboard
end
end

context "when the resource has a title attribute" do
it "returns the id and title of the resource" do
class BookDashboard < Administrate::BaseDashboard; end
book = double(id: 1, title: "Example Title")
dashboard = BookDashboard.new

rendered_resource = dashboard.display_resource(book)
expect(rendered_resource).to eq("#1 Example Title")
ensure
remove_constants :BookDashboard
end
end

context "when the resource has not a name or title attribute" do
it "returns the id of the resource" do
class LogDashboard < Administrate::BaseDashboard; end
log = double(id: 1)
dashboard = LogDashboard.new

rendered_resource = dashboard.display_resource(log)
expect(rendered_resource).to eq("#1")
ensure
remove_constants :LogDashboard
end
end
end
end

0 comments on commit 90ceb35

Please sign in to comment.