diff --git a/app/assets/javascripts/administrate/components/table.js b/app/assets/javascripts/administrate/components/table.js index c283eb410d..bb9b36919a 100644 --- a/app/assets/javascripts/administrate/components/table.js +++ b/app/assets/javascripts/administrate/components/table.js @@ -2,12 +2,14 @@ $(function() { var keycodes = { space: 32, enter: 13 }; var visitDataUrl = function(event) { - if (event.type=="click" || + if (event.type == "click" || event.keyCode == keycodes.space || event.keyCode == keycodes.enter) { - if(!event.target.href) { - window.location = $(event.target).closest("tr").data("url"); + if (!event.target.href) { + var url = $(event.target).closest("tr").data("url"); + + if (url) { window.location = url; } } } }; diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index a496574f40..52288ab20c 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -79,6 +79,17 @@ def nav_link_state(resource) end end + helper_method :valid_action? + def valid_action?(name, resource = resource_name) + !!routes.detect do |controller, action| + controller == resource.to_s.pluralize && action == name.to_s + end + end + + def routes + @routes ||= Namespace.new(namespace).routes + end + def records_per_page params[:per_page] || 20 end diff --git a/app/views/administrate/application/_collection.html.erb b/app/views/administrate/application/_collection.html.erb index c500e55479..11f75de125 100644 --- a/app/views/administrate/application/_collection.html.erb +++ b/app/views/administrate/application/_collection.html.erb @@ -47,16 +47,17 @@ to display a collection of resources in an HTML table. <% end %> <% end %> - + <% [valid_action?(:edit), valid_action?(:destroy)].count(true).times do %> + + <% end %> <% resources.each do |resource| %> > <% collection_presenter.attributes_for(resource).each do |attribute| %> @@ -64,19 +65,23 @@ to display a collection of resources in an HTML table. <% end %> - <%= link_to( - t("administrate.actions.edit"), - [:edit, namespace, resource], - class: "action-edit", - ) %> + <% if valid_action? :edit %> + <%= link_to( + t("administrate.actions.edit"), + [:edit, namespace, resource], + class: "action-edit", + ) %> + <% end %> - <%= link_to( - t("administrate.actions.destroy"), - [namespace, resource], - class: "table__action--destroy", - method: :delete, - data: { confirm: t("administrate.actions.confirm") } - ) %> + <% if valid_action? :destroy %> + <%= link_to( + t("administrate.actions.destroy"), + [namespace, resource], + class: "table__action--destroy", + method: :delete, + data: { confirm: t("administrate.actions.confirm") } + ) %> + <% end %> <% end %> diff --git a/app/views/administrate/application/edit.html.erb b/app/views/administrate/application/edit.html.erb index 4fac99aa51..99903beb0e 100644 --- a/app/views/administrate/application/edit.html.erb +++ b/app/views/administrate/application/edit.html.erb @@ -24,7 +24,7 @@ It displays a header, and renders the `_form` partial to do the heavy lifting. "Show #{page.page_title}", [namespace, page.resource], class: "button", - ) %> + ) if valid_action? :show %> diff --git a/app/views/administrate/application/index.html.erb b/app/views/administrate/application/index.html.erb index 63dc8b0651..f754046b74 100644 --- a/app/views/administrate/application/index.html.erb +++ b/app/views/administrate/application/index.html.erb @@ -51,7 +51,7 @@ It renders the `_table` partial to display details about the resources. "New #{page.resource_name.titleize.downcase}", [:new, namespace, page.resource_name], class: "button", - ) %> + ) if valid_action? :new %> diff --git a/app/views/administrate/application/show.html.erb b/app/views/administrate/application/show.html.erb index b4e453b12f..b515a08d3b 100644 --- a/app/views/administrate/application/show.html.erb +++ b/app/views/administrate/application/show.html.erb @@ -25,7 +25,7 @@ as well as a link to its edit page. "Edit", [:edit, namespace, page.resource], class: "button", - ) %> + ) if valid_action? :edit %> diff --git a/app/views/fields/belongs_to/_index.html.erb b/app/views/fields/belongs_to/_index.html.erb index 86df398be2..005dba2b91 100644 --- a/app/views/fields/belongs_to/_index.html.erb +++ b/app/views/fields/belongs_to/_index.html.erb @@ -16,8 +16,12 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to( - field.display_associated_resource, - [namespace, field.data], - ) %> + <% if valid_action?(:show, field.attribute) %> + <%= link_to( + field.display_associated_resource, + [namespace, field.data], + ) %> + <% else %> + <%= field.display_associated_resource %> + <% end %> <% end %> diff --git a/app/views/fields/belongs_to/_show.html.erb b/app/views/fields/belongs_to/_show.html.erb index ca64897cfd..eeb0650f0b 100644 --- a/app/views/fields/belongs_to/_show.html.erb +++ b/app/views/fields/belongs_to/_show.html.erb @@ -16,8 +16,12 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <%= link_to( - field.display_associated_resource, - [namespace, field.data], - ) %> + <% if valid_action?(:show, field.attribute) %> + <%= link_to( + field.display_associated_resource, + [namespace, field.data], + ) %> + <% else %> + <%= field.display_associated_resource %> + <% end %> <% end %> diff --git a/lib/administrate/namespace.rb b/lib/administrate/namespace.rb index 8b62b350ee..79ab2e7cc3 100644 --- a/lib/administrate/namespace.rb +++ b/lib/administrate/namespace.rb @@ -5,8 +5,14 @@ def initialize(namespace) end def resources - namespace_controller_paths.uniq.map do |controller| - controller.gsub(/^#{namespace}\//, "").to_sym + @resources ||= routes.map(&:first).uniq.map(&:to_sym) + end + + def routes + @routes ||= all_routes.select do |controller, _action| + controller.starts_with?(namespace.to_s) + end.map do |controller, action| + [controller.gsub(/^#{namespace}\//, ""), action] end end @@ -14,15 +20,9 @@ def resources attr_reader :namespace - def namespace_controller_paths - all_controller_paths.select do |controller| - controller.starts_with?(namespace.to_s) - end - end - - def all_controller_paths + def all_routes Rails.application.routes.routes.map do |route| - route.defaults[:controller].to_s + route.defaults.values_at(:controller, :action).map(&:to_s) end end end