diff --git a/app/helpers/administrate/application_helper.rb b/app/helpers/administrate/application_helper.rb index 6586dc67b8..c604850779 100644 --- a/app/helpers/administrate/application_helper.rb +++ b/app/helpers/administrate/application_helper.rb @@ -35,7 +35,7 @@ def model_from_resource(resource_name) # - Symbol: name of a resource class # - Class: resource class # - ActiveRecord::Base: resource instance - def administrate_valid_action?(target, action_name) + def accessible_action?(target, action_name) target = target.to_sym if target.is_a?(String) target_class_or_class_name = target.is_a?(ActiveRecord::Base) ? target.class : target diff --git a/app/views/administrate/application/_collection.html.erb b/app/views/administrate/application/_collection.html.erb index 0d9ec13cc9..044a53efdd 100644 --- a/app/views/administrate/application/_collection.html.erb +++ b/app/views/administrate/application/_collection.html.erb @@ -59,13 +59,13 @@ to display a collection of resources in an HTML table. <% resources.each do |resource| %> + <% if accessible_action?(resource, :show) %> <%= %(tabindex=0 role=link data-url=#{polymorphic_path([namespace, resource])}) %> <% end %> > <% collection_presenter.attributes_for(resource).each do |attribute| %> - <% if administrate_valid_action?(resource, :show) -%> + <% if accessible_action?(resource, :show) -%> + ) if accessible_action?(resource, :edit) %> <% end %> <% if existing_action?(:destroy, collection_presenter.resource_name) %> @@ -13,5 +13,5 @@ class: "text-color-red", method: :delete, data: { confirm: t("administrate.actions.confirm") } - ) if administrate_valid_action?(resource, :destroy) %> + ) if accessible_action?(resource, :destroy) %> <% end %> diff --git a/app/views/administrate/application/_index_header.html.erb b/app/views/administrate/application/_index_header.html.erb index c69d79d40d..ad069cac3a 100644 --- a/app/views/administrate/application/_index_header.html.erb +++ b/app/views/administrate/application/_index_header.html.erb @@ -23,6 +23,6 @@ ), [:new, namespace, page.resource_path.to_sym], class: "button", - ) if administrate_valid_action?(new_resource, :new) %> + ) if accessible_action?(new_resource, :new) %> diff --git a/app/views/administrate/application/_navigation.html.erb b/app/views/administrate/application/_navigation.html.erb index 014f6d13b5..8df01ef8f3 100644 --- a/app/views/administrate/application/_navigation.html.erb +++ b/app/views/administrate/application/_navigation.html.erb @@ -15,6 +15,6 @@ as defined by the routes in the `admin/` namespace display_resource_name(resource), resource_index_route(resource), class: "navigation__link navigation__link--#{nav_link_state(resource)}" - ) if administrate_valid_action?(resource.singularize, :index) %> + ) if accessible_action?(resource.singularize, :index) %> <% end %> diff --git a/app/views/administrate/application/edit.html.erb b/app/views/administrate/application/edit.html.erb index 2165022cd1..415de8a5c8 100644 --- a/app/views/administrate/application/edit.html.erb +++ b/app/views/administrate/application/edit.html.erb @@ -27,7 +27,7 @@ It displays a header, and renders the `_form` partial to do the heavy lifting. t("administrate.actions.show_resource", name: page.page_title), [namespace, page.resource], class: "button", - ) if administrate_valid_action?(page.resource, :show) %> + ) if accessible_action?(page.resource, :show) %> diff --git a/app/views/administrate/application/show.html.erb b/app/views/administrate/application/show.html.erb index 16b01d606a..71b4638ff7 100644 --- a/app/views/administrate/application/show.html.erb +++ b/app/views/administrate/application/show.html.erb @@ -28,7 +28,7 @@ as well as a link to its edit page. t("administrate.actions.edit_resource", name: page.page_title), [:edit, namespace, page.resource], class: "button", - ) if administrate_valid_action?(page.resource, :edit) %> + ) if accessible_action?(page.resource, :edit) %> <%= link_to( t("administrate.actions.destroy"), @@ -36,7 +36,7 @@ as well as a link to its edit page. class: "button button--danger", method: :delete, data: { confirm: t("administrate.actions.confirm") } - ) if administrate_valid_action?(page.resource, :destroy) %> + ) if accessible_action?(page.resource, :destroy) %> diff --git a/app/views/fields/belongs_to/_index.html.erb b/app/views/fields/belongs_to/_index.html.erb index d9a7e555e0..1e0ab2f4c9 100644 --- a/app/views/fields/belongs_to/_index.html.erb +++ b/app/views/fields/belongs_to/_index.html.erb @@ -16,7 +16,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if administrate_valid_action?(field.data, :show) %> + <% if accessible_action?(field.data, :show) %> <%= link_to( field.display_associated_resource, [namespace, field.data], diff --git a/app/views/fields/belongs_to/_show.html.erb b/app/views/fields/belongs_to/_show.html.erb index 3299bcc148..3bca714d68 100644 --- a/app/views/fields/belongs_to/_show.html.erb +++ b/app/views/fields/belongs_to/_show.html.erb @@ -16,7 +16,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if administrate_valid_action?(field.data, :show) %> + <% if accessible_action?(field.data, :show) %> <%= link_to( field.display_associated_resource, [namespace, field.data], diff --git a/app/views/fields/has_one/_index.html.erb b/app/views/fields/has_one/_index.html.erb index e8f32fa95c..4ff3ad1747 100644 --- a/app/views/fields/has_one/_index.html.erb +++ b/app/views/fields/has_one/_index.html.erb @@ -17,7 +17,7 @@ By default, the relationship is rendered as a link to the associated object. <% if field.linkable? %> <%= link_to_if( - administrate_valid_action?(field.data, :show), + accessible_action?(field.data, :show), field.display_associated_resource, [namespace, field.data], ) %> diff --git a/app/views/fields/has_one/_show.html.erb b/app/views/fields/has_one/_show.html.erb index 9e0a09989f..542af5e09d 100644 --- a/app/views/fields/has_one/_show.html.erb +++ b/app/views/fields/has_one/_show.html.erb @@ -19,7 +19,7 @@ All show page attributes of has_one relationship would be rendered
<%= link_to_if( - administrate_valid_action?(field.data, :show), + accessible_action?(field.data, :show), field.display_associated_resource, [namespace, field.data], ) %> diff --git a/app/views/fields/polymorphic/_index.html.erb b/app/views/fields/polymorphic/_index.html.erb index 35ec725312..acb16e4086 100644 --- a/app/views/fields/polymorphic/_index.html.erb +++ b/app/views/fields/polymorphic/_index.html.erb @@ -17,7 +17,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if administrate_valid_action?(field.data, :show) %> + <% if accessible_action?(field.data, :show) %> <%= link_to( field.display_associated_resource, [namespace, field.data] diff --git a/app/views/fields/polymorphic/_show.html.erb b/app/views/fields/polymorphic/_show.html.erb index 4daad67383..a656d2925e 100644 --- a/app/views/fields/polymorphic/_show.html.erb +++ b/app/views/fields/polymorphic/_show.html.erb @@ -17,7 +17,7 @@ By default, the relationship is rendered as a link to the associated object. %> <% if field.data %> - <% if administrate_valid_action?(field.data, :show) %> + <% if accessible_action?(field.data, :show) %> <%= link_to( field.display_associated_resource, [namespace, field.data], diff --git a/lib/administrate.rb b/lib/administrate.rb index 0e721d9aa9..7218fb06af 100644 --- a/lib/administrate.rb +++ b/lib/administrate.rb @@ -24,7 +24,7 @@ def self.warn_of_deprecated_option(name) def self.warn_of_deprecated_authorization_method(method) ActiveSupport::Deprecation.warn( "The method `#{method}` is deprecated. " + - "Please use `administrate_valid_action?` instead, " + + "Please use `accessible_action?` instead, " + "or see the documentation for other options.", ) end diff --git a/spec/administrate/views/fields/belongs_to/_index_spec.rb b/spec/administrate/views/fields/belongs_to/_index_spec.rb index ccce07d260..5ea69f7c61 100644 --- a/spec/administrate/views/fields/belongs_to/_index_spec.rb +++ b/spec/administrate/views/fields/belongs_to/_index_spec.rb @@ -32,7 +32,7 @@ context "if associated resource has a show route" do context "and the user has permission to access it" do it "displays link" do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render_belongs_to_index expect(rendered.strip).to include(link) end @@ -40,7 +40,7 @@ context "and the user does not have permission to access it" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_belongs_to_index expect(rendered.strip).to_not include(link) end @@ -49,7 +49,7 @@ context "if associated resource has no show route" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_belongs_to_index expect(rendered.strip).to_not include(link) end diff --git a/spec/administrate/views/fields/belongs_to/_show_spec.rb b/spec/administrate/views/fields/belongs_to/_show_spec.rb index c5aaa4b718..f61a1ae416 100644 --- a/spec/administrate/views/fields/belongs_to/_show_spec.rb +++ b/spec/administrate/views/fields/belongs_to/_show_spec.rb @@ -17,7 +17,7 @@ context "if associated resource has a show route" do context "and the user has permission to access it" do it "displays link" do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render_belongs_to_show expect(rendered.strip).to include(link) end @@ -25,7 +25,7 @@ context "and the user does not have permission to access it" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_belongs_to_show expect(rendered.strip).to_not include(link) end @@ -34,7 +34,7 @@ context "if associated resource has no show route" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_belongs_to_show expect(rendered.strip).to_not include(link) end diff --git a/spec/administrate/views/fields/has_one/_index_spec.rb b/spec/administrate/views/fields/has_one/_index_spec.rb index 4fee947257..6093d745b2 100644 --- a/spec/administrate/views/fields/has_one/_index_spec.rb +++ b/spec/administrate/views/fields/has_one/_index_spec.rb @@ -22,7 +22,7 @@ end it "displays nothing" do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render_has_one_index expect(rendered.strip).to eq("") end @@ -31,7 +31,7 @@ context "if associated resource has a show route" do context "and the user has permission to access it" do it "displays link" do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render_has_one_index expect(rendered.strip).to include(link) end @@ -39,7 +39,7 @@ context "and the user does not have permission to access it" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_has_one_index expect(rendered.strip).to_not include(link) end @@ -48,7 +48,7 @@ context "if associated resource has no show route" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_has_one_index expect(rendered.strip).to_not include(link) end diff --git a/spec/administrate/views/fields/has_one/_show_spec.rb b/spec/administrate/views/fields/has_one/_show_spec.rb index a7fd2b3d02..76b7aeb986 100644 --- a/spec/administrate/views/fields/has_one/_show_spec.rb +++ b/spec/administrate/views/fields/has_one/_show_spec.rb @@ -3,7 +3,7 @@ describe "fields/has_one/_show", type: :view do before do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) end context "without an associated record" do @@ -111,7 +111,7 @@ context "when linking the record is not allowed" do it "displays the record without a link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) product = create(:product) nested_show = instance_double( "Administrate::Page::Show", diff --git a/spec/administrate/views/fields/polymorphic/_index_spec.rb b/spec/administrate/views/fields/polymorphic/_index_spec.rb index 8e1a5f915c..f0c1d72218 100644 --- a/spec/administrate/views/fields/polymorphic/_index_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_index_spec.rb @@ -29,7 +29,7 @@ context "if associated resource has a show route" do context "and the user has permission to access it" do it "displays link" do - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render_polymorphic_index expect(rendered.strip).to include(link) end @@ -37,7 +37,7 @@ context "and the user does not have permission to access it" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_polymorphic_index expect(rendered.strip).to_not include(link) end @@ -46,7 +46,7 @@ context "if associated resource has no show route" do it "hides link" do - allow(view).to receive(:administrate_valid_action?).and_return(false) + allow(view).to receive(:accessible_action?).and_return(false) render_polymorphic_index expect(rendered.strip).to_not include(link) end diff --git a/spec/administrate/views/fields/polymorphic/_show_spec.rb b/spec/administrate/views/fields/polymorphic/_show_spec.rb index 6696192804..9e19f2d480 100644 --- a/spec/administrate/views/fields/polymorphic/_show_spec.rb +++ b/spec/administrate/views/fields/polymorphic/_show_spec.rb @@ -31,7 +31,7 @@ attribute: "product", ) - allow(view).to receive(:administrate_valid_action?).and_return(true) + allow(view).to receive(:accessible_action?).and_return(true) render( partial: "fields/polymorphic/show", diff --git a/spec/example_app/app/views/admin/customers/_index_header.html.erb b/spec/example_app/app/views/admin/customers/_index_header.html.erb index 7301b620ad..5716ec7908 100644 --- a/spec/example_app/app/views/admin/customers/_index_header.html.erb +++ b/spec/example_app/app/views/admin/customers/_index_header.html.erb @@ -28,6 +28,6 @@ ), [:new, namespace, page.resource_path.to_sym], class: "button", - ) if administrate_valid_action?(new_resource, :new) %> + ) if accessible_action?(new_resource, :new) %> diff --git a/spec/helpers/administrate/application_helper_spec.rb b/spec/helpers/administrate/application_helper_spec.rb index 45021a6b65..3162d66f07 100644 --- a/spec/helpers/administrate/application_helper_spec.rb +++ b/spec/helpers/administrate/application_helper_spec.rb @@ -102,14 +102,14 @@ end end - describe "#administrate_valid_action?" do + describe "#accessible_action?" do context "when given a string target" do it "checks the class it names with `existing_action?` and `authorized_action?`" do class MyResource; end ctx = double(existing_action?: true, authorized_action?: true) ctx.extend(Administrate::ApplicationHelper) - ctx.administrate_valid_action?("my_resource", "foo") + ctx.accessible_action?("my_resource", "foo") expect(ctx).to( have_received(:existing_action?).with("foo", :my_resource), @@ -128,7 +128,7 @@ class MyResource; end ctx = double(existing_action?: true, authorized_action?: true) ctx.extend(Administrate::ApplicationHelper) - ctx.administrate_valid_action?(:my_resource, "foo") + ctx.accessible_action?(:my_resource, "foo") expect(ctx).to( have_received(:existing_action?).with("foo", :my_resource), @@ -147,7 +147,7 @@ class MyResource; end ctx = double(existing_action?: true, authorized_action?: true) ctx.extend(Administrate::ApplicationHelper) - ctx.administrate_valid_action?(MyResource, "foo") + ctx.accessible_action?(MyResource, "foo") expect(ctx).to have_received(:existing_action?).with("foo", MyResource) expect(ctx).to( @@ -164,7 +164,7 @@ class MyResource; end ctx.extend(Administrate::ApplicationHelper) object = Product.new - ctx.administrate_valid_action?(object, "foo") + ctx.accessible_action?(object, "foo") expect(ctx).to have_received(:existing_action?).with("foo", Product) expect(ctx).to have_received(:authorized_action?).with("foo", object) @@ -178,7 +178,7 @@ class MyResource; end ctx.extend(Administrate::ApplicationHelper) object = MyResource.new - ctx.administrate_valid_action?(object, "foo") + ctx.accessible_action?(object, "foo") expect(ctx).to have_received(:existing_action?).with("foo", MyResource) expect(ctx).to have_received(:authorized_action?).with("foo", object)