Skip to content

Commit

Permalink
fixed (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Aug 2, 2017
1 parent b213f3a commit e5357fe
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 27 deletions.
37 changes: 33 additions & 4 deletions lib/drawers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@
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'
require 'drawers/resource_parts'

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
Expand All @@ -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
17 changes: 0 additions & 17 deletions lib/drawers/active_support/dependency_extensions.rb
Original file line number Diff line number Diff line change
@@ -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/, '')
Expand Down
9 changes: 4 additions & 5 deletions lib/drawers/resource_parts.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/drawers/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module Drawers
VERSION = '1.0.1'
VERSION = '1.1.0'
end
23 changes: 23 additions & 0 deletions spec/integration/auto_loading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions spec/support/rails_app/app/resources/comments/thing.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true
class CommentThing < ActiveModel::Serializer
end

0 comments on commit e5357fe

Please sign in to comment.