Skip to content

Commit

Permalink
oops
Browse files Browse the repository at this point in the history
  • Loading branch information
NullVoxPopuli committed Sep 18, 2016
1 parent df9c6c6 commit 69e37d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 952 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Gemfile.lock
capybara-*.html
.rspec
spec/support/rails_app/log
/log
/tmp
/db/*.sqlite3
Expand Down
37 changes: 22 additions & 15 deletions lib/rails_module_unification/active_support_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true
module RailsModuleUnification
module ActiveSupportExtensions
RESOURCE_SUFFIXES = /(Controller|Serializer|Operation|Policy)/

def load_from_path(file_path, qualified_name, from_mod, const_name)
expanded = File.expand_path(file_path)
expanded.sub!(/\.rb\z/, '')
Expand Down Expand Up @@ -50,20 +52,7 @@ def load_from_parent(from_mod, const_name)
raise unless e.missing_name? qualified_name_for(parent, const_name)
end

# Load the constant named +const_name+ which is missing from +from_mod+. If
# it is not possible to load the constant into from_mod, try its parent
# module using +const_missing+.
def load_missing_constant(from_mod, const_name)
# always default to the actual implementation
super
rescue LoadError, NameError
suffixes = /(Controller|Serializer)\z/

# examples
# - Api::PostsController
# - PostsController
qualified_name = qualified_name_for from_mod, const_name

def resource_path_from_qualified_name(qualified_name)
# examples
# - api/posts_controller
# - posts_controller
Expand All @@ -78,7 +67,7 @@ def load_missing_constant(from_mod, const_name)
# examples:
# - api/posts
# - posts
folder_name = qualified_name.split(suffixes).first.underscore.pluralize
folder_name = qualified_name.split(RESOURCE_SUFFIXES).first.underscore.pluralize

# examples:
# - posts/posts_controller
Expand All @@ -95,6 +84,24 @@ def load_missing_constant(from_mod, const_name)
# the resource_name/resource_names_controller.rb naming scheme
file_path ||= search_for_file(folder_named_type)

file_path
end

# Load the constant named +const_name+ which is missing from +from_mod+. If
# it is not possible to load the constant into from_mod, try its parent
# module using +const_missing+.
def load_missing_constant(from_mod, const_name)
# always default to the actual implementation
super
rescue LoadError, NameError

# examples
# - Api::PostsController
# - PostsController
qualified_name = qualified_name_for from_mod, const_name

file_path = resource_path_from_qualified_name(qualified_name)

return load_from_path(file_path, qualified_name, from_mod, const_name) if file_path

# TODO: what is the situation in which this is needed?
Expand Down
2 changes: 1 addition & 1 deletion lib/rails_module_unification/version.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# frozen_string_literal: true
module RailsModuleUnification
VERSION = '0.5.1'.freeze
VERSION = '0.5.3'.freeze
end
Loading

0 comments on commit 69e37d7

Please sign in to comment.