aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/generators.rb64
-rw-r--r--railties/lib/generators/active_model.rb (renamed from railties/lib/generators/action_orm.rb)14
-rw-r--r--railties/lib/generators/active_record.rb4
-rw-r--r--railties/lib/generators/named_base.rb10
4 files changed, 57 insertions, 35 deletions
diff --git a/railties/lib/generators.rb b/railties/lib/generators.rb
index ef837e1488..c97c61507a 100644
--- a/railties/lib/generators.rb
+++ b/railties/lib/generators.rb
@@ -83,6 +83,34 @@ module Rails
@@options ||= DEFAULT_OPTIONS.dup
end
+ # Get paths only from loaded rubygems. In other words, to use rspec
+ # generators, you first have to ensure that rspec gem was already loaded.
+ #
+ def self.rubygems_generators_paths
+ paths = []
+ return paths unless defined?(Gem)
+
+ Gem.loaded_specs.each do |name, spec|
+ generator_path = File.join(spec.full_gem_path, "lib/generators")
+ paths << generator_path if File.exist?(generator_path)
+ end
+
+ paths
+ end
+
+ # If RAILS_ROOT is defined, add vendor/gems, vendor/plugins and lib/generators
+ # paths.
+ #
+ def self.rails_root_generators_paths
+ paths = []
+ if defined?(RAILS_ROOT)
+ paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "gems", "*", "lib", "generators")]
+ paths += Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
+ paths << File.join(RAILS_ROOT, "lib", "generators")
+ end
+ paths
+ end
+
# Hold configured generators fallbacks. If a plugin developer wants a
# generator group to fallback to another group in case of missing generators,
# they can add a fallback.
@@ -108,30 +136,25 @@ module Rails
# Generators load paths used on lookup. The lookup happens as:
#
- # 1) builtin generators
- # 2) frozen gems generators
- # 3) rubygems gems generators (not available yet)
- # 4) plugin generators
- # 5) lib generators
- # 6) ~/rails/generators
+ # 1) lib generators
+ # 2) vendor/plugin generators
+ # 3) vendor/gems generators
+ # 4) ~/rails/generators
+ # 5) rubygems generators
+ # 6) builtin generators
#
- # TODO Add Rubygems generators (depends on dependencies system rework)
# TODO Remove hardcoded paths for all, except (1).
#
- def self.load_path
- @@load_path ||= begin
- paths = []
- paths << File.expand_path(File.join(File.dirname(__FILE__), "generators"))
- if defined?(RAILS_ROOT)
- paths += Dir[File.join(RAILS_ROOT, "vendor", "gems", "*", "lib", "generators")]
- paths += Dir[File.join(RAILS_ROOT, "vendor", "plugins", "*", "lib", "generators")]
- paths << File.join(RAILS_ROOT, "lib", "generators")
- end
+ def self.load_paths
+ @@load_paths ||= begin
+ paths = self.rails_root_generators_paths
paths << File.join(Thor::Util.user_home, ".rails", "generators")
+ paths += self.rubygems_generators_paths
+ paths << File.expand_path(File.join(File.dirname(__FILE__), "generators"))
paths
end
end
- load_path # Cache load paths. Needed to avoid __FILE__ pointing to wrong paths.
+ load_paths # Cache load paths. Needed to avoid __FILE__ pointing to wrong paths.
# Receives a namespace and tries different combinations to find a generator.
#
@@ -204,8 +227,8 @@ module Rails
puts "Builtin: #{rails.join(', ')}."
# Load paths and remove builtin
- paths, others = load_path.dup, []
- paths.shift
+ paths, others = load_paths.dup, []
+ paths.pop
paths.each do |path|
tail = [ "*", "*", "*_generator.rb" ]
@@ -242,7 +265,6 @@ module Rails
#
def self.invoke_fallbacks_for(name, base)
return nil unless base && fallbacks[base.to_sym]
-
invoked_fallbacks = []
Array(fallbacks[base.to_sym]).each do |fallback|
@@ -283,7 +305,7 @@ module Rails
generators_path.uniq!
generators_path = "{#{generators_path.join(',')}}"
- self.load_path.each do |path|
+ self.load_paths.each do |path|
Dir[File.join(path, generators_path, name)].each do |file|
begin
require file
diff --git a/railties/lib/generators/action_orm.rb b/railties/lib/generators/active_model.rb
index 69cf227fd7..1a849a0e02 100644
--- a/railties/lib/generators/action_orm.rb
+++ b/railties/lib/generators/active_model.rb
@@ -1,6 +1,6 @@
module Rails
module Generators
- # ActionORM is a class to be implemented by each ORM to allow Rails to
+ # ActiveModel is a class to be implemented by each ORM to allow Rails to
# generate customized controller code.
#
# The API has the same methods as ActiveRecord, but each method returns a
@@ -8,22 +8,22 @@ module Rails
#
# For example:
#
- # ActiveRecord::Generators::ActionORM.find(Foo, "params[:id]")
+ # ActiveRecord::Generators::ActiveModel.find(Foo, "params[:id]")
# #=> "Foo.find(params[:id])"
#
- # Datamapper::Generators::ActionORM.find(Foo, "params[:id]")
+ # Datamapper::Generators::ActiveModel.find(Foo, "params[:id]")
# #=> "Foo.get(params[:id])"
#
- # On initialization, the ActionORM accepts the instance name that will
+ # On initialization, the ActiveModel accepts the instance name that will
# receive the calls:
#
- # builder = ActiveRecord::Generators::ActionORM.new "@foo"
+ # builder = ActiveRecord::Generators::ActiveModel.new "@foo"
# builder.save #=> "@foo.save"
#
- # The only exception in ActionORM for ActiveRecord is the use of self.build
+ # The only exception in ActiveModel for ActiveRecord is the use of self.build
# instead of self.new.
#
- class ActionORM
+ class ActiveModel
attr_reader :name
def initialize(name)
diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb
index 64bee3904e..924b70881a 100644
--- a/railties/lib/generators/active_record.rb
+++ b/railties/lib/generators/active_record.rb
@@ -1,6 +1,6 @@
require 'generators/named_base'
require 'generators/migration'
-require 'generators/action_orm'
+require 'generators/active_model'
require 'active_record'
module ActiveRecord
@@ -20,7 +20,7 @@ module ActiveRecord
end
end
- class ActionORM < Rails::Generators::ActionORM #:nodoc:
+ class ActiveModel < Rails::Generators::ActiveModel #:nodoc:
def self.all(klass)
"#{klass}.all"
end
diff --git a/railties/lib/generators/named_base.rb b/railties/lib/generators/named_base.rb
index 699b8ed651..9632e6806c 100644
--- a/railties/lib/generators/named_base.rb
+++ b/railties/lib/generators/named_base.rb
@@ -124,18 +124,18 @@ module Rails
protected
- # Loads the ORM::Generators::ActionORM class. This class is responsable
+ # Loads the ORM::Generators::ActiveModel class. This class is responsable
# to tell scaffold entities how to generate an specific method for the
- # ORM. Check Rails::Generators::ActionORM for more information.
+ # ORM. Check Rails::Generators::ActiveModel for more information.
#
def orm_class
@orm_class ||= begin
# Raise an error if the class_option :orm was not defined.
unless self.class.class_options[:orm]
- raise "You need to have :orm as class option to invoke orm_class and orm_instance"
+ raise "You need to have :orm as class option to invoke orm_class and orm_instance"
end
- action_orm = "#{options[:orm].to_s.classify}::Generators::ActionORM"
+ action_orm = "#{options[:orm].to_s.classify}::Generators::ActiveModel"
# If the orm was not loaded, try to load it at "generators/orm",
# for example "generators/active_record" or "generators/sequel".
@@ -152,7 +152,7 @@ module Rails
end
end
- # Initialize ORM::Generators::ActionORM to access instance methods.
+ # Initialize ORM::Generators::ActiveModel to access instance methods.
#
def orm_instance(name=file_name)
@orm_instance ||= @orm_class.new(name)