Skip to content

Instantly share code, notes, and snippets.

@jdickey
Last active August 29, 2015 14:17
Show Gist options
  • Save jdickey/3fe83f05b74bbb931b10 to your computer and use it in GitHub Desktop.
Save jdickey/3fe83f05b74bbb931b10 to your computer and use it in GitHub Desktop.
Simply subtle namespacing clusterfox FIXED

There once was a project that made extensive use of Pivotal's "Unbuilt Rails Dependency" pseudo-Gem architectural feature. After realising that that was far more limited in its applicable use cases than hoped, efforts were made to claw those back into namespaced classes within the main Rails app.

This was generally successful for a while. The use-case actions/service layer and one of the two core entities, Users, worked Just Fine. When work began on the Post entity, however, increasing forward progress was abruptly halted by a weird error.

Given the files spec/entities/post_spec.rb and app/entities/post.rb as below, running bundle exec rspec spec/entities/post_spec.rb gives the following output

Coverage report generated for RSpec to /Users/jeffdickey/src/rails/meldd/new_poc/coverage. 377 / 707 LOC (53.32%) covered.
Coverage = 53.32%. Sending report to https://codeclimate.com for branch post-entities... done.
/Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:495:in `load_missing_constant': Unable to autoload constant Post, expected /Users/jeffdickey/src/rails/meldd/new_poc/app/entities/post.rb to define it (LoadError)
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:526:in `load_missing_constant'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:184:in `const_missing'
	from /Users/jeffdickey/src/rails/meldd/new_poc/spec/entities/post_spec.rb:5:in `<module:Entity>'
	from /Users/jeffdickey/src/rails/meldd/new_poc/spec/entities/post_spec.rb:4:in `<top (required)>'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `load'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1226:in `block in load_spec_files'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `each'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/configuration.rb:1224:in `load_spec_files'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:97:in `setup'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:85:in `run'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:70:in `run'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/lib/rspec/core/runner.rb:38:in `invoke'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/gems/rspec-core-3.2.2/exe/rspec:4:in `<top (required)>'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/bin/rspec:23:in `load'
	from /Users/jeffdickey/src/rails/meldd/new_poc/vendor/ruby/2.2.0/bin/rspec:23:in `<main>'

This Should Be an Evolution, Not a Revolution (Or a Devolution)

Bear in mind that Post is the second entity to be given this treatment. There is a corresponding Entity::User entity that Works Just Fine, including running its top-level RSpec specs as bundle exec rspec spec/entities/user_spec.rb.

FIXED

As i describe in a comment below. Major thanks to @bradstewart for his patient prodding that finally "[knocked] my mental rust loose".

# app/entities/post.rb
module Entity
class Post
def initialize(attributes)
end
end # class Entity::Post
end
# spec/entities/post_spec.rb
require 'spec_helper'
module Entity
describe Post do
end # describe Entity::Post
end
@bradstewart
Copy link

So I got busy and forgot about this, any progress?

As far as I know, require goes looking in your system's Ruby load paths for a dependency with the specified name. Rails adds project-level files to your "system" paths in order to require the project's files, and it does so according to its own autoloading rules, not necessarily file paths. So I'll bet you can require 'post'and it will load just fine.

@jdickey
Copy link
Author

jdickey commented Mar 30, 2015

@bradstewart That does, indeed, seem to work. I've been off on an epic quest for the last week-plus and just happened to notice I had this tab open as I was about to shut down (at 02.30 on a long Monday night/Tuesday morning).

I was entirely too fixated on requiring entities/post when a simple require 'post' does indeed work Just Fine. Thanks for knocking my mental rust loose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment