<commit_before>AR = require './active-record'
fs = require 'fs'

# Navigation class contains methods used to navigate on
# rails directory structure

module.exports =
class Navigation

  # Given a model name, returns the file path for that model.
  @modelFilePath: (model) ->
    "app/models/#{model}.rb"

  # Given a model name, returns the file path for the respective controller
  @controllerFilePath: (model) ->
    "app/controllers/#{AR.pluralize(model)}_controller.rb"

  @helperFilePath: (model) ->
    "app/helpers/#{AR.pluralize(model)}_helper.rb"

  # Given a model name, returns the migration that creates it
  # It only works for migrations with the name
  # Returns undefined if not found
  @migrationFilePath: (model) ->
    pluralized_model = AR.pluralize(model)
    files = fs.readdirSync atom.project.getPath() + "/db/migrate"
    for file in files
      if file.match new RegExp("[0-9]+_create_" + pluralized_model + "\.rb")
        return "db/migrate/#{file}"


  # This is the base method used to navigational purposes.
  # It returns the model name from the current file.
  @getModelName: (file) ->
    regexps = [
      /\/models\/(\w+)\.rb$/,
      /\/controllers\/(\w+)_controller\.rb$/,
      /\/views\/(\w+)\/.*rb$/,
      /\/helpers\/(\w+)_helper\.rb$/,
      /\/migrate\/[0-9]+_create_(\w+)\.rb$/,
      /\/migrate\/[0-9]+_add_\w+_to_(\w+)\.rb$/
    ]

    for regexp in regexps
      if match = file.match regexp
        return AR.singularize(match[1])

  # Acordingly to the selected Editor and the file path function passed as
  # parameter, this method opens a new tab.
  @goTo: (fileKind) ->
    if editor = atom.workspace.getActiveEditor()
      modelName = Navigation.getModelName editor.getPath()

      targetFile = switch fileKind
        when "model"
          @modelFilePath(modelName)
        when "controller"
          @controllerFilePath(modelName)
        when "helper"
          @helperFilePath(modelName)
        when "migration"
          @migrationFilePath(modelName)

      atom.workspaceView.open(targetFile)<commit_msg>Move navigation regexps to variables<commit_after>AR = require './active-record'
fs = require 'fs'

# Navigation class contains methods used to navigate on
# rails directory structure

module.exports =
class Navigation

  modelFileMatcher = /\/models\/(\w+)\.rb$/
  controllerFileMatcher = /\/controllers\/(\w+)_controller\.rb$/
  viewFileMatcher = /\/views\/(\w+)\/.*rb$/
  helperFileMatcher = /\/helpers\/(\w+)_helper\.rb$/
  migrationCreateFileMatcher = /\/migrate\/[0-9]+_create_(\w+)\.rb$/
  migrationModifyFilematcher = /\/migrate\/[0-9]+_add_\w+_to_(\w+)\.rb$/

  # Given a model name, returns the file path for that model.
  @modelFilePath: (model) ->
    "app/models/#{model}.rb"

  # Given a model name, returns the file path for the respective controller
  @controllerFilePath: (model) ->
    "app/controllers/#{AR.pluralize(model)}_controller.rb"

  @helperFilePath: (model) ->
    "app/helpers/#{AR.pluralize(model)}_helper.rb"

  # Given a model name, returns the migration that creates it
  # It only works for migrations with the name
  # Returns undefined if not found
  @migrationFilePath: (model) ->
    pluralized_model = AR.pluralize(model)
    files = fs.readdirSync atom.project.getPath() + "/db/migrate"
    for file in files
      if file.match new RegExp("[0-9]+_create_" + pluralized_model + "\.rb")
        return "db/migrate/#{file}"


  # This is the base method used to navigational purposes.
  # It returns the model name from the current file.
  @getModelName: (file) ->
    regexps = [
      modelFileMatcher,
      controllerFileMatcher,
      viewFileMatcher,
      helperFileMatcher,
      migrationCreateFileMatcher,
      migrationModifyFilematcher
    ]

    for regexp in regexps
      if match = file.match regexp
        return AR.singularize(match[1])


  # Acordingly to the selected Editor and the file path function passed as
  # parameter, this method opens a new tab.
  @goTo: (fileKind) ->
    if editor = atom.workspace.getActiveEditor()
      modelName = Navigation.getModelName editor.getPath()

      targetFile = switch fileKind
        when "model"
          @modelFilePath(modelName)
        when "controller"
          @controllerFilePath(modelName)
        when "helper"
          @helperFilePath(modelName)
        when "migration"
          @migrationFilePath(modelName)

      atom.workspaceView.open(targetFile)
{'fraction_non_alphanumeric': 0.12553430821147357, 'fraction_numerical': 0.0031496062992125984, 'mean_word_length': 2.9449866903283053, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1739359', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1346, 'n_words': 365}