require 'test_helper' module AdminModules module Engine end class SmartThing < ExpressTemplates::Components::Configurable include ExpressTemplates::Components::Capabilities::Resourceful attr_accessor :virtual_path, :config def initialize(virtual_path, config = {}) @virtual_path = virtual_path @config = config @args = [self] end def template_virtual_path @virtual_path end end end module Admin; end class FooBar; end class Something; end module ExpressTemplates class ResourcefulTest < ActiveSupport::TestCase test 'infers namespace and path prefix within an engine and scope' do smart_thing = AdminModules::SmartThing.new('admin_modules/admin/something/index') assert_equal 'admin_modules', smart_thing.namespace assert_equal 'admin', smart_thing.path_prefix end test 'infers a namespace and no prefix within an engine' do # if defined? ExpressFoo::Engine smart_thing = AdminModules::SmartThing.new('admin_modules/something/index') assert_equal 'admin_modules', smart_thing.namespace assert_equal nil, smart_thing.path_prefix end test 'no namespace, infers prefix within a scope within an app' do # else of case above smart_thing = AdminModules::SmartThing.new('admin/something/index') assert_equal nil, smart_thing.namespace assert_equal 'admin', smart_thing.path_prefix end test 'no namespace, no prefix within an app' do smart_thing = AdminModules::SmartThing.new('somethings/index') assert_equal nil, smart_thing.namespace assert_equal nil, smart_thing.path_prefix end test "#resource_class returns resource_class option if specified" do assert_equal FooBar, AdminModules::SmartThing.new('somethings/index', resource_class: 'FooBar').resource_class assert_equal Something, AdminModules::SmartThing.new('somethings/index', id: :something).resource_class end test "#resource_class raises a helpful error message when the class does not exist" do expected_error_message = [ "Could not find the class `FooBar`.", "You may need to define the option `:resource_class`.", ].join(" ") smart_thing = AdminModules::SmartThing. new('somethings/index', resource_class: 'NonExistentBar') assert_raises(ArgumentError, expected_error_message) do smart_thing.resource_class end end end end