Skip to content

Commit

Permalink
Better name for helper
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Apr 28, 2022
1 parent 6b3590e commit 4355d12
Show file tree
Hide file tree
Showing 22 changed files with 40 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/helpers/administrate/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions app/views/administrate/application/_collection.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ to display a collection of resources in an HTML table.
<tbody>
<% resources.each do |resource| %>
<tr class="js-table-row"
<% if administrate_valid_action?(resource, :show) %>
<% if accessible_action?(resource, :show) %>
<%= %(tabindex=0 role=link data-url=#{polymorphic_path([namespace, resource])}) %>
<% end %>
>
<% collection_presenter.attributes_for(resource).each do |attribute| %>
<td class="cell-data cell-data--<%= attribute.html_class %>">
<% if administrate_valid_action?(resource, :show) -%>
<% if accessible_action?(resource, :show) -%>
<a href="<%= polymorphic_path([namespace, resource]) -%>"
tabindex="-1"
class="action-show"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
t("administrate.actions.edit"),
[:edit, namespace, resource],
class: "action-edit",
) if administrate_valid_action?(resource, :edit) %></td>
) if accessible_action?(resource, :edit) %></td>
<% end %>

<% if existing_action?(:destroy, collection_presenter.resource_name) %>
Expand All @@ -13,5 +13,5 @@
class: "text-color-red",
method: :delete,
data: { confirm: t("administrate.actions.confirm") }
) if administrate_valid_action?(resource, :destroy) %></td>
) if accessible_action?(resource, :destroy) %></td>
<% end %>
2 changes: 1 addition & 1 deletion app/views/administrate/application/_index_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
</div>
</header>
2 changes: 1 addition & 1 deletion app/views/administrate/application/_navigation.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</nav>
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 @@ -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) %>
</div>
</header>

Expand Down
4 changes: 2 additions & 2 deletions app/views/administrate/application/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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"),
[namespace, page.resource],
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) %>
</div>
</header>

Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/belongs_to/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/belongs_to/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/has_one/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/has_one/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ All show page attributes of has_one relationship would be rendered
<fieldset class="attribute--nested">
<legend>
<%= link_to_if(
administrate_valid_action?(field.data, :show),
accessible_action?(field.data, :show),
field.display_associated_resource,
[namespace, field.data],
) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/polymorphic/_index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/polymorphic/_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/administrate/views/fields/belongs_to/_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
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
end

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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions spec/administrate/views/fields/belongs_to/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
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
end

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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/administrate/views/fields/has_one/_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,15 +31,15 @@
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
end

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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/administrate/views/fields/has_one/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions spec/administrate/views/fields/polymorphic/_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@
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
end

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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/administrate/views/fields/polymorphic/_show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>
</div>
</header>
12 changes: 6 additions & 6 deletions spec/helpers/administrate/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand All @@ -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(
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 4355d12

Please sign in to comment.