Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
a little refactoring of cached model name constants, better handling …
…of namespaces
- Loading branch information
1 parent
59938ae
commit 9c0e9cd
Showing
4 changed files
with
116 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Module handling from config: :module_handling option' do | ||
let(:clazz) do | ||
Class.new do | ||
include Neo4j::ActiveNode | ||
end | ||
end | ||
|
||
before { stub_const 'ModuleTest::Student', clazz } | ||
after do | ||
Neo4j::Config[:association_model_namespace] = nil | ||
Neo4j::Config[:module_handling] = nil | ||
end | ||
|
||
describe 'labels' do | ||
context 'with config unspecified or neither :demodulize nor a proc' do | ||
it 'are set using the full module and class name' do | ||
expect(ModuleTest::Student.mapped_label_name).to eq :'ModuleTest::Student' | ||
end | ||
end | ||
|
||
context 'with config set to :demodulize' do | ||
before { Neo4j::Config[:module_handling] = :demodulize } | ||
|
||
it 'strips module names from labels' do | ||
expect(ModuleTest::Student.mapped_label_name).to eq :Student | ||
end | ||
end | ||
|
||
context 'with a proc' do | ||
before do | ||
Neo4j::Config[:module_handling] = proc do |name| | ||
module_name = name.deconstantize | ||
name.gsub(module_name, 'Foo') | ||
end | ||
end | ||
|
||
it 'lets you modify the name as you see fit' do | ||
expect(ModuleTest::Student.mapped_label_name).to eq :'Foo::Student' | ||
end | ||
end | ||
end | ||
|
||
describe 'association model locations' do | ||
let(:discovered_model) { clazz.associations[:students].instance_variable_get(:@target_class_option) } | ||
|
||
context 'with config set to :none or unspecified' do | ||
before { clazz.has_many :out, :students } | ||
|
||
it 'expects a class with the singular version of the association' do | ||
expect(discovered_model).to eq '::Student' | ||
end | ||
end | ||
|
||
context ' with :association_model_namespace set' do | ||
before do | ||
Neo4j::Config[:association_model_namespace] = 'ModuleTest' | ||
clazz.has_many :out, :students | ||
end | ||
|
||
it 'expects namespacing and looks for a model in the same namespace as the source' do | ||
expect(discovered_model).to eq '::ModuleTest::Student' | ||
node1 = ModuleTest::Student.create | ||
node2 = ModuleTest::Student.create | ||
node1.students << node2 | ||
expect(node1.students.to_a).to include node2 | ||
end | ||
end | ||
end | ||
end |