Skip to content

Commit

Permalink
Allow administering gem typo exceptions thru admin interface
Browse files Browse the repository at this point in the history
Removes another support action that requires using the console
  • Loading branch information
segiddins committed Oct 13, 2023
1 parent 38be873 commit 94f5911
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/avo/resources/gem_typo_exception_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class GemTypoExceptionResource < Avo::BaseResource
self.title = :name
self.includes = []
self.search_query = lambda {
scope.where("name ILIKE ?", "%#{params[:q]}%")
}

field :id, as: :id, hide_on: :index
# Fields generated from the model
field :name, as: :text, link_to_resource: true
field :info, as: :textarea
# add fields here
field :created_at, as: :date_time, sortable: true
field :updated_at, as: :date_time, sortable: true
end
4 changes: 4 additions & 0 deletions app/controllers/avo/gem_typo_exceptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This controller has been generated to enable Rails' resource routes.
# More information on https://docs.avohq.io/2.0/controllers.html
class Avo::GemTypoExceptionsController < Avo::ResourcesController
end
20 changes: 20 additions & 0 deletions app/policies/gem_typo_exception_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class GemTypoExceptionPolicy < ApplicationPolicy
class Scope < Scope
def resolve
scope.all
end
end

def avo_index?
rubygems_org_admin?
end

def avo_show?
rubygems_org_admin?
end

def avo_create? = rubygems_org_admin?
def avo_update? = rubygems_org_admin?
def avo_delete? = rubygems_org_admin?
def act_on? = rubygems_org_admin?
end
42 changes: 42 additions & 0 deletions test/policies/gem_typo_exception_policy_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "test_helper"

class GemTypoExceptionPolicyTest < ActiveSupport::TestCase
setup do
@exception = create(:gem_typo_exception)

@admin = create(:admin_github_user, :is_admin)
@non_admin = create(:admin_github_user)
end

def test_scope
assert_equal [@exception], Pundit.policy_scope!(
@admin,
GemTypoException
).to_a
end

def test_avo_index
assert_predicate Pundit.policy!(@admin, GemTypoException), :avo_index?
refute_predicate Pundit.policy!(@non_admin, GemTypoException), :avo_index?
end

def test_avo_show
assert_predicate Pundit.policy!(@admin, @exception), :avo_show?
refute_predicate Pundit.policy!(@non_admin, @exception), :avo_show?
end

def test_avo_create
assert_predicate Pundit.policy!(@admin, GemTypoException), :avo_create?
refute_predicate Pundit.policy!(@non_admin, GemTypoException), :avo_create?
end

def test_avo_update
assert_predicate Pundit.policy!(@admin, @exception), :avo_update?
refute_predicate Pundit.policy!(@non_admin, @exception), :avo_update?
end

def test_avo_destroy
assert_predicate Pundit.policy!(@admin, @exception), :avo_destroy?
refute_predicate Pundit.policy!(@non_admin, @exception), :avo_destroy?
end
end

0 comments on commit 94f5911

Please sign in to comment.