diff options
Diffstat (limited to 'railties/lib/rails/generators')
64 files changed, 443 insertions, 306 deletions
diff --git a/railties/lib/rails/generators/actions.rb b/railties/lib/rails/generators/actions.rb index 32793b1a70..c41acc7841 100644 --- a/railties/lib/rails/generators/actions.rb +++ b/railties/lib/rails/generators/actions.rb @@ -5,15 +5,11 @@ module Rails module Generators module Actions - # Adds an entry into Gemfile for the supplied gem. If env - # is specified, add the gem to the given environment. - # - # ==== Example + # Adds an entry into Gemfile for the supplied gem. # # gem "rspec", :group => :test # gem "technoweenie-restful-authentication", :lib => "restful-authentication", :source => "http://gems.github.com/" # gem "rails", "3.0", :git => "git://github.com/rails/rails" - # def gem(*args) options = args.extract_options! name, version = args @@ -30,7 +26,7 @@ module Rails log :gemfile, message options.each do |option, value| - parts << ":#{option} => #{value.inspect}" + parts << "#{option}: #{value.inspect}" end in_root do @@ -43,12 +39,9 @@ module Rails # Wraps gem entries inside a group. # - # ==== Example - # # gem_group :development, :test do # gem "rspec-rails" # end - # def gem_group(*names, &block) name = names.map(&:inspect).join(", ") log :gemfile, "group #{name}" @@ -66,8 +59,6 @@ module Rails # Add the given source to Gemfile # - # ==== Example - # # add_source "http://gems.github.com/" def add_source(source, options={}) log :source, source @@ -82,6 +73,13 @@ module Rails # If options :env is specified, the line is appended to the corresponding # file in config/environments. # + # environment do + # "config.autoload_paths += %W(#{config.root}/extras)" + # end + # + # environment(nil, :env => "development") do + # "config.active_record.observers = :cacher" + # end def environment(data=nil, options={}, &block) sentinel = /class [a-z_:]+ < Rails::Application/i env_file_sentinel = /::Application\.configure do/ @@ -101,12 +99,9 @@ module Rails # Run a command in git. # - # ==== Examples - # # git :init # git :add => "this.file that.rb" # git :add => "onefile.rb", :rm => "badfile.cxx" - # def git(commands={}) if commands.is_a?(Symbol) run "git #{commands}" @@ -120,15 +115,12 @@ module Rails # Create a new file in the vendor/ directory. Code can be specified # in a block or a data string can be given. # - # ==== Examples - # # vendor("sekrit.rb") do # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--" # "salt = '#{sekrit_salt}'" # end # # vendor("foreign.rb", "# Foreign code is fun") - # def vendor(filename, data=nil, &block) log :vendor, filename create_file("vendor/#{filename}", data, :verbose => false, &block) @@ -137,14 +129,11 @@ module Rails # Create a new file in the lib/ directory. Code can be specified # in a block or a data string can be given. # - # ==== Examples - # # lib("crypto.rb") do # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'" # end # # lib("foreign.rb", "# Foreign code is fun") - # def lib(filename, data=nil, &block) log :lib, filename create_file("lib/#{filename}", data, :verbose => false, &block) @@ -152,22 +141,19 @@ module Rails # Create a new Rakefile with the provided code (either in a block or a string). # - # ==== Examples - # # rakefile("bootstrap.rake") do # project = ask("What is the UNIX name of your project?") # # <<-TASK # namespace :#{project} do # task :bootstrap do - # puts "i like boots!" + # puts "I like boots!" # end # end # TASK # end # - # rakefile("seed.rake", "puts 'im plantin ur seedz'") - # + # rakefile('seed.rake', 'puts "Planting seeds"') def rakefile(filename, data=nil, &block) log :rakefile, filename create_file("lib/tasks/#{filename}", data, :verbose => false, &block) @@ -175,8 +161,6 @@ module Rails # Create a new initializer with the provided code (either in a block or a string). # - # ==== Examples - # # initializer("globals.rb") do # data = "" # @@ -188,7 +172,6 @@ module Rails # end # # initializer("api.rb", "API_KEY = '123456'") - # def initializer(filename, data=nil, &block) log :initializer, filename create_file("config/initializers/#{filename}", data, :verbose => false, &block) @@ -198,10 +181,7 @@ module Rails # The second parameter is the argument string that is passed to # the generator or an Array that is joined. # - # ==== Example - # # generate(:authenticated, "user session") - # def generate(what, *args) log :generate, what argument = args.map {|arg| arg.to_s }.flatten.join(" ") @@ -211,12 +191,9 @@ module Rails # Runs the supplied rake task # - # ==== Example - # # rake("db:migrate") # rake("db:migrate", :env => "production") # rake("gems:install", :sudo => true) - # def rake(command, options={}) log :rake, command env = options[:env] || ENV["RAILS_ENV"] || 'development' @@ -226,10 +203,7 @@ module Rails # Just run the capify command in root # - # ==== Example - # # capify! - # def capify! log :capify, "" in_root { run("#{extify(:capify)} .", :verbose => false) } @@ -237,10 +211,7 @@ module Rails # Make an entry in Rails routing file config/routes.rb # - # === Example - # - # route "root :to => 'welcome'" - # + # route "root :to => 'welcome#index'" def route(routing_code) log :route, routing_code sentinel = /\.routes\.draw do\s*$/ @@ -252,10 +223,7 @@ module Rails # Reads the given file at the source root and prints it in the console. # - # === Example - # # readme "README" - # def readme(path) log File.read(find_in_source_paths(path)) end @@ -265,7 +233,6 @@ module Rails # Define log for backwards compatibility. If just one argument is sent, # invoke say, otherwise invoke say_status. Differently from say and # similarly to say_status, this method respects the quiet? option given. - # def log(*args) if args.size == 1 say args.first.to_s unless options.quiet? @@ -276,7 +243,6 @@ module Rails end # Add an extension to the given name based on the platform. - # def extify(name) if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ "#{name}.bat" diff --git a/railties/lib/rails/generators/active_model.rb b/railties/lib/rails/generators/active_model.rb index 4b828340d2..0e51b9c568 100644 --- a/railties/lib/rails/generators/active_model.rb +++ b/railties/lib/rails/generators/active_model.rb @@ -11,7 +11,7 @@ module Rails # ActiveRecord::Generators::ActiveModel.find(Foo, "params[:id]") # # => "Foo.find(params[:id])" # - # Datamapper::Generators::ActiveModel.find(Foo, "params[:id]") + # DataMapper::Generators::ActiveModel.find(Foo, "params[:id]") # # => "Foo.get(params[:id])" # # On initialization, the ActiveModel accepts the instance name that will @@ -37,7 +37,7 @@ module Rails # GET show # GET edit - # PUT update + # PATCH/PUT update # DELETE destroy def self.find(klass, params=nil) "#{klass}.find(#{params})" @@ -58,13 +58,13 @@ module Rails "#{name}.save" end - # PUT update + # PATCH/PUT update def update_attributes(params=nil) "#{name}.update_attributes(#{params})" end # POST create - # PUT update + # PATCH/PUT update def errors "#{name}.errors" end diff --git a/railties/lib/rails/generators/app_base.rb b/railties/lib/rails/generators/app_base.rb index 7c449657b5..d69afcd2cb 100644 --- a/railties/lib/rails/generators/app_base.rb +++ b/railties/lib/rails/generators/app_base.rb @@ -49,6 +49,9 @@ module Rails class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false, :desc => "Skip JavaScript files" + class_option :skip_index_html, :type => :boolean, :aliases => "-I", :default => false, + :desc => "Skip public/index.html and app/assets/images/rails.png files" + class_option :dev, :type => :boolean, :default => false, :desc => "Setup the #{name} with Gemfile pointing to your Rails checkout" @@ -120,7 +123,7 @@ module Rails end def database_gemfile_entry - options[:skip_active_record] ? "" : "gem '#{gem_for_database}'\n" + options[:skip_active_record] ? "" : "gem '#{gem_for_database}'" end def include_all_railties? @@ -134,22 +137,24 @@ module Rails def rails_gemfile_entry if options.dev? <<-GEMFILE.strip_heredoc - gem 'rails', :path => '#{Rails::Generators::RAILS_DEV_PATH}' - gem 'journey', :git => 'https://github.com/rails/journey.git' - gem 'arel', :git => 'https://github.com/rails/arel.git' + gem 'rails', path: '#{Rails::Generators::RAILS_DEV_PATH}' + gem 'journey', github: 'rails/journey' + gem 'arel', github: 'rails/arel' + gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders' GEMFILE elsif options.edge? <<-GEMFILE.strip_heredoc - gem 'rails', :git => 'https://github.com/rails/rails.git' - gem 'journey', :git => 'https://github.com/rails/journey.git' - gem 'arel', :git => 'https://github.com/rails/arel.git' + gem 'rails', github: 'rails/rails' + gem 'journey', github: 'rails/journey' + gem 'arel', github: 'rails/arel' + gem 'activerecord-deprecated_finders', github: 'rails/activerecord-deprecated_finders' GEMFILE else <<-GEMFILE.strip_heredoc gem 'rails', '#{Rails::VERSION::STRING}' # Bundle edge Rails instead: - # gem 'rails', :git => 'https://github.com/rails/rails.git' + # gem 'rails', github: 'rails/rails' GEMFILE end end @@ -189,8 +194,9 @@ module Rails # Gems used only for assets and not required # in production environments by default. group :assets do - gem 'sass-rails', :git => 'https://github.com/rails/sass-rails.git' - gem 'coffee-rails', :git => 'https://github.com/rails/coffee-rails.git' + gem 'sprockets-rails', github: 'rails/sprockets-rails' + gem 'sass-rails', github: 'rails/sass-rails' + gem 'coffee-rails', github: 'rails/coffee-rails' # See https://github.com/sstephenson/execjs#readme for more supported runtimes #{javascript_runtime_gemfile_entry} @@ -202,6 +208,7 @@ module Rails # Gems used only for assets and not required # in production environments by default. group :assets do + gem 'sprockets-rails', github: 'rails/sprockets-rails' gem 'sass-rails', '~> 4.0.0.beta' gem 'coffee-rails', '~> 4.0.0.beta' @@ -223,7 +230,7 @@ module Rails if defined?(JRUBY_VERSION) "gem 'therubyrhino'\n" else - "# gem 'therubyracer'\n" + "# gem 'therubyracer', platforms: :ruby\n" end end @@ -239,8 +246,15 @@ module Rails # is easier to silence stdout in the existing test suite this way. The # end-user gets the bundler commands called anyway, so no big deal. # + # We unset temporary bundler variables to load proper bundler and Gemfile. + # # Thanks to James Tucker for the Gem tricks involved in this call. - print `"#{Gem.ruby}" -rubygems "#{Gem.bin_path('bundler', 'bundle')}" #{command}` + _bundle_command = Gem.bin_path('bundler', 'bundle') + + require 'bundler' + Bundler.with_clean_env do + print `"#{Gem.ruby}" "#{_bundle_command}" #{command}` + end end def run_bundle @@ -255,11 +269,6 @@ module Rails def git_keep(destination) create_file("#{destination}/.gitkeep") unless options[:skip_git] end - - # Returns Ruby 1.9 style key-value pair. - def key_value(key, value) - "#{key}: #{value}" - end end end end diff --git a/railties/lib/rails/generators/base.rb b/railties/lib/rails/generators/base.rb index 60e94486bb..a3f8ebf476 100644 --- a/railties/lib/rails/generators/base.rb +++ b/railties/lib/rails/generators/base.rb @@ -8,7 +8,6 @@ rescue LoadError end require 'rails/generators/actions' -require 'active_support/core_ext/object/inclusion' module Rails module Generators @@ -20,6 +19,7 @@ module Rails include Rails::Generators::Actions add_runtime_options! + strict_args_position! # Returns the source root for this generator using default_source_root as default. def self.source_root(path=nil) @@ -31,10 +31,9 @@ module Rails # root otherwise uses a default description. def self.desc(description=nil) return super if description - usage = source_root && File.expand_path("../USAGE", source_root) - @desc ||= if usage && File.exist?(usage) - ERB.new(File.read(usage)).result(binding) + @desc ||= if usage_path + ERB.new(File.read(usage_path)).result(binding) else "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator." end @@ -171,7 +170,7 @@ module Rails names.each do |name| defaults = if options[:type] == :boolean { } - elsif default_value_for_option(name, options).in?([true, false]) + elsif [true, false].include?(default_value_for_option(name, options)) { :banner => "" } else { :desc => "#{name.to_s.humanize} to be invoked", :banner => "NAME" } @@ -188,10 +187,7 @@ module Rails # Remove a previously added hook. # - # ==== Examples - # # remove_hook_for :orm - # def self.remove_hook_for(*names) remove_invocation(*names) @@ -213,7 +209,8 @@ module Rails # root, you should use source_root. def self.default_source_root return unless base_name && generator_name - path = File.expand_path(File.join(base_name, generator_name, 'templates'), base_root) + return unless default_generator_root + path = File.join(default_generator_root, 'templates') path if File.exists?(path) end @@ -248,7 +245,6 @@ module Rails # Check whether the given class names are already taken by user # application or Ruby on Rails. - # def class_collisions(*class_names) #:nodoc: return unless behavior == :invoke @@ -275,13 +271,11 @@ module Rails end # Use Rails default banner. - # def self.banner "rails generate #{namespace.sub(/^rails:/,'')} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]".gsub(/\s+/, ' ') end # Sets the base_name taking into account the current class namespace. - # def self.base_name @base_name ||= begin if base = name.to_s.split('::').first @@ -292,7 +286,6 @@ module Rails # Removes the namespaces and get the generator name. For example, # Rails::Generators::ModelGenerator will return "model" as generator name. - # def self.generator_name @generator_name ||= begin if generator = name.to_s.split('::').last @@ -304,20 +297,17 @@ module Rails # Return the default value for the option name given doing a lookup in # Rails::Generators.options. - # def self.default_value_for_option(name, options) default_for_option(Rails::Generators.options, name, options, options[:default]) end # Return default aliases for the option name given doing a lookup in # Rails::Generators.aliases. - # def self.default_aliases_for_option(name, options) default_for_option(Rails::Generators.aliases, name, options, options[:aliases]) end # Return default for the option name given doing a lookup in config. - # def self.default_for_option(config, name, options, default) if generator_name and c = config[generator_name.to_sym] and c.key?(name) c[name] @@ -331,14 +321,12 @@ module Rails end # Keep hooks configuration that are used on prepare_for_invocation. - # def self.hooks #:nodoc: @hooks ||= from_superclass(:hooks, {}) end # Prepare class invocation to search on Rails namespace if a previous # added hook is being used. - # def self.prepare_for_invocation(name, value) #:nodoc: return super unless value.is_a?(String) || value.is_a?(Symbol) @@ -354,7 +342,6 @@ module Rails # Small macro to add ruby as an option to the generator with proper # default value plus an instance helper method called shebang. - # def self.add_shebang_option! class_option :ruby, :type => :string, :aliases => "-r", :default => Thor::Util.ruby_command, :desc => "Path to the Ruby binary of your choice", :banner => "PATH" @@ -373,6 +360,19 @@ module Rails } end + def self.usage_path + paths = [ + source_root && File.expand_path("../USAGE", source_root), + default_generator_root && File.join(default_generator_root, "USAGE") + ] + paths.compact.detect { |path| File.exists? path } + end + + def self.default_generator_root + path = File.expand_path(File.join(base_name, generator_name), base_root) + path if File.exists?(path) + end + end end end diff --git a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb index 85296ca37b..f5182bcc50 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/index.html.erb @@ -1,23 +1,27 @@ <h1>Listing <%= plural_table_name %></h1> <table> - <tr> -<% attributes.each do |attribute| -%> - <th><%= attribute.human_name %></th> -<% end -%> - <th></th> - <th></th> - <th></th> - </tr> + <thead> + <tr> + <% attributes.each do |attribute| -%> + <th><%= attribute.human_name %></th> + <% end -%> + <th></th> + <th></th> + <th></th> + </tr> + </thead> - <%%= content_tag_for(:tr, @<%= plural_table_name %>) do |<%= singular_table_name %>| %> -<% attributes.each do |attribute| -%> - <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td> -<% end -%> - <td><%%= link_to 'Show', <%= singular_table_name %> %></td> - <td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td> - <td><%%= link_to 'Destroy', <%= singular_table_name %>, <%= key_value :confirm, "'Are you sure?'" %>, <%= key_value :method, ":delete" %> %></td> - <%% end %> + <tbody> + <%%= content_tag_for(:tr, @<%= plural_table_name %>) do |<%= singular_table_name %>| %> + <% attributes.each do |attribute| -%> + <td><%%= <%= singular_table_name %>.<%= attribute.name %> %></td> + <% end -%> + <td><%%= link_to 'Show', <%= singular_table_name %> %></td> + <td><%%= link_to 'Edit', edit_<%= singular_table_name %>_path(<%= singular_table_name %>) %></td> + <td><%%= link_to 'Destroy', <%= singular_table_name %>, method: :delete, data: { confirm: 'Are you sure?' } %></td> + <%% end %> + </tbody> </table> <br /> diff --git a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb index 67f263efbb..e15c963971 100644 --- a/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb +++ b/railties/lib/rails/generators/erb/scaffold/templates/show.html.erb @@ -2,7 +2,7 @@ <% attributes.each do |attribute| -%> <p> - <b><%= attribute.human_name %>:</b> + <strong><%= attribute.human_name %>:</strong> <%%= @<%= singular_table_name %>.<%= attribute.name %> %> </p> diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index 7dfc1aa599..d2c2abf40c 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -1,6 +1,4 @@ require 'active_support/time' -require 'active_support/core_ext/object/inclusion' -require 'active_support/core_ext/object/blank' module Rails module Generators @@ -10,6 +8,7 @@ module Rails attr_accessor :name, :type attr_reader :attr_options + attr_writer :index_name class << self def parse(column_definition) @@ -21,12 +20,23 @@ module Rails has_index, type = type, nil if INDEX_OPTIONS.include?(type) type, attr_options = *parse_type_and_options(type) + type = type.to_sym if type + + if type && reference?(type) + references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { :unique => true } : true + attr_options[:index] = references_index + end + new(name, type, has_index, attr_options) end + def reference?(type) + [:references, :belongs_to].include? type + end + private - # parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals + # parse possible attribute options like :limit for string/text/binary/integer, :precision/:scale for decimals or :polymorphic for references/belongs_to # when declaring options curly brackets should be used def parse_type_and_options(type) case type @@ -34,6 +44,8 @@ module Rails return $1, :limit => $2.to_i when /decimal\{(\d+)[,.-](\d+)\}/ return :decimal, :precision => $1.to_i, :scale => $2.to_i + when /(references|belongs_to)\{polymorphic\}/ + return $1, :polymorphic => true else return type, {} end @@ -42,7 +54,7 @@ module Rails def initialize(name, type=nil, index_type=false, attr_options={}) @name = name - @type = (type.presence || :string).to_sym + @type = type || :string @has_index = INDEX_OPTIONS.include?(index_type) @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type) @attr_options = attr_options @@ -78,16 +90,32 @@ module Rails end end + def plural_name + name.sub(/_id$/, '').pluralize + end + def human_name - name.to_s.humanize + name.humanize end def index_name - reference? ? "#{name}_id" : name + @index_name ||= if reference? + polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id" + else + name + end + end + + def foreign_key? + !!(name =~ /_id$/) end def reference? - self.type.in?(:references, :belongs_to) + self.class.reference?(type) + end + + def polymorphic? + self.attr_options.has_key?(:polymorphic) end def has_index? diff --git a/railties/lib/rails/generators/migration.rb b/railties/lib/rails/generators/migration.rb index 0c5c4f6e09..5bf98bb6e0 100644 --- a/railties/lib/rails/generators/migration.rb +++ b/railties/lib/rails/generators/migration.rb @@ -3,7 +3,6 @@ module Rails # Holds common methods for migrations. It assumes that migrations has the # [0-9]*_name format and can be used by another frameworks (like Sequel) # just by implementing the next migration version method. - # module Migration attr_reader :migration_number, :migration_file_name, :migration_class_name @@ -38,10 +37,7 @@ module Rails # The migration version, migration file name, migration class name are # available as instance variables in the template to be rendered. # - # ==== Examples - # # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" - # def migration_template(source, destination=nil, config={}) destination = File.expand_path(destination || source, self.destination_root) diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 9cef55e0a6..b61a5fc69d 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -40,7 +40,7 @@ module Rails def indent(content, multiplier = 2) spaces = " " * multiplier - content = content.each_line.map {|line| "#{spaces}#{line}" }.join + content = content.each_line.map {|line| line.blank? ? line : "#{spaces}#{line}" }.join end def wrap_with_namespace(content) @@ -79,11 +79,16 @@ module Rails @class_path end + def namespaced_file_path + @namespaced_file_path ||= namespaced_class_path.join("/") + end + def namespaced_class_path - @namespaced_class_path ||= begin - namespace_path = namespace.name.split("::").map {|m| m.underscore } - namespace_path + @class_path - end + @namespaced_class_path ||= [namespaced_path] + @class_path + end + + def namespaced_path + @namespaced_path ||= namespace.name.split("::").map {|m| m.underscore }[0] end def class_name @@ -99,7 +104,7 @@ module Rails end def i18n_scope - @i18n_scope ||= file_path.gsub('/', '.') + @i18n_scope ||= file_path.tr('/', '.') end def table_name @@ -130,7 +135,7 @@ module Rails end def route_url - @route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name + @route_url ||= class_path.collect {|dname| "/" + dname }.join + "/" + plural_file_name end # Tries to retrieve the application name or simple return application. @@ -180,11 +185,6 @@ module Rails class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}" end end - - # Returns Ruby 1.9 style key-value pair. - def key_value(key, value) - "#{key}: #{value}" - end end end end diff --git a/railties/lib/rails/generators/rails/app/app_generator.rb b/railties/lib/rails/generators/rails/app/app_generator.rb index f0745df667..c06b0f8994 100644 --- a/railties/lib/rails/generators/rails/app/app_generator.rb +++ b/railties/lib/rails/generators/rails/app/app_generator.rb @@ -97,6 +97,11 @@ module Rails def public_directory directory "public", "public", :recursive => false + if options[:skip_index_html] + remove_file "public/index.html" + remove_file 'app/assets/images/rails.png' + git_keep 'app/assets/images' + end end def script diff --git a/railties/lib/rails/generators/rails/app/templates/Gemfile b/railties/lib/rails/generators/rails/app/templates/Gemfile index 5fdfe8f04e..55a6b3f4f2 100644 --- a/railties/lib/rails/generators/rails/app/templates/Gemfile +++ b/railties/lib/rails/generators/rails/app/templates/Gemfile @@ -15,11 +15,11 @@ source 'https://rubygems.org' # To use Jbuilder templates for JSON # gem 'jbuilder' -# Use unicorn as the web server +# Use unicorn as the app server # gem 'unicorn' # Deploy with Capistrano -# gem 'capistrano', :group => :development +# gem 'capistrano', group: :development # To use debugger -# gem 'ruby-debug19', :require => 'ruby-debug' +# gem 'debugger' diff --git a/railties/lib/rails/generators/rails/app/templates/README b/railties/lib/rails/generators/rails/app/templates/README index d2014bd35f..b5d7b6436b 100644 --- a/railties/lib/rails/generators/rails/app/templates/README +++ b/railties/lib/rails/generators/rails/app/templates/README @@ -86,8 +86,8 @@ programming in general. Debugger support is available through the debugger command when you start your Mongrel or WEBrick server with --debugger. This means that you can break out of execution at any point in the code, investigate and change the model, and then, -resume execution! You need to install ruby-debug19 to run the server in debugging -mode. With gems, use <tt>sudo gem install ruby-debug19</tt>. Example: +resume execution! You need to install the 'debugger' gem to run the server in debugging +mode. Add gem 'debugger' to your Gemfile and run <tt>bundle</tt> to install it. Example: class WeblogController < ActionController::Base def index diff --git a/railties/lib/rails/generators/rails/app/templates/Rakefile b/railties/lib/rails/generators/rails/app/templates/Rakefile index 4dc1023f1f..6eb23f68a3 100755..100644 --- a/railties/lib/rails/generators/rails/app/templates/Rakefile +++ b/railties/lib/rails/generators/rails/app/templates/Rakefile @@ -1,4 +1,3 @@ -#!/usr/bin/env rake # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. diff --git a/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css b/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css index 3b5cc6648e..3192ec897b 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css +++ b/railties/lib/rails/generators/rails/app/templates/app/assets/stylesheets/application.css @@ -10,4 +10,4 @@ * *= require_self *= require_tree . -*/ + */ diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb deleted file mode 100644 index e8065d9505..0000000000 --- a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class ApplicationController < ActionController::Base - protect_from_forgery -end diff --git a/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt new file mode 100644 index 0000000000..6c0ef31725 --- /dev/null +++ b/railties/lib/rails/generators/rails/app/templates/app/controllers/application_controller.rb.tt @@ -0,0 +1,5 @@ +class ApplicationController < ActionController::Base + # Prevent CSRF attacks by raising an exception. + # For APIs, you may want to use :reset_session instead. + protect_from_forgery with: :exception +end diff --git a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt index bba96a7431..e0539aa8bb 100644 --- a/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +++ b/railties/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt @@ -2,7 +2,7 @@ <html> <head> <title><%= camelized %></title> - <%%= stylesheet_link_tag "application", :media => "all" %> + <%%= stylesheet_link_tag "application", media: "all" %> <%%= javascript_include_tag "application" %> <%%= csrf_meta_tags %> </head> diff --git a/railties/lib/rails/generators/rails/app/templates/config/application.rb b/railties/lib/rails/generators/rails/app/templates/config/application.rb index acf47a03e5..1ac0248bcf 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/application.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/application.rb @@ -7,14 +7,13 @@ require 'rails/all' <%= comment_if :skip_active_record %>require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" -require "active_resource/railtie" -<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/rails/railtie" <%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" <% end -%> if defined?(Bundler) # If you precompile assets before deploying to production, use this line. - Bundler.require(*Rails.groups(:assets => %w(development test))) + Bundler.require(*Rails.groups(assets: %w(development test))) # If you want your assets lazily compiled in production, use this line. # Bundler.require(:default, :assets, Rails.env) end @@ -28,9 +27,6 @@ module <%= app_const_base %> # Custom directories with classes and modules you want to be autoloadable. # config.autoload_paths += %W(#{config.root}/extras) - # Activate observers that should always be running. - # config.active_record.observers = :cacher, :garbage_collector, :forum_observer - # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # config.time_zone = 'Central Time (US & Canada)' @@ -54,19 +50,17 @@ module <%= app_const_base %> # This will create an empty whitelist of attributes available for mass-assignment for all models # in your app. As such, your models will need to explicitly whitelist or blacklist accessible # parameters by using an attr_accessible or attr_protected declaration. - # config.active_record.whitelist_attributes = true - - # Specifies wether or not has_many or has_one association option :dependent => :restrict raises - # an exception. If set to true, then an ActiveRecord::DeleteRestrictionError exception would be - # raised. If set to false, then an error will be added on the model instead. - <%= comment_if :skip_active_record %>config.active_record.dependent_restrict_raises = false - + <%= comment_if :skip_active_record %>config.active_record.whitelist_attributes = true <% unless options.skip_sprockets? -%> + # Enable the asset pipeline. config.assets.enabled = true # Version of your assets, change this if you want to expire all your assets. config.assets.version = '1.0' <% end -%> + + # Enable app-wide asynchronous ActionMailer. + # config.action_mailer.async = true end end diff --git a/railties/lib/rails/generators/rails/app/templates/config/boot.rb b/railties/lib/rails/generators/rails/app/templates/config/boot.rb index 4489e58688..3596736667 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/boot.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/boot.rb @@ -1,5 +1,3 @@ -require 'rubygems' - # Set up gems listed in the Gemfile. ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml index fe9466b366..e1a00d076f 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml @@ -22,8 +22,8 @@ development: # Minimum log levels, in increasing order: # debug5, debug4, debug3, debug2, debug1, # log, notice, warning, error, fatal, and panic - # The server defaults to notice. - #min_messages: warning + # Defaults to warning. + #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml index cce166c7c3..c3349912aa 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/mysql.yml @@ -1,5 +1,5 @@ # MySQL. Versions 4.1 and 5.0 are recommended. -# +# # Install the MYSQL driver # gem install mysql2 # @@ -11,7 +11,6 @@ development: adapter: mysql2 encoding: utf8 - reconnect: false database: <%= app_name %>_development pool: 5 username: root @@ -28,7 +27,6 @@ development: test: adapter: mysql2 encoding: utf8 - reconnect: false database: <%= app_name %>_test pool: 5 username: root @@ -42,7 +40,6 @@ test: production: adapter: mysql2 encoding: utf8 - reconnect: false database: <%= app_name %>_production pool: 5 username: root diff --git a/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml index f08f86aac3..07223a71c9 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml @@ -24,6 +24,9 @@ development: # domain socket that doesn't need configuration. Windows does not have # domain sockets, so uncomment these lines. #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. #port: 5432 # Schema search path. The server defaults to $user,public @@ -32,8 +35,8 @@ development: # Minimum log levels, in increasing order: # debug5, debug4, debug3, debug2, debug1, # log, notice, warning, error, fatal, and panic - # The server defaults to notice. - #min_messages: warning + # Defaults to warning. + #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". diff --git a/railties/lib/rails/generators/rails/app/templates/config/environment.rb b/railties/lib/rails/generators/rails/app/templates/config/environment.rb index 1684986a59..e080ebd74e 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environment.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/environment.rb @@ -1,5 +1,5 @@ -# Load the rails application +# Load the rails application. require File.expand_path('../application', __FILE__) -# Initialize the rails application +# Initialize the rails application. <%= app_const %>.initialize! diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt index eb4dfa7c89..122e7e2b34 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt @@ -6,6 +6,9 @@ # since you don't have to restart the web server when you make code changes. config.cache_classes = false + # Do not eager load code on boot. + config.eager_load = false + # Show full error reports and disable caching. config.consider_all_requests_local = true config.action_controller.perform_caching = false @@ -26,13 +29,19 @@ # Log the query plan for queries taking more than this (works # with SQLite, MySQL, and PostgreSQL). config.active_record.auto_explain_threshold_in_seconds = 0.5 + + # Raise an error on page load if there are pending migrations + config.active_record.migration_error = :page_load <%- end -%> <%- unless options.skip_sprockets? -%> # Do not compress assets. config.assets.compress = false - # Expands the lines which load the assets. + # Debug mode disables concatenation and preprocessing of assets. config.assets.debug = true <%- end -%> + + # In development, use an in-memory queue for queueing. + config.queue = Rails::Queueing::Queue end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt index e9a86d175e..a627636089 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt @@ -4,6 +4,12 @@ # Code is not reloaded between requests. config.cache_classes = true + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both thread web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + # Full error reports are disabled and caching is turned on. config.consider_all_requests_local = false config.action_controller.perform_caching = true @@ -20,9 +26,6 @@ # Generate digests for assets URLs. config.assets.digest = true - - # Defaults to Rails.root.join("public/assets"). - # config.assets.manifest = YOUR_PATH <%- end -%> # Specifies the header that your server uses for sending files. @@ -32,8 +35,8 @@ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. # config.force_ssl = true - # See everything in the log (default is :info). - # config.log_level = :debug + # Set to :debug to see everything in the log. + config.log_level = :info # Prepend all log lines with the following tags. # config.log_tags = [ :subdomain, :uuid ] @@ -70,4 +73,14 @@ # with SQLite, MySQL, and PostgreSQL). # config.active_record.auto_explain_threshold_in_seconds = 0.5 <%- end -%> + + # Disable automatic flushing of the log to improve performance. + # config.autoflush_log = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Default the production mode queue to an in-memory queue. You will probably + # want to replace this with an out-of-process queueing solution. + config.queue = Rails::Queueing::Queue end diff --git a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt index b725dd19f6..8ab27eb6e1 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt @@ -7,6 +7,11 @@ # and recreated between test runs. Don't rely on the data there! config.cache_classes = true + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + # Configure static asset server for tests with Cache-Control for performance. config.serve_static_assets = true config.static_cache_control = "public, max-age=3600" @@ -33,4 +38,7 @@ # Print deprecation notices to the stderr. config.active_support.deprecation = :stderr + + # Use the testing queue. + config.queue = Rails::Queueing::TestQueue end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb b/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb index 5d8d9be237..9262c3379f 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/inflections.rb @@ -1,8 +1,9 @@ # Be sure to restart your server when you modify this file. -# Add new inflection rules using the following format -# (all these examples are active by default): -# ActiveSupport::Inflector.inflections do |inflect| +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| # inflect.plural /^(ox)$/i, '\1en' # inflect.singular /^(ox)en/i, '\1' # inflect.irregular 'person', 'people' @@ -10,6 +11,6 @@ # end # # These inflection rules are supported but not enabled by default: -# ActiveSupport::Inflector.inflections do |inflect| +# ActiveSupport::Inflector.inflections(:en) do |inflect| # inflect.acronym 'RESTful' # end diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt index a3143f1346..3c5611ca59 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/secret_token.rb.tt @@ -2,6 +2,11 @@ # Your secret key for verifying the integrity of signed cookies. # If you change this key, all old signed cookies will become invalid! + # Make sure the secret is at least 30 characters and all random, # no regular words or you'll be exposed to dictionary attacks. +# You can use `rake secret` to generate a secure secret key. + +# Make sure your secret_token is kept private +# if you're sharing your code publicly. <%= app_const %>.config.secret_token = '<%= app_secret %>' diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt index ddfe4ba1e1..4a099a4ce2 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/session_store.rb.tt @@ -1,8 +1,3 @@ # Be sure to restart your server when you modify this file. -<%= app_const %>.config.session_store :cookie_store, <%= key_value :key, "'_#{app_name}_session'" %> - -# Use the database for sessions instead of the cookie-based default, -# which shouldn't be used to store highly confidential information -# (create the session table with "rails generate session_migration") -# <%= app_const %>.config.session_store :active_record_store +<%= app_const %>.config.session_store :cookie_store, key: <%= "'_#{app_name}_session'" %> diff --git a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt index d640f578da..280f777cc0 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/config/initializers/wrap_parameters.rb.tt @@ -5,12 +5,12 @@ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. ActiveSupport.on_load(:action_controller) do - wrap_parameters <%= key_value :format, "[:json]" %> + wrap_parameters format: [:json] if respond_to?(:wrap_parameters) end <%- unless options.skip_active_record? -%> -# Disable root element in JSON by default. -ActiveSupport.on_load(:active_record) do - self.include_root_in_json = false -end +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end <%- end -%> diff --git a/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml b/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml index 179c14ca52..0653957166 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml +++ b/railties/lib/rails/generators/rails/app/templates/config/locales/en.yml @@ -1,5 +1,23 @@ -# Sample localization file for English. Add more files in this directory for other locales. -# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# To learn more, please read the Rails Internationalization guide +# available at http://guides.rubyonrails.org/i18n.html. en: hello: "Hello world" diff --git a/railties/lib/rails/generators/rails/app/templates/config/routes.rb b/railties/lib/rails/generators/rails/app/templates/config/routes.rb index ea81748464..f6b1ef1feb 100644 --- a/railties/lib/rails/generators/rails/app/templates/config/routes.rb +++ b/railties/lib/rails/generators/rails/app/templates/config/routes.rb @@ -2,13 +2,17 @@ # The priority is based upon order of creation: # first created -> highest priority. + # You can have the root of your site routed with "root" + # just remember to delete public/index.html. + # root to: 'welcome#index' + # Sample of regular route: - # match 'products/:id' => 'catalog#view' - # Keep in mind you can assign values other than :controller and :action + # get 'products/:id' => 'catalog#view' + # Keep in mind you can assign values other than :controller and :action. # Sample of named route: - # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase - # This route can be invoked with purchase_url(:id => product.id) + # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase + # This route can be invoked with purchase_url(id: product.id). # Sample resource route (maps HTTP verbs to controller actions automatically): # resources :products @@ -31,11 +35,11 @@ # resource :seller # end - # Sample resource route with more complex sub-resources + # Sample resource route with more complex sub-resources: # resources :products do # resources :comments # resources :sales do - # get 'recent', :on => :collection + # get 'recent', on: :collection # end # end @@ -46,13 +50,6 @@ # resources :products # end - # You can have the root of your site routed with "root" - # just remember to delete public/index.html. - # root :to => 'welcome#index' - - # See how all your routes lay out with "rake routes" - # This is a legacy wild controller route that's not recommended for RESTful applications. - # Note: This route will make all actions in every controller accessible via GET requests. - # match ':controller(/:action(/:id))(.:format)' + # See how all your routes lay out with "rake routes". end diff --git a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt index f75c5dd941..4edb1e857e 100644 --- a/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +++ b/railties/lib/rails/generators/rails/app/templates/db/seeds.rb.tt @@ -3,5 +3,5 @@ # # Examples: # -# cities = City.create([{ <%= key_value :name, "'Chicago'" %> }, { <%= key_value :name, "'Copenhagen'" %> }]) -# Mayor.create(<%= key_value :name, "'Emanuel'" %>, <%= key_value :city, "cities.first" %>) +# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) +# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/railties/lib/rails/generators/rails/app/templates/public/404.html b/railties/lib/rails/generators/rails/app/templates/public/404.html index 9a48320a5f..3d875c342e 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/404.html +++ b/railties/lib/rails/generators/rails/app/templates/public/404.html @@ -2,7 +2,7 @@ <html> <head> <title>The page you were looking for doesn't exist (404)</title> - <style type="text/css"> + <style> body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } div.dialog { width: 25em; @@ -22,5 +22,6 @@ <h1>The page you were looking for doesn't exist.</h1> <p>You may have mistyped the address or the page may have moved.</p> </div> + <p>If you are the application owner check the logs for more information.</p> </body> </html> diff --git a/railties/lib/rails/generators/rails/app/templates/public/422.html b/railties/lib/rails/generators/rails/app/templates/public/422.html index 83660ab187..3f1bfb3417 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/422.html +++ b/railties/lib/rails/generators/rails/app/templates/public/422.html @@ -2,7 +2,7 @@ <html> <head> <title>The change you wanted was rejected (422)</title> - <style type="text/css"> + <style> body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } div.dialog { width: 25em; diff --git a/railties/lib/rails/generators/rails/app/templates/public/500.html b/railties/lib/rails/generators/rails/app/templates/public/500.html index f3648a0dbc..012977d3d2 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/500.html +++ b/railties/lib/rails/generators/rails/app/templates/public/500.html @@ -2,7 +2,7 @@ <html> <head> <title>We're sorry, but something went wrong (500)</title> - <style type="text/css"> + <style> body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; } div.dialog { width: 25em; @@ -21,5 +21,6 @@ <div class="dialog"> <h1>We're sorry, but something went wrong.</h1> </div> + <p>If you are the application owner check the logs for more information.</p> </body> </html> diff --git a/railties/lib/rails/generators/rails/app/templates/public/index.html b/railties/lib/rails/generators/rails/app/templates/public/index.html index a1d50995c5..dd09a96de9 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/index.html +++ b/railties/lib/rails/generators/rails/app/templates/public/index.html @@ -2,7 +2,7 @@ <html> <head> <title>Ruby on Rails: Welcome aboard</title> - <style type="text/css" media="screen"> + <style media="screen"> body { margin: 0; margin-bottom: 25px; @@ -171,7 +171,7 @@ font-style: italic; } </style> - <script type="text/javascript"> + <script> function about() { info = document.getElementById('about-content'); if (window.XMLHttpRequest) diff --git a/railties/lib/rails/generators/rails/app/templates/public/robots.txt b/railties/lib/rails/generators/rails/app/templates/public/robots.txt index 085187fa58..1a3a5e4dd2 100644 --- a/railties/lib/rails/generators/rails/app/templates/public/robots.txt +++ b/railties/lib/rails/generators/rails/app/templates/public/robots.txt @@ -1,5 +1,5 @@ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file # # To ban all spiders from the entire site uncomment the next two lines: -# User-Agent: * +# User-agent: * # Disallow: / diff --git a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb b/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb index 3fea27b916..d09ce5ad34 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb @@ -3,10 +3,10 @@ require 'rails/performance_test_help' class BrowsingTest < ActionDispatch::PerformanceTest # Refer to the documentation for all available options - # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] - # :output => 'tmp/performance', :formats => [:flat] } + # self.profile_options = { runs: 5, metrics: [:wall_time, :memory], + # output: 'tmp/performance', formats: [:flat] } - def test_homepage + test "homepage" do get '/' end end diff --git a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb index a8f7aeac7d..9afda2d0df 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/test_helper.rb @@ -4,7 +4,9 @@ require 'rails/test_help' class ActiveSupport::TestCase <% unless options[:skip_active_record] -%> - # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. + ActiveRecord::Migration.check_pending! + + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting diff --git a/railties/lib/rails/generators/rails/controller/USAGE b/railties/lib/rails/generators/rails/controller/USAGE index 70618a3906..b658777b12 100644 --- a/railties/lib/rails/generators/rails/controller/USAGE +++ b/railties/lib/rails/generators/rails/controller/USAGE @@ -9,10 +9,10 @@ Description: template engine and test framework generators. Example: - `rails generate controller CreditCard open debit credit close` + `rails generate controller CreditCards open debit credit close` - Credit card controller with URLs like /credit_card/debit. - Controller: app/controllers/credit_card_controller.rb - Functional Test: test/functional/credit_card_controller_test.rb - Views: app/views/credit_card/debit.html.erb [...] - Helper: app/helpers/credit_card_helper.rb + CreditCards controller with URLs like /credit_cards/debit. + Controller: app/controllers/credit_cards_controller.rb + Functional Test: test/functional/credit_cards_controller_test.rb + Views: app/views/credit_cards/debit.html.erb [...] + Helper: app/helpers/credit_cards_helper.rb diff --git a/railties/lib/rails/generators/rails/controller/templates/controller.rb b/railties/lib/rails/generators/rails/controller/templates/controller.rb index 52243f4a2f..633e0b3177 100644 --- a/railties/lib/rails/generators/rails/controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/controller/templates/controller.rb @@ -1,3 +1,7 @@ +<% if namespaced? -%> +require_dependency "<%= namespaced_path %>/application_controller" + +<% end -%> <% module_namespacing do -%> class <%= class_name %>Controller < ApplicationController <% actions.each do |action| -%> diff --git a/railties/lib/rails/generators/rails/model/USAGE b/railties/lib/rails/generators/rails/model/USAGE index 67f76aad01..c46c86076e 100644 --- a/railties/lib/rails/generators/rails/model/USAGE +++ b/railties/lib/rails/generators/rails/model/USAGE @@ -19,6 +19,55 @@ Description: then the generator will create a module with a table_name_prefix method to prefix the model's table name with the module name (e.g. admin_account) +Available field types: + + Just after the field name you can specify a type like text or boolean. + It will generate the column with the associated SQL type. For instance: + + `rails generate model post title:string body:text` + + will generate a title column with a varchar type and a body column with a text + type. You can use the following types: + + integer + primary_key + decimal + float + boolean + binary + string + text + date + time + datetime + timestamp + + You can also consider `references` as a kind of type. For instance, if you run: + + `rails generate model photo title:string album:references` + + It will generate an album_id column. You should generate this kind of fields when + you will use a `belongs_to` association for instance. `references` also support + the polymorphism, you could enable the polymorphism like this: + + `rails generate model product supplier:references{polymorphic}` + + You can also specify some options just after the field type. You can use the + following options: + + limit Set the maximum size of the field giving a number between curly braces + default Set a default value for the field + precision Defines the precision for the decimal fields + scale Defines the scale for the decimal fields + uniq Defines the field values as unique + index Will add an index on the field + + Examples: + + `rails generate model user pseudo:string{30}` + `rails generate model user pseudo:string:uniq` + + Examples: `rails generate model account` diff --git a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb index f4263d1b98..4f937ad65a 100644 --- a/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb +++ b/railties/lib/rails/generators/rails/plugin_new/plugin_new_generator.rb @@ -139,7 +139,7 @@ task :default => :test gemfile_in_app_path = File.join(rails_app_path, "Gemfile") if File.exist? gemfile_in_app_path - entry = "gem '#{name}', :path => '#{relative_path}'" + entry = "gem '#{name}', path: '#{relative_path}'" append_file gemfile_in_app_path, entry end end @@ -155,7 +155,7 @@ task :default => :test :desc => "Create dummy application at given path" class_option :full, :type => :boolean, :default => false, - :desc => "Generate rails engine with integration tests" + :desc => "Generate a rails engine with bundled Rails application for testing" class_option :mountable, :type => :boolean, :default => false, :desc => "Generate mountable isolated application" @@ -232,6 +232,18 @@ task :default => :test public_task :apply_rails_template, :run_bundle + def name + @name ||= begin + # same as ActiveSupport::Inflector#underscore except not replacing '-' + underscored = original_name.dup + underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') + underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2') + underscored.downcase! + + underscored + end + end + protected def app_templates_dir @@ -268,18 +280,6 @@ task :default => :test @original_name ||= File.basename(destination_root) end - def name - @name ||= begin - # same as ActiveSupport::Inflector#underscore except not replacing '-' - underscored = original_name.dup - underscored.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') - underscored.gsub!(/([a-z\d])([A-Z])/,'\1_\2') - underscored.downcase! - - underscored - end - end - def camelized @camelized ||= name.gsub(/\W/, '_').squeeze('_').camelize end @@ -302,7 +302,7 @@ task :default => :test dummy_application_path = File.expand_path("#{dummy_path}/config/application.rb", destination_root) unless options[:pretend] || !File.exists?(dummy_application_path) contents = File.read(dummy_application_path) - contents[(contents.index("module Dummy"))..-1] + contents[(contents.index(/module ([\w]+)\n(.*)class Application/m))..-1] end end end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec index 8588e88077..568ed653b7 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec +++ b/railties/lib/rails/generators/rails/plugin_new/templates/%name%.gemspec @@ -13,7 +13,7 @@ Gem::Specification.new do |s| s.summary = "TODO: Summary of <%= camelized %>." s.description = "TODO: Description of <%= camelized %>." - s.files = Dir["{app,config,db,lib}/**/*"] + ["MIT-LICENSE", "Rakefile", "README.rdoc"] + s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] <% unless options.skip_test_unit? -%> s.test_files = Dir["test/**/*"] <% end -%> @@ -22,6 +22,8 @@ Gem::Specification.new do |s| <% if full? && !options[:skip_javascript] -%> # s.add_dependency "<%= "#{options[:javascript]}-rails" %>" <% end -%> +<% unless options[:skip_active_record] -%> s.add_development_dependency "<%= gem_for_database %>" +<% end -%> end diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile index d316b00c43..7448b386c5 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile +++ b/railties/lib/rails/generators/rails/plugin_new/templates/Gemfile @@ -1,17 +1,32 @@ source "http://rubygems.org" +<% if options[:skip_gemspec] -%> +<%= '# ' if options.dev? || options.edge? -%>gem "rails", "~> <%= Rails::VERSION::STRING %>" +<% if full? && !options[:skip_javascript] -%> +# gem "<%= "#{options[:javascript]}-rails" %>" +<% end -%> +<% else -%> # Declare your gem's dependencies in <%= name %>.gemspec. # Bundler will treat runtime dependencies like base dependencies, and # development dependencies will be added by default to the :development group. gemspec +<% end -%> +<% unless options[:javascript] == 'jquery' -%> # jquery-rails is used by the dummy application gem "jquery-rails" +<% end -%> +<% if options[:skip_gemspec] -%> +group :development do + gem "<%= gem_for_database %>" +end +<% else -%> # Declare any dependencies that are still in development here instead of in # your gemspec. These might include edge Rails or gems from your path or # Git. Remember to move these dependencies to your gemspec before releasing # your gem to rubygems.org. +<% end -%> <% if options.dev? || options.edge? -%> # Your gem is dependent on dev or edge Rails. Once you can lock this @@ -20,4 +35,4 @@ gem "jquery-rails" <% end -%> # To use debugger -# gem 'ruby-debug19', :require => 'ruby-debug' +# gem 'debugger' diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile index 6ed6adcf1b..1369140537 100755..100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile +++ b/railties/lib/rails/generators/rails/plugin_new/templates/Rakefile @@ -1,16 +1,10 @@ -#!/usr/bin/env rake begin require 'bundler/setup' rescue LoadError puts 'You must `gem install bundler` and `bundle install` to run rake tasks' end -begin - require 'rdoc/task' -rescue LoadError - require 'rdoc/rdoc' - require 'rake/rdoctask' - RDoc::Task = Rake::RDocTask -end + +require 'rdoc/task' RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'rdoc' @@ -20,7 +14,7 @@ RDoc::Task.new(:rdoc) do |rdoc| rdoc.rdoc_files.include('lib/**/*.rb') end -<% if full? && !options[:skip_active_record] -%> +<% if full? && !options[:skip_active_record] && !options[:skip_test_unit] -%> APP_RAKEFILE = File.expand_path("../<%= dummy_path -%>/Rakefile", __FILE__) load 'rails/tasks/engine.rake' <% end %> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/gitignore b/railties/lib/rails/generators/rails/plugin_new/templates/gitignore index 458b2c662e..086d87818a 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/gitignore +++ b/railties/lib/rails/generators/rails/plugin_new/templates/gitignore @@ -1,8 +1,10 @@ .bundle/ log/*.log pkg/ +<% unless options[:skip_test_unit] && options[:dummy_path] == 'test/dummy' -%> <%= dummy_path %>/db/*.sqlite3 <%= dummy_path %>/db/*.sqlite3-journal <%= dummy_path %>/log/*.log <%= dummy_path %>/tmp/ <%= dummy_path %>/.sass-cache +<% end -%>
\ No newline at end of file diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb index 996ea79e67..2f9b7fc962 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb +++ b/railties/lib/rails/generators/rails/plugin_new/templates/rails/application.rb @@ -7,8 +7,7 @@ require 'rails/all' <%= comment_if :skip_active_record %>require "active_record/railtie" require "action_controller/railtie" require "action_mailer/railtie" -require "active_resource/railtie" -<%= comment_if :skip_sprockets %>require "sprockets/railtie" +<%= comment_if :skip_sprockets %>require "sprockets/rails/railtie" <%= comment_if :skip_test_unit %>require "rails/test_unit/railtie" <% end -%> diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb b/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb index eba0681370..c78bfb7f63 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb +++ b/railties/lib/rails/generators/rails/plugin_new/templates/rails/boot.rb @@ -1,4 +1,3 @@ -require 'rubygems' gemfile = File.expand_path('../../../../Gemfile', __FILE__) if File.exist?(gemfile) diff --git a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb b/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb index dcd3b276e3..1e26a313cd 100644 --- a/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb +++ b/railties/lib/rails/generators/rails/plugin_new/templates/test/test_helper.rb @@ -8,3 +8,8 @@ Rails.backtrace_cleaner.remove_silencers! # Load support files Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } + +# Load fixtures from the engine +if ActiveSupport::TestCase.method_defined?(:fixture_path=) + ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) +end diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb index c7345f3cfb..3a0586ee43 100644 --- a/railties/lib/rails/generators/rails/resource/resource_generator.rb +++ b/railties/lib/rails/generators/rails/resource/resource_generator.rb @@ -14,13 +14,7 @@ module Rails class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [], :desc => "Actions for the resource controller" - def add_resource_route - return if options[:actions].present? - route_config = regular_class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ") - route_config << "resources :#{file_name.pluralize}" - route_config << " end" * regular_class_path.size - route route_config - end + hook_for :resource_route, :required => true end end end diff --git a/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb new file mode 100644 index 0000000000..6a5d62803c --- /dev/null +++ b/railties/lib/rails/generators/rails/resource_route/resource_route_generator.rb @@ -0,0 +1,13 @@ +module Rails + module Generators + class ResourceRouteGenerator < NamedBase + def add_resource_route + return if options[:actions].present? + route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ") + route_config << "resources :#{file_name.pluralize}" + route_config << " end" * regular_class_path.size + route route_config + end + end + end +end diff --git a/railties/lib/rails/generators/rails/scaffold_controller/USAGE b/railties/lib/rails/generators/rails/scaffold_controller/USAGE index 673f69bc81..5cd51b62d4 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/USAGE +++ b/railties/lib/rails/generators/rails/scaffold_controller/USAGE @@ -1,8 +1,7 @@ Description: - Stubs out a scaffolded controller and its views. Pass the model name, - either CamelCased or under_scored, and a list of views as arguments. - The controller name is retrieved as a pluralized version of the model - name. + Stubs out a scaffolded controller, its seven RESTful actions and related + views. Pass the model name, either CamelCased or under_scored. The + controller name is retrieved as a pluralized version of the model name. To create a controller within a module, specify the model name as a path like 'parent_module/controller_name'. diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb index 2271c6f9c1..0618b16984 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb @@ -11,7 +11,7 @@ module Rails :desc => "ORM to generate the controller for" def create_controller_files - template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") + template "controller.rb", File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb") end hook_for :template_engine, :test_framework, :as => :scaffold diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb index 4ff15fd288..b3e74f9b02 100644 --- a/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb +++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/controller.rb @@ -1,3 +1,7 @@ +<% if namespaced? -%> +require_dependency "<%= namespaced_file_path %>/application_controller" + +<% end -%> <% module_namespacing do -%> class <%= controller_class_name %>Controller < ApplicationController # GET <%= route_url %> @@ -7,7 +11,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # index.html.erb - format.json { render <%= key_value :json, "@#{plural_table_name}" %> } + format.json { render json: <%= "@#{plural_table_name}" %> } end end @@ -18,7 +22,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # show.html.erb - format.json { render <%= key_value :json, "@#{singular_table_name}" %> } + format.json { render json: <%= "@#{singular_table_name}" %> } end end @@ -29,7 +33,7 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| format.html # new.html.erb - format.json { render <%= key_value :json, "@#{singular_table_name}" %> } + format.json { render json: <%= "@#{singular_table_name}" %> } end end @@ -45,27 +49,27 @@ class <%= controller_class_name %>Controller < ApplicationController respond_to do |format| if @<%= orm_instance.save %> - format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully created.'" %> } - format.json { render <%= key_value :json, "@#{singular_table_name}" %>, <%= key_value :status, ':created' %>, <%= key_value :location, "@#{singular_table_name}" %> } + format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully created.'" %> } + format.json { render json: <%= "@#{singular_table_name}" %>, status: :created, location: <%= "@#{singular_table_name}" %> } else - format.html { render <%= key_value :action, '"new"' %> } - format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } + format.html { render action: "new" } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end end - # PUT <%= route_url %>/1 - # PUT <%= route_url %>/1.json + # PATCH/PUT <%= route_url %>/1 + # PATCH/PUT <%= route_url %>/1.json def update @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> respond_to do |format| if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %> - format.html { redirect_to @<%= singular_table_name %>, <%= key_value :notice, "'#{human_name} was successfully updated.'" %> } + format.html { redirect_to @<%= singular_table_name %>, notice: <%= "'#{human_name} was successfully updated.'" %> } format.json { head :no_content } else - format.html { render <%= key_value :action, '"edit"' %> } - format.json { render <%= key_value :json, "@#{orm_instance.errors}" %>, <%= key_value :status, ':unprocessable_entity' %> } + format.html { render action: "edit" } + format.json { render json: <%= "@#{orm_instance.errors}" %>, status: :unprocessable_entity } end end end diff --git a/railties/lib/rails/generators/rails/session_migration/USAGE b/railties/lib/rails/generators/rails/session_migration/USAGE deleted file mode 100644 index 564d1ffd78..0000000000 --- a/railties/lib/rails/generators/rails/session_migration/USAGE +++ /dev/null @@ -1,8 +0,0 @@ -Description: - Creates a migration to add the sessions table used by the ORM session store. - Pass the migration name, either CamelCased or under_scored, as an argument. - - Before invoking this generator, be sure that your ORM supports session stores. - -Example: - `rails generate session_migration CreateSessionTable` diff --git a/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb b/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb deleted file mode 100644 index 258cc5b4a0..0000000000 --- a/railties/lib/rails/generators/rails/session_migration/session_migration_generator.rb +++ /dev/null @@ -1,8 +0,0 @@ -module Rails - module Generators - class SessionMigrationGenerator < NamedBase #metagenerator - argument :name, :type => :string, :default => "add_sessions_table" - hook_for :orm, :required => true - end - end -end diff --git a/railties/lib/rails/generators/resource_helpers.rb b/railties/lib/rails/generators/resource_helpers.rb index 3c5b39fa16..48833869e5 100644 --- a/railties/lib/rails/generators/resource_helpers.rb +++ b/railties/lib/rails/generators/resource_helpers.rb @@ -50,7 +50,7 @@ module Rails end def controller_i18n_scope - @controller_i18n_scope ||= controller_file_path.gsub('/', '.') + @controller_i18n_scope ||= controller_file_path.tr('/', '.') end # Loads the ORM::Generators::ActiveModel class. This class is responsible diff --git a/railties/lib/rails/generators/test_case.rb b/railties/lib/rails/generators/test_case.rb index d81c4c3e1d..2ff340755a 100644 --- a/railties/lib/rails/generators/test_case.rb +++ b/railties/lib/rails/generators/test_case.rb @@ -31,15 +31,22 @@ module Rails include FileUtils class_attribute :destination_root, :current_path, :generator_class, :default_arguments - delegate :destination_root, :current_path, :generator_class, :default_arguments, :to => :'self.class' # Generators frequently change the current path using +FileUtils.cd+. # So we need to store the path at file load and revert back to it after each test. self.current_path = File.expand_path(Dir.pwd) self.default_arguments = [] - setup :destination_root_is_set?, :ensure_current_path - teardown :ensure_current_path + def setup + destination_root_is_set? + ensure_current_path + super + end + + def teardown + ensure_current_path + super + end # Sets which generator should be tested: # @@ -79,8 +86,8 @@ module Rails # # Finally, when a block is given, it yields the file content: # - # assert_file "app/controller/products_controller.rb" do |controller| - # assert_instance_method :index, content do |index| + # assert_file "app/controllers/products_controller.rb" do |controller| + # assert_instance_method :index, controller do |index| # assert_match(/Product\.all/, index) # end # end @@ -135,7 +142,7 @@ module Rails # Asserts a given migration does not exist. You need to supply an absolute path or a # path relative to the configured destination: # - # assert_no_file "config/random.rb" + # assert_no_migration "db/migrate/create_products.rb" # def assert_no_migration(relative) file_name = migration_file_name(relative) @@ -159,8 +166,8 @@ module Rails # Asserts the given method exists in the given content. When a block is given, # it yields the content of the method. # - # assert_file "app/controller/products_controller.rb" do |controller| - # assert_instance_method :index, content do |index| + # assert_file "app/controllers/products_controller.rb" do |controller| + # assert_instance_method :index, controller do |index| # assert_match(/Product\.all/, index) # end # end @@ -182,7 +189,7 @@ module Rails # Asserts the given attribute type gets a proper default value: # - # assert_field_type :string, "MyString" + # assert_field_default_value :string, "MyString" # def assert_field_default_value(attribute_type, value) assert_equal(value, create_generated_attribute(attribute_type).default) diff --git a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb index d296b26b16..2f25dcf832 100644 --- a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb +++ b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb @@ -3,10 +3,10 @@ require 'rails/performance_test_help' class <%= class_name %>Test < ActionDispatch::PerformanceTest # Refer to the documentation for all available options - # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] + # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory], # :output => 'tmp/performance', :formats => [:flat] } - def test_homepage + test "homepage" do get '/' end end diff --git a/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb b/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb index e82e321914..c9af2ca832 100644 --- a/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb +++ b/railties/lib/rails/generators/test_unit/plugin/templates/test_helper.rb @@ -1,3 +1,2 @@ -require 'rubygems' require 'minitest/autorun' require 'active_support' diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb index f7e907a017..ca7fee3b6e 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb @@ -8,10 +8,27 @@ module TestUnit check_class_collision :suffix => "ControllerTest" + argument :attributes, :type => :array, :default => [], :banner => "field:type field:type" + def create_test_files - template 'functional_test.rb', - File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb") + template "functional_test.rb", + File.join("test/functional", controller_class_path, "#{controller_file_name}_controller_test.rb") end + + private + + def attributes_hash + return if accessible_attributes.empty? + + accessible_attributes.map do |a| + name = a.name + "#{name}: @#{singular_table_name}.#{name}" + end.sort.join(', ') + end + + def accessible_attributes + attributes.reject(&:reference?) + end end end end diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb index 9ec2e34545..30e1650555 100644 --- a/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb +++ b/railties/lib/rails/generators/test_unit/scaffold/templates/functional_test.rb @@ -19,30 +19,30 @@ class <%= controller_class_name %>ControllerTest < ActionController::TestCase test "should create <%= singular_table_name %>" do assert_difference('<%= class_name %>.count') do - post :create, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> + post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %> end assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should show <%= singular_table_name %>" do - get :show, <%= key_value :id, "@#{singular_table_name}" %> + get :show, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should get edit" do - get :edit, <%= key_value :id, "@#{singular_table_name}" %> + get :edit, id: <%= "@#{singular_table_name}" %> assert_response :success end test "should update <%= singular_table_name %>" do - put :update, <%= key_value :id, "@#{singular_table_name}" %>, <%= key_value singular_table_name, "@#{singular_table_name}.attributes" %> + put :update, id: <%= "@#{singular_table_name}" %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %> assert_redirected_to <%= singular_table_name %>_path(assigns(:<%= singular_table_name %>)) end test "should destroy <%= singular_table_name %>" do assert_difference('<%= class_name %>.count', -1) do - delete :destroy, <%= key_value :id, "@#{singular_table_name}" %> + delete :destroy, id: <%= "@#{singular_table_name}" %> end assert_redirected_to <%= index_helper %>_path |