<commit_before>module TlEmberEdit
  class EditController < ApplicationController
    def view
      # Change from CamelCase to under_score
      cls = params[:cls].underscore

      # Remove the app namespace
      cls.sub!(/^.+?\./, '')

      @filename = "#{Rails.root}/#{EMBER_ROOT}/#{VIEWS_DIR}/#{cls}"

      try_extensions(['js', 'js.coffee', 'coffee'])

      system(EDIT_CMD.sub('FILENAME', @filename))

      head :ok
    end


    def template
      has_ember_rails = Module.const_defined?('Ember') && Ember.const_defined?('Rails')

      if has_ember_rails
        ember_config = Ember::Rails::Engine.config.handlebars
        templates_root = ember_config.templates_root || ''
        templates_path_separator = ember_config.templates_path_separator || '/'
      else
        templates_root = ''
        templates_path_separator = '/'
      end

      templates_root = templates_root.present? ? templates_root + '/' : ''
      template_path = templates_root + params[:tpl].gsub(templates_path_separator, '/')

      @filename = "#{Rails.root}/#{EMBER_ROOT}/#{params[:tpl]}"

      try_extensions(['handlebars', 'js.hjs', 'hbs'])

      system(EDIT_CMD.sub('FILENAME', @filename))

      head :ok
    end

    ########
    private
    ########

    def try_extensions(extensions)
      extensions.each do |extension|
        candidate = @filename + '.' + extension
        if File.exists?(candidate)
          @filename = candidate
          break
        end
      end
    end
  end
end<commit_msg>Refactoring<commit_after>module TlEmberEdit
  class EditController < ApplicationController
    def view
      # Change from CamelCase to under_score
      cls = params[:cls].underscore

      # Remove the app namespace
      cls.sub!(/^.+?\./, '')

      @filename = "#{Rails.root}/#{EMBER_ROOT}/#{VIEWS_DIR}/#{cls}"

      try_extensions(['js', 'js.coffee', 'coffee'])

      start_editor_and_render
    end


    def template
      has_ember_rails = Module.const_defined?('Ember') && Ember.const_defined?('Rails')

      if has_ember_rails
        ember_config = Ember::Rails::Engine.config.handlebars
        templates_root = ember_config.templates_root || ''
        templates_path_separator = ember_config.templates_path_separator || '/'
      else
        templates_root = ''
        templates_path_separator = '/'
      end

      templates_root = templates_root.present? ? templates_root + '/' : ''
      template_path = templates_root + params[:tpl].gsub(templates_path_separator, '/')

      @filename = "#{Rails.root}/#{EMBER_ROOT}/#{params[:tpl]}"

      try_extensions(['handlebars', 'js.hjs', 'hbs'])

      start_editor_and_render

      head :ok
    end

    ########
    private
    ########

    def try_extensions(extensions)
      extensions.each do |extension|
        candidate = @filename + '.' + extension
        if File.exists?(candidate)
          @filename = candidate
          return
        end
      end
      @filename = nil
    end

    def start_editor
      system(EDIT_CMD.sub('FILENAME', @filename))
    end

    def start_editor_and_render
      if @filename
        start_editor
        head :ok
      else
        render json: {msg: 'File not found'}, status: :internal_server_error
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.1297148114075437, 'fraction_numerical': 0.0, 'mean_word_length': 2.612403100775194, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '4816243', 'n_tokens_mistral': 1020, 'n_tokens_neox': 973, 'n_words': 182}