Skip to content

Commit

Permalink
refactor: BulletTrain::LoadsAndAuthorizesResource.model_namespace_fro…
Browse files Browse the repository at this point in the history
…m_controller_namespace and add a test
  • Loading branch information
aaricpittman committed Sep 1, 2023
1 parent bbbd8a6 commit 57c3821
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ module BulletTrain::LoadsAndAuthorizesResource
extend ActiveSupport::Concern

class_methods do
# Returns an array of module names based on the classes namespace minus regex_to_remove_controller_namespace
def model_namespace_from_controller_namespace
controller_class_name =
if regex_to_remove_controller_namespace
name.gsub(regex_to_remove_controller_namespace, "")
else
name
end
namespace = controller_class_name.split("::")
# Remove "::ThingsController"
namespace.pop
namespace
name
.gsub(regex_to_remove_controller_namespace || //, "")
.split("::")
.slice(0...-1) # drops actual class name
end

def regex_to_remove_controller_namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
class BulletTrain::LoadsAndAuthorizeResourceTest < ActiveSupport::TestCase
class TestClass
include BulletTrain::LoadsAndAuthorizesResource

def self.regex_to_remove_controller_namespace
end
end

module Users
class TestClass
include BulletTrain::LoadsAndAuthorizesResource

def self.regex_to_remove_controller_namespace
/^BulletTrain::LoadsAndAuthorizeResourceTest::/
end
end
end

test "it defines .model_namespace_from_controller_namespace" do
assert TestClass.respond_to?(:model_namespace_from_controller_namespace)
test "model_namespace_from_controller_namespace returns an array of modules names based on the classes namespace minus regex_to_remove_controller_namespace" do
assert_equal ["BulletTrain", "LoadsAndAuthorizeResourceTest"], TestClass.model_namespace_from_controller_namespace
assert_equal ["Users"], Users::TestClass.model_namespace_from_controller_namespace
end

test "it defines .account_load_and_authorize_resource" do
Expand Down

0 comments on commit 57c3821

Please sign in to comment.