diff --git a/lib/trestle/admin.rb b/lib/trestle/admin.rb index 128f7642..466fdb00 100644 --- a/lib/trestle/admin.rb +++ b/lib/trestle/admin.rb @@ -65,7 +65,7 @@ def controller_namespace "#{name.underscore}/admin" end - def path(action=:index, options={}) + def path(action=root_action, options={}) Engine.routes.url_for(options.merge(controller: controller_namespace, action: action, only_path: true)) end @@ -73,6 +73,10 @@ def actions [:index] end + def root_action + :index + end + def routes admin = self diff --git a/lib/trestle/resource.rb b/lib/trestle/resource.rb index c8417055..cff70c0f 100644 --- a/lib/trestle/resource.rb +++ b/lib/trestle/resource.rb @@ -99,10 +99,18 @@ def actions @actions ||= (readonly? ? READONLY_ACTIONS : RESOURCE_ACTIONS).dup end + def root_action + singular? ? :show : :index + end + def readonly? options[:readonly] end + def singular? + options[:singular] + end + def default_breadcrumb Breadcrumb.new(I18n.t("admin.breadcrumbs.#{admin_name}", default: model_name.plural.titleize), path) end @@ -110,8 +118,17 @@ def default_breadcrumb def routes admin = self + resource_method = singular? ? :resource : :resources + resource_name = admin_name + resource_options = { + controller: controller_namespace, + as: route_name, + path: options[:path], + except: (RESOURCE_ACTIONS - actions) + } + Proc.new do - resources admin.admin_name, controller: admin.controller_namespace, as: admin.route_name, path: admin.options[:path], except: (RESOURCE_ACTIONS - admin.actions) do + public_send(resource_method, resource_name, resource_options) do instance_exec(&admin.additional_routes) if admin.additional_routes end end