Class: RailsCodeHealth::ProjectDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_code_health/project_detector.rb

Constant Summary collapse

RAILS_INDICATORS =
[
  'config/application.rb',
  'config/environment.rb',
  'Gemfile'
].freeze
RAILS_DIRECTORIES =
[
  'app/controllers',
  'app/models',
  'app/views',
  'config'
].freeze

Class Method Summary collapse

Class Method Details

.detect_detailed_file_type(file_path, project_root) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rails_code_health/project_detector.rb', line 70

def detect_detailed_file_type(file_path, project_root)
  relative_path = file_path.relative_path_from(project_root).to_s
  file_type = detect_file_type(file_path, project_root)
  
  context = {
    organization: detect_organization_pattern(relative_path),
    domain: extract_domain(relative_path),
    area: extract_area(relative_path),
    api_version: extract_api_version(relative_path)
  }.compact

  { type: file_type, context: context }
end

.detect_file_type(file_path, project_root) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rails_code_health/project_detector.rb', line 23

def detect_file_type(file_path, project_root)
  relative_path = file_path.relative_path_from(project_root).to_s

  case relative_path
  when %r{^app/.*/controllers/.*_controller\.rb$}, %r{^app/controllers/.*_controller\.rb$}
    :controller
  when %r{^app/models/.*\.rb$}
    :model
  when %r{^app/views/.*\.(erb|haml|slim)$}
    :view
  when %r{^app/helpers/.*_helper\.rb$}
    :helper
  when %r{^app/.*/services/.*\.rb$}, %r{^app/services/.*\.rb$}
    :service
  when %r{^app/.*/interactors/.*\.rb$}, %r{^app/interactors/.*\.rb$}
    :interactor
  when %r{^app/.*/serializers/.*\.rb$}, %r{^app/serializers/.*\.rb$}
    :serializer
  when %r{^app/.*/forms/.*\.rb$}, %r{^app/forms/.*\.rb$}
    :form
  when %r{^app/.*/decorators/.*\.rb$}, %r{^app/decorators/.*\.rb$}
    :decorator
  when %r{^app/.*/presenters/.*\.rb$}, %r{^app/presenters/.*\.rb$}
    :presenter
  when %r{^app/.*/policies/.*\.rb$}, %r{^app/policies/.*\.rb$}
    :policy
  when %r{^app/.*/jobs/.*\.rb$}, %r{^app/jobs/.*\.rb$}
    :job
  when %r{^app/.*/workers/.*\.rb$}, %r{^app/workers/.*\.rb$}
    :worker
  when %r{^app/.*/mailers/.*\.rb$}, %r{^app/mailers/.*\.rb$}
    :mailer
  when %r{^app/.*/channels/.*\.rb$}, %r{^app/channels/.*\.rb$}
    :channel
  when %r{^lib/.*\.rb$}
    :lib
  when %r{^db/migrate/.*\.rb$}
    :migration
  when %r{^spec/.*_spec\.rb$}, %r{^test/.*_test\.rb$}
    :test
  when %r{^config/.*\.rb$}
    :config
  else
    :ruby if file_path.extname == '.rb'
  end
end

.rails_project?(path) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
# File 'lib/rails_code_health/project_detector.rb', line 17

def rails_project?(path)
  path = Pathname.new(path) unless path.is_a?(Pathname)
  
  has_rails_files?(path) && has_rails_structure?(path) && has_rails_gemfile?(path)
end