Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider not every models subdirectory to be a class namespace #99

Merged
merged 1 commit into from
Oct 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions lib/railroady/models_diagram.rb
Expand Up @@ -39,9 +39,31 @@ def get_files(prefix = '')
def engine_files
engines.collect { |engine| Dir.glob("#{engine.root}/app/models/**/*.rb") }.flatten
end

def extract_class_name(filename)
filename.match(/.*\/models\/(.*).rb$/)[1].camelize
filename_was, class_name = filename, nil

filename = "app/models/#{filename.split('app/models')[1]}"

while filename.split('/').length > 2
begin
class_name = filename.match(/.*\/models\/(.*).rb$/)[1].camelize
class_name.constantize

break
rescue Exception => e
class_name = nil
filename_end = filename.split('/')[2..-1]
filename_end.shift
filename = "#{filename.split('/')[0, 2].join('/')}/#{filename_end.join('/')}"
end
end

if class_name.nil?
filename_was.match(/.*\/models\/(.*).rb$/)[1].camelize
else
class_name
end
end

# Process a model class
Expand Down
12 changes: 12 additions & 0 deletions test/file_fixture/app/models/concerns/author_settings.rb
@@ -0,0 +1,12 @@
module AuthorSettings
extend ActiveSupport::Concern

included do
before_validation :set_up_author
end

def set_up_author
self.created_by = 'Admin'
self.updated_by = 'Admin'
end
end
2 changes: 1 addition & 1 deletion test/lib/railroady/aasm_diagram_spec.rb
Expand Up @@ -12,7 +12,7 @@
options = OptionsStruct.new(include_concerns: true)
ad = AasmDiagram.new(options)
files = ad.get_files('test/file_fixture/')
files.size.must_equal 4
files.size.must_equal 5
end

it 'should exclude a specific file' do
Expand Down
38 changes: 37 additions & 1 deletion test/lib/railroady/models_diagram_spec.rb
Expand Up @@ -12,7 +12,7 @@
options = OptionsStruct.new(include_concerns: true)
ad = ModelsDiagram.new(options)
files = ad.get_files('test/file_fixture/')
files.size.must_equal 4
files.size.must_equal 5
end

it 'should exclude a specific file' do
Expand Down Expand Up @@ -60,4 +60,40 @@
end
end
end

describe '#extract_class_name' do
describe 'class can be found' do
describe 'module without namespace' do
module AuthorSettings
end

it 'does not take every models subdirectory as a namespace' do
md = ModelsDiagram.new(OptionsStruct.new)

md.extract_class_name('test/file_fixture/app/models/concerns/author_settings.rb').must_equal 'AuthorSettings'
end
end

describe 'module with parent namespace / class' do
class User
module Authentication
end
end

it 'does not take every models subdirectory as a namespace' do
md = ModelsDiagram.new(OptionsStruct.new)

md.extract_class_name('test/file_fixture/app/models/concerns/user/authentication.rb').must_equal 'User::Authentication'
end
end
end

describe 'class cannot be found' do
it 'returns the full class name' do
md = ModelsDiagram.new(OptionsStruct.new)

md.extract_class_name('test/file_fixture/app/models/concerns/dummy.rb').must_equal 'Concerns::Dummy'
end
end
end
end