Sha256: c832a22bbf8e698a7b394daed922e9ebbcbc37a830393c2b760ad6b35b8dcbf6

Contents?: true

Size: 1.79 KB

Versions: 37

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

module Rails
  module Generators
    module AppName # :nodoc:
      RESERVED_NAMES = %w(application destroy plugin runner test)

      private
        def app_name
          @app_name ||= original_app_name.tr("\\", "").tr("-. ", "_")
        end

        def original_app_name
          @original_app_name ||= defined_app_const_base? ? defined_app_name : File.basename(destination_root)
        end

        def defined_app_name
          defined_app_const_base.underscore
        end

        def defined_app_const_base
          Rails.respond_to?(:application) && defined?(Rails::Application) &&
            Rails.application.is_a?(Rails::Application) && Rails.application.class.name.chomp("::Application")
        end

        alias :defined_app_const_base? :defined_app_const_base

        def app_const_base
          @app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, "_").squeeze("_").camelize
        end
        alias :camelized :app_const_base

        def app_const
          @app_const ||= "#{app_const_base}::Application"
        end

        def valid_const?
          if /^\d/.match?(app_const)
            raise Error, "Invalid application name #{original_app_name}. Please give a name which does not start with numbers."
          elsif RESERVED_NAMES.include?(original_app_name)
            raise Error, "Invalid application name #{original_app_name}. Please give a " \
                         "name which does not match one of the reserved rails " \
                         "words: #{RESERVED_NAMES.join(", ")}"
          elsif Object.const_defined?(app_const_base)
            raise Error, "Invalid application name #{original_app_name}, constant #{app_const_base} is already in use. Please choose another application name."
          end
        end
    end
  end
end

Version data entries

37 entries across 35 versions & 4 rubygems

Version Path
railties-7.0.8.6 lib/rails/generators/app_name.rb
railties-7.0.8.5 lib/rails/generators/app_name.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/railties-7.0.8.4/lib/rails/generators/app_name.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/app_name.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/app_name.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/railties-7.0.5.1/lib/rails/generators/app_name.rb
railties-7.0.8.4 lib/rails/generators/app_name.rb
railties-7.0.8.1 lib/rails/generators/app_name.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.3.1/lib/rails/generators/app_name.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/railties-7.0.2.3/lib/rails/generators/app_name.rb
railties-7.0.8 lib/rails/generators/app_name.rb
railties-7.0.7.2 lib/rails/generators/app_name.rb
railties-7.0.7.1 lib/rails/generators/app_name.rb
railties-7.0.7 lib/rails/generators/app_name.rb
railties-7.0.6 lib/rails/generators/app_name.rb
railties-7.0.5.1 lib/rails/generators/app_name.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/railties-7.0.2.3/lib/rails/generators/app_name.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/railties-7.0.3.1/lib/rails/generators/app_name.rb
railties-7.0.5 lib/rails/generators/app_name.rb
railties-7.0.4.3 lib/rails/generators/app_name.rb