class RorGenerators::Generators::NewGenerator

Public Instance Methods

add_file_to_bundle() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 55
def add_file_to_bundle
  if dest_file_exists?("client/webpack.config.js")
    if not_in_bundle?
      puts "not in bundle"
      inject_into_file "client/webpack.config.js", after: "'babel-polyfill',\n" do
        "\t\t\t'./app/bundles/#{module_name}/startup/registration',\n"
      end
    end
  end
end
add_route() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 34
def add_route 
  route "resources :#{module_name.downcase.pluralize}, only: :index"
end
client_dest_prefix() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 12
def client_dest_prefix 
  "client/app/bundles/#{module_name}/"
end
copy_config_files() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 45
def copy_config_files 
  template "client/startup/registration.jsx.erb", "#{client_dest_prefix}/startup/registration.jsx"
end
copy_package_json() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 66
def copy_package_json 
  template "client/package.json.tt", "client/package.json" unless dest_file_exists?("client/package.json")
end
copy_server_files() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 38
def copy_server_files
  empty_directory("app/views/#{module_name}")
  template "server/controller.rb", "app/controllers/#{module_name.pluralize}_controller.rb"
  template "server/index.html.erb", "app/views/#{module_name.pluralize}/index.html.erb"
  template "server/layout.html.erb", "app/views/layouts/#{module_name}.html.erb"
end
copy_template_files() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 26
def copy_template_files 
  client_template_files.each do |file|
    file_name = file[2] ? module_name.capitalize : module_name
    template "client/#{file[0]}/#{file[1]}", 
      "#{client_dest_prefix}/#{file[0]}/#{file_name}#{file[1].gsub('.erb', '')}"
  end
end
copy_webpack_config() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 49
def copy_webpack_config
  if !dest_file_exists?("client/webpack.config.js")
    template "client/webpack.config.js.erb", "client/webpack.config.js"
  end
end
create_react_directories() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 16
def create_react_directories
  dirs = %w[components containers startup]
  dirs.each { |name| empty_directory("#{client_dest_prefix}/#{name}") }
end
create_redux_directories() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 21
def create_redux_directories
  dirs = %w[actions constants reducers store]
  dirs.each { |name| empty_directory("#{client_dest_prefix}/#{name}") }
end
module_name() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 8
def module_name 
  @module_name ||= "#{file_name}"
end

Private Instance Methods

add_yarn_postinstall_script_in_package_json() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 118
      def add_yarn_postinstall_script_in_package_json
        client_package_json = File.join(destination_root, "package.json")
        contents = File.read(client_package_json)
        postinstall = %("postinstall": "cd client && yarn install")
        if contents =~ /"scripts" *:/
          replacement = <<-STRING
    "scripts": {
    #{postinstall},
    STRING
          regexp = / {2}"scripts": {/
        else
          regexp = /^{/
          replacement = <<-STRING.strip_heredoc
            {
              "scripts": {
                #{postinstall}
              },
          STRING
        end
    
        contents.gsub!(regexp, replacement)
        File.open(client_package_json, "w+") { |f| f.puts contents }
      end
app_const_base() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 109
def app_const_base
  @app_const_base ||= defined_app_const_base || app_name.gsub(/\W/, "_").squeeze("_").camelize
end
app_name() click to toggle source

From github.com/rails/rails/blob/4c940b2dbfb457f67c6250b720f63501d74a45fd/railties/lib/rails/generators/rails/app/app_generator.rb

# File lib/generators/ror_generators/new_generator.rb, line 93
def app_name
  @app_name ||= (defined_app_const_base? ? defined_app_name : File.basename(destination_root))
                .tr('\\', "").tr(". ", "_")
end
client_template_files() click to toggle source

def template_package_json

if dest_file_exists?("package.json")
  add_yarn_postinstall_script_in_package_json
else
  template("base/base/package.json", "package.json")
end

end

# File lib/generators/ror_generators/new_generator.rb, line 80
def client_template_files 
  [
    ["actions", "ActionCreators.jsx.erb", false],
    ["components", ".jsx.erb", true],
    ["constants", "Constants.jsx.erb", false],
    ["containers", "Container.jsx.erb", true],
    ["reducers", "Reducer.jsx.erb", false],
    ["startup", "App.jsx.erb", true],
    ["store", "Store.jsx.erb", false]
  ]
end
defined_app_const_base() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 102
def defined_app_const_base
  Rails.respond_to?(:application) && defined?(Rails::Application) &&
    Rails.application.is_a?(Rails::Application) && Rails.application.class.name.sub(/::Application$/, "")
end
Also aliased as: defined_app_const_base?
defined_app_const_base?()
defined_app_name() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 98
def defined_app_name
  defined_app_const_base.underscore
end
dest_file_exists?(file) click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 113
def dest_file_exists?(file)
  dest_file = File.join(destination_root, file)
  File.exist?(dest_file) ? dest_file : nil
end
not_in_bundle?() click to toggle source
# File lib/generators/ror_generators/new_generator.rb, line 142
def not_in_bundle?
  if Pathname.new('client/webpack.config.js').file? 
    match = File.readlines('client/webpack.config.js').find do |line|
      line.include?("#{module_name}/startup/registration")
    end
  end
  match.nil?
end