diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index d3229f84a4d81189d54e1271e0e3303b82cf3ca7..41033075dc8eb53fbd5adbae6a0c301439295014 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -37,7 +37,7 @@ module Administrate if resource.save redirect_to( - [Administrate::NAMESPACE, resource], + [namespace, resource], notice: translate_with_resource("create.success"), ) else @@ -50,7 +50,7 @@ module Administrate def update if requested_resource.update(resource_params) redirect_to( - [Administrate::NAMESPACE, requested_resource], + [namespace, requested_resource], notice: translate_with_resource("update.success"), ) else @@ -105,7 +105,8 @@ module Administrate dashboard.permitted_attributes end - delegate :resource_class, :resource_name, to: :resource_resolver + delegate :resource_class, :resource_name, :namespace, to: :resource_resolver + helper_method :namespace def resource_resolver @_resource_resolver ||= diff --git a/app/views/administrate/application/_collection.html.erb b/app/views/administrate/application/_collection.html.erb index a11e8fd6326f806415201ba856e4305a452e4e59..cc4c41117d41bf8a358a9aa684b4b1d4302ffb7b 100644 --- a/app/views/administrate/application/_collection.html.erb +++ b/app/views/administrate/application/_collection.html.erb @@ -47,7 +47,7 @@ to display a collection of resources in an HTML table. <tr class="table__row" role="link" tabindex="0" - data-url="<%= polymorphic_path([Administrate::NAMESPACE, resource]) -%>" + data-url="<%= polymorphic_path([namespace, resource]) -%>" > <% collection_presenter.attributes_for(resource).each do |attribute| %> <td class="cell-data cell-data--<%= attribute.html_class %>"> @@ -57,13 +57,13 @@ to display a collection of resources in an HTML table. <td><%= link_to( t("administrate.actions.edit"), - [:edit, Administrate::NAMESPACE, resource], + [:edit, namespace, resource], class: "action-edit", ) %></td> <td><%= link_to( t("administrate.actions.destroy"), - [Administrate::NAMESPACE, resource], + [namespace, resource], class: "table__action--destroy", method: :delete, data: { confirm: t("administrate.actions.confirm") } diff --git a/app/views/administrate/application/_form.html.erb b/app/views/administrate/application/_form.html.erb index 9f7a2719ed5f02d6e31d035c2c79bfd026b144e4..6f83ccc26239001b12162be585f7b64fd8f02df4 100644 --- a/app/views/administrate/application/_form.html.erb +++ b/app/views/administrate/application/_form.html.erb @@ -14,7 +14,7 @@ and renders all form fields for a resource's editable attributes. [1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Form %> -<%= form_for([Administrate::NAMESPACE, page.resource], html: { class: "form" }) do |f| %> +<%= form_for([namespace, page.resource], html: { class: "form" }) do |f| %> <% if page.resource.errors.any? %> <div id="error_explanation"> <h2> diff --git a/app/views/administrate/application/_sidebar.html.erb b/app/views/administrate/application/_sidebar.html.erb index 74f5141d807c740e6f077581690d8d12fe8570db..cf636129ea34ff5cff095abb851d81c34686e609 100644 --- a/app/views/administrate/application/_sidebar.html.erb +++ b/app/views/administrate/application/_sidebar.html.erb @@ -12,7 +12,7 @@ as defined by the DashboardManifest. <li> <%= link_to( display_resource_name(resource), - [Administrate::NAMESPACE, resource], + [namespace, resource], class: "sidebar__link sidebar__link--#{nav_link_state(resource)}" ) %> </li> diff --git a/app/views/administrate/application/edit.html.erb b/app/views/administrate/application/edit.html.erb index 8a83d092314c07a17a17c12cd3199483bced145e..1ee3a36ef3aad512d5b4fe1357ade90115c15fa3 100644 --- a/app/views/administrate/application/edit.html.erb +++ b/app/views/administrate/application/edit.html.erb @@ -22,7 +22,7 @@ It displays a header, and renders the `_form` partial to do the heavy lifting. <div class="header-actions"> <%= link_to( "Show #{page.page_title}", - [Administrate::NAMESPACE, page.resource], + [namespace, page.resource], class: "button", ) %> </div> diff --git a/app/views/administrate/application/index.html.erb b/app/views/administrate/application/index.html.erb index ef2a726af8dae444bfa35c53c235b2659b582859..5308ae058b40b0fbc37ba052d251dd08c5360e44 100644 --- a/app/views/administrate/application/index.html.erb +++ b/app/views/administrate/application/index.html.erb @@ -48,7 +48,7 @@ It renders the `_table` partial to display details about the resources. <div class="header-actions"> <%= link_to( "New #{page.resource_name.titleize.downcase}", - [:new, Administrate::NAMESPACE, page.resource_name], + [:new, namespace, page.resource_name], class: "button", ) %> </div> diff --git a/app/views/administrate/application/show.html.erb b/app/views/administrate/application/show.html.erb index 45e0f51159e3e20c7521540fcd93d3930bdd5a9e..0f894ef28f2bc01f7c862157359f1702d4c812aa 100644 --- a/app/views/administrate/application/show.html.erb +++ b/app/views/administrate/application/show.html.erb @@ -23,7 +23,7 @@ as well as a link to its edit page. <div class="header-actions"> <%= link_to( "Edit", - [:edit, Administrate::NAMESPACE, page.resource], + [:edit, namespace, page.resource], class: "button", ) %> </div> diff --git a/app/views/fields/belongs_to/_index.html.erb b/app/views/fields/belongs_to/_index.html.erb index 5df755f723849d292380928c2e4c52e7eddd88f3..86df398be29877f88f064dd610f59b2d32dd2d13 100644 --- a/app/views/fields/belongs_to/_index.html.erb +++ b/app/views/fields/belongs_to/_index.html.erb @@ -18,6 +18,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data], + [namespace, field.data], ) %> <% end %> diff --git a/app/views/fields/belongs_to/_show.html.erb b/app/views/fields/belongs_to/_show.html.erb index df80d57ba3d1d9aa1d2826fb6513ae650ce48e78..ca64897cfd68c7a29dee09f89949129bb5d34dd0 100644 --- a/app/views/fields/belongs_to/_show.html.erb +++ b/app/views/fields/belongs_to/_show.html.erb @@ -18,6 +18,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data], + [namespace, field.data], ) %> <% end %> diff --git a/app/views/fields/has_one/_index.html.erb b/app/views/fields/has_one/_index.html.erb index ccc86d99a4755fcf8c6976a1db6f1a45c5b05446..5eaf6e46065a5cf9688a519d6696e52501067a82 100644 --- a/app/views/fields/has_one/_index.html.erb +++ b/app/views/fields/has_one/_index.html.erb @@ -18,6 +18,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data], + [namespace, field.data], ) %> <% end %> diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index 21017c9ac13900ab35e47ce6238d128213ff26d0..19c10f038dea9f15caf351f0842b1f6a1cdc0879 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -18,6 +18,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data], + [namespace, field.data], ) %> <% end %> diff --git a/app/views/fields/polymorphic/_index.html.erb b/app/views/fields/polymorphic/_index.html.erb index 58b009e2bb8371146070eefe5cd6834edeb48c2f..7c27a3b37903b335fea8cfa50a1d08b086460b01 100644 --- a/app/views/fields/polymorphic/_index.html.erb +++ b/app/views/fields/polymorphic/_index.html.erb @@ -19,6 +19,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data] + [namespace, field.data] ) %> <% end %> diff --git a/app/views/fields/polymorphic/_show.html.erb b/app/views/fields/polymorphic/_show.html.erb index c81234989dd2605ab4eacd8fa9d71a63dbc72a45..13047830abf9ea4fa5a2d6a957081a4cd5832a59 100644 --- a/app/views/fields/polymorphic/_show.html.erb +++ b/app/views/fields/polymorphic/_show.html.erb @@ -19,6 +19,6 @@ By default, the relationship is rendered as a link to the associated object. <% if field.data %> <%= link_to( field.display_associated_resource, - [Administrate::NAMESPACE, field.data], + [namespace, field.data], ) %> <% end %> diff --git a/lib/administrate/engine.rb b/lib/administrate/engine.rb index cc7e44b1b2cee41e27d8b9cf13e76761628e6b8d..0c1a280c5127b83dafd192e1456d356f5a3acd0a 100644 --- a/lib/administrate/engine.rb +++ b/lib/administrate/engine.rb @@ -9,7 +9,6 @@ require "sass-rails" require "selectize-rails" require "sprockets/railtie" -require "administrate/namespace" require "administrate/page/form" require "administrate/page/show" require "administrate/page/collection" diff --git a/lib/administrate/namespace.rb b/lib/administrate/namespace.rb deleted file mode 100644 index 90288951297c25ec0c29320897d4b06527bfbb13..0000000000000000000000000000000000000000 --- a/lib/administrate/namespace.rb +++ /dev/null @@ -1,3 +0,0 @@ -module Administrate - NAMESPACE = :admin -end diff --git a/lib/administrate/resource_resolver.rb b/lib/administrate/resource_resolver.rb index cbc21314bc0c368f37a8662b7ae5cfd250d93545..553e7b38fe8aedb92acb7e9f06c4526ec0f066a2 100644 --- a/lib/administrate/resource_resolver.rb +++ b/lib/administrate/resource_resolver.rb @@ -1,5 +1,3 @@ -require "administrate/namespace" - module Administrate class ResourceResolver def initialize(controller_path) @@ -10,6 +8,10 @@ module Administrate Object.const_get(resource_class_name + "Dashboard") end + def namespace + controller_path.split("/").first + end + def resource_class Object.const_get(resource_class_name) end @@ -33,7 +35,7 @@ module Administrate end def controller_path_parts - controller_path.singularize.split("/") - [Administrate::NAMESPACE.to_s] + controller_path.singularize.split("/")[1..-1] end attr_reader :controller_path diff --git a/spec/administrate/views/fields/has_one/_index_spec.rb b/spec/administrate/views/fields/has_one/_index_spec.rb index acc8c0f61cf99468c38ad2c2561de0056c47d984..b9c5a28b51f52f766192fda6e527010f200d44a6 100644 --- a/spec/administrate/views/fields/has_one/_index_spec.rb +++ b/spec/administrate/views/fields/has_one/_index_spec.rb @@ -1,7 +1,7 @@ require "rails_helper" describe "fields/has_one/_index", type: :view do - context "without an associated records" do + context "without an associated record" do it "displays nothing" do has_one = double(data: nil) @@ -26,7 +26,7 @@ describe "fields/has_one/_index", type: :view do render( partial: "fields/has_one/index.html.erb", - locals: { field: has_one }, + locals: { field: has_one, namespace: "admin" }, ) expected = "<a href=\"#{product_path}\">#{product.name}</a>" diff --git a/spec/administrate/views/fields/has_one/_show_spec.rb b/spec/administrate/views/fields/has_one/_show_spec.rb index cc9575c10a07249f3e0dafc4675f4573117d3138..795bea3f01436c991beb219cb23fa6016d27fc32 100644 --- a/spec/administrate/views/fields/has_one/_show_spec.rb +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" require "administrate/fields/has_one" describe "fields/has_one/_show", type: :view do - context "without an associated records" do + context "without an associated record" do it "displays nothing" do has_one = instance_double( "Administrate::Field::HasOne", @@ -31,7 +31,7 @@ describe "fields/has_one/_show", type: :view do render( partial: "fields/has_one/show.html.erb", - locals: { field: has_one }, + locals: { field: has_one, namespace: "admin" }, ) expected = "<a href=\"#{product_path}\">#{product.name}</a>" diff --git a/spec/administrate/views/fields/polymorphic/_index_spec.rb b/spec/administrate/views/fields/polymorphic/_index_spec.rb index 3265d4855a6b034678035394610752faa77d18fe..27db78513248ca4b63b8929ab19e93e6ddbe9fc0 100644 --- a/spec/administrate/views/fields/polymorphic/_index_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_index_spec.rb @@ -26,7 +26,7 @@ describe "fields/polymorphic/_index", type: :view do render( partial: "fields/polymorphic/index.html.erb", - locals: { field: polymorphic }, + locals: { field: polymorphic, namespace: "admin" }, ) expected = "<a href=\"#{product_path}\">#{product.name}</a>" diff --git a/spec/administrate/views/fields/polymorphic/_show_spec.rb b/spec/administrate/views/fields/polymorphic/_show_spec.rb index 6d872372d0f01a4a4f44df6f359b98dcab58d043..4f3d5f0f229525e4ab0d323c5820336390c644ae 100644 --- a/spec/administrate/views/fields/polymorphic/_show_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_show_spec.rb @@ -31,7 +31,7 @@ describe "fields/polymorphic/_show", type: :view do render( partial: "fields/polymorphic/show.html.erb", - locals: { field: polymorphic }, + locals: { field: polymorphic, namespace: "admin" }, ) expected = "<a href=\"#{product_path}\">#{product.name}</a>" diff --git a/spec/lib/administrate/resource_resolver_spec.rb b/spec/lib/administrate/resource_resolver_spec.rb index 46a26dbd8764e06dddc9335cd50e65dd037aea64..59493b8672cf1ad318d22dd6dbcd1d4340c34498 100644 --- a/spec/lib/administrate/resource_resolver_spec.rb +++ b/spec/lib/administrate/resource_resolver_spec.rb @@ -1,50 +1,59 @@ require "spec_helper" +require "active_support/core_ext/string/inflections" require "support/constant_helpers" require "administrate/resource_resolver" describe Administrate::ResourceResolver do - describe "#resource_class" do + describe "#dashboard_class" do it "handles global-namepsace models" do begin - class User; end + class UserDashboard; end resolver = Administrate::ResourceResolver.new("admin/users") - expect(resolver.resource_class).to eq(User) + expect(resolver.dashboard_class).to eq(UserDashboard) ensure - remove_constants :User + remove_constants :UserDashboard end end it "handles namespaced models" do begin - module Blog; class Post; end; end + module Blog; class PostDashboard; end; end resolver = Administrate::ResourceResolver.new("admin/blog/posts") - expect(resolver.resource_class).to eq(Blog::Post) + expect(resolver.dashboard_class).to eq(Blog::PostDashboard) ensure remove_constants :Blog end end end - describe "#dashboard_class" do + describe "#namespace" do + it "returns the top-level namespace" do + resolver = Administrate::ResourceResolver.new("foobar/user") + + expect(resolver.namespace).to eq("foobar") + end + end + + describe "#resource_class" do it "handles global-namepsace models" do begin - class UserDashboard; end + class User; end resolver = Administrate::ResourceResolver.new("admin/users") - expect(resolver.dashboard_class).to eq(UserDashboard) + expect(resolver.resource_class).to eq(User) ensure - remove_constants :UserDashboard + remove_constants :User end end it "handles namespaced models" do begin - module Blog; class PostDashboard; end; end + module Blog; class Post; end; end resolver = Administrate::ResourceResolver.new("admin/blog/posts") - expect(resolver.dashboard_class).to eq(Blog::PostDashboard) + expect(resolver.resource_class).to eq(Blog::Post) ensure remove_constants :Blog end diff --git a/spec/support/table.rb b/spec/support/table.rb index 3bd71b04f4fb8ff73502738fa00a672da1185a8c..93e093ac786cb4f61080d209afffbbd6bd650266 100644 --- a/spec/support/table.rb +++ b/spec/support/table.rb @@ -17,7 +17,7 @@ module Features def url_for(model) "/" + [ - Administrate::NAMESPACE, + :admin, model.class.to_s.underscore.pluralize, model.to_param, ].join("/")