Skip to content

Commit

Permalink
Allow to limit route actions
Browse files Browse the repository at this point in the history
This allows to limit route actions such as :index and :show in routes.rb
and only display these routes in views.
  • Loading branch information
infertux committed May 27, 2016
1 parent 328aaec commit a0303fa
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 39 deletions.
8 changes: 5 additions & 3 deletions app/assets/javascripts/administrate/components/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
};
Expand Down
11 changes: 11 additions & 0 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions app/views/administrate/application/_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,41 @@ to display a collection of resources in an HTML table.
<% end %>
</th>
<% end %>
<th colspan="2" scope="col"></th>
<% [valid_action?(:edit), valid_action?(:destroy)].count(true).times do %>
<th scope="col"></th>
<% end %>
</tr>
</thead>

<tbody>
<% resources.each do |resource| %>
<tr class="table__row"
role="link"
tabindex="0"
data-url="<%= polymorphic_path([namespace, resource]) -%>"
<%= %(role=link data-url=#{polymorphic_path([namespace, resource])}) if valid_action? :show -%>
>
<% collection_presenter.attributes_for(resource).each do |attribute| %>
<td class="cell-data cell-data--<%= attribute.html_class %>">
<%= render_field attribute %>
</td>
<% end %>

<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) %></td>
<% if valid_action? :edit %>
<td><%= link_to(
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) %></td>
<% end %>

<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "table__action--destroy",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) %></td>
<% if valid_action? :destroy %>
<td><%= link_to(
t("administrate.actions.destroy"),
[namespace, resource],
class: "table__action--destroy",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) %></td>
<% end %>
</tr>
<% end %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</header>

Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</header>

Expand Down
2 changes: 1 addition & 1 deletion app/views/administrate/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ as well as a link to its edit page.
"Edit",
[:edit, namespace, page.resource],
class: "button",
) %>
) if valid_action? :edit %>
</div>
</header>

Expand Down
12 changes: 8 additions & 4 deletions app/views/fields/belongs_to/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
12 changes: 8 additions & 4 deletions app/views/fields/belongs_to/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
20 changes: 10 additions & 10 deletions lib/administrate/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,24 @@ 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

private

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
Expand Down

0 comments on commit a0303fa

Please sign in to comment.