diff --git a/lib/drawers.rb b/lib/drawers.rb index c389960..a5dcd77 100644 --- a/lib/drawers.rb +++ b/lib/drawers.rb @@ -3,6 +3,17 @@ require 'active_support' module Drawers + DEFAULT_RESOURCE_SUFFIXES = %w( + Controller + Forms + Serializer + Operations + Presenters + Policy + Policies + Services + ).freeze + require 'drawers/active_support/dependency_extensions' require 'drawers/action_view/path_extensions' require 'drawers/action_view/resource_resolver' @@ -10,6 +21,14 @@ module Drawers module_function + def resource_suffixes + @resource_suffixes || DEFAULT_RESOURCE_SUFFIXES + end + + def resource_suffixes=(suffixes) + @resource_suffixes = suffixes.freeze + end + def directory=(dir) @directory = dir end @@ -18,11 +37,21 @@ def directory @directory || '' end + # @api private + # Join all the suffix names together with an "OR" operator + def resource_suffixes_regex + /(#{resource_suffixes.join('|')})/ + end + + # @api private + # split on any of the resource suffixes OR the ruby namespace seperator + def qualified_name_split + /::|#{resource_suffixes_regex}/ + end + require 'drawers/railtie' + ActiveSupport::Dependencies.extend Drawers::DependencyExtensions ActionController::Base.extend Drawers::PathExtensions - - if Rails.version > '5' - ActionController::API.extend Drawers::PathExtensions - end + ActionController::API.extend Drawers::PathExtensions if Rails.version > '5' end diff --git a/lib/drawers/active_support/dependency_extensions.rb b/lib/drawers/active_support/dependency_extensions.rb index 8f86b1f..99b696c 100644 --- a/lib/drawers/active_support/dependency_extensions.rb +++ b/lib/drawers/active_support/dependency_extensions.rb @@ -1,25 +1,8 @@ # frozen_string_literal: true module Drawers module DependencyExtensions - RESOURCE_SUFFIX_NAMES = %w( - Controller - Forms - Serializer - Operations - Presenters - Policy - Policies - Services - ).freeze - ERROR_CIRCULAR_DEPENDENCY = 'Circular dependency detected while autoloading constant' - # Join all the suffix names together with an "OR" operator - RESOURCE_SUFFIXES = /(#{RESOURCE_SUFFIX_NAMES.join('|')})/ - - # split on any of the resource suffixes OR the ruby namespace seperator - QUALIFIED_NAME_SPLIT = /::|#{RESOURCE_SUFFIXES}/ - def load_from_path(file_path, qualified_name, from_mod, const_name) expanded = File.expand_path(file_path) expanded.sub!(/\.rb\z/, '') diff --git a/lib/drawers/resource_parts.rb b/lib/drawers/resource_parts.rb index 2a9fe41..5be3803 100644 --- a/lib/drawers/resource_parts.rb +++ b/lib/drawers/resource_parts.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true module Drawers class ResourceParts - RESOURCE_SUFFIX_NAMES = Drawers::DependencyExtensions::RESOURCE_SUFFIX_NAMES - QUALIFIED_NAME_SPLIT = Drawers::DependencyExtensions::QUALIFIED_NAME_SPLIT - attr_reader :namespace, :resource_name, :resource_type, :named_resource_type, :class_path @@ -80,7 +77,9 @@ def call # Api::V2::PostOperations::Update # => Api, V2, Post, Operations, Update def qualified_parts - @qualified_parts ||= @qualified_name.split(QUALIFIED_NAME_SPLIT).reject(&:blank?) + @qualified_parts ||= @qualified_name + .split(Drawers.qualified_name_split) + .reject(&:blank?) end # based on the position of of the resource type name, @@ -91,7 +90,7 @@ def qualified_parts # Given: Api, V2, Post, Operations, Update # ^ index_of_resource_type (3) def index_of_resource_type - @index_of_resource_type ||= qualified_parts.index { |x| RESOURCE_SUFFIX_NAMES.include?(x) } + @index_of_resource_type ||= qualified_parts.index { |x| Drawers.resource_suffixes.include?(x) } end end end diff --git a/lib/drawers/version.rb b/lib/drawers/version.rb index b4b1eb5..0b21ee5 100644 --- a/lib/drawers/version.rb +++ b/lib/drawers/version.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true module Drawers - VERSION = '1.0.1' + VERSION = '1.1.0' end diff --git a/spec/integration/auto_loading_spec.rb b/spec/integration/auto_loading_spec.rb index 0eda395..1b5bc35 100644 --- a/spec/integration/auto_loading_spec.rb +++ b/spec/integration/auto_loading_spec.rb @@ -94,4 +94,27 @@ expect { Author.create.posts }.to_not raise_error end end + + context 'with a non-default-ily named file' do + context 'is configured to find non-default file' do + before(:each) do + Drawers.resource_suffixes = ['Thing'] + end + + after(:each) do + Drawers.resource_suffixes = Drawers::DEFAULT_RESOURCE_SUFFIXES + Object.send(:remove_const, :CommentThing) if defined? CommentThing + end + + it 'finds the file' do + expect { CommentThing }.to_not raise_error + end + end + + context 'is not configured to find non-default file' do + it 'errors' do + expect { CommentThing }.to raise_error(NameError, /uninitialized constant CommentThing/) + end + end + end end diff --git a/spec/support/rails_app/app/resources/comments/thing.rb b/spec/support/rails_app/app/resources/comments/thing.rb new file mode 100644 index 0000000..781148f --- /dev/null +++ b/spec/support/rails_app/app/resources/comments/thing.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true +class CommentThing < ActiveModel::Serializer +end