Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Provide a cleaner syntax for paths configuration that does not rely o…

…n method_missing.
  • Loading branch information...
commit d649bf158be130515566aed987f83d36ac9b0ae8 1 parent d40ca9c
@josevalim josevalim authored
View
6 actionmailer/lib/action_mailer/railtie.rb
@@ -14,9 +14,9 @@ class Railtie < Rails::Railtie
paths = app.config.paths
options = app.config.action_mailer
- options.assets_dir ||= paths.public.to_a.first
- options.javascripts_dir ||= paths.public.javascripts.to_a.first
- options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
+ options.assets_dir ||= paths["public"].first
+ options.javascripts_dir ||= paths["public/javascripts"].first
+ options.stylesheets_dir ||= paths["public/stylesheets"].first
# make sure readers methods get compiled
options.asset_path ||= nil
View
8 actionpack/lib/action_controller/railtie.rb
@@ -21,10 +21,10 @@ class Railtie < Rails::Railtie
paths = app.config.paths
options = app.config.action_controller
- options.assets_dir ||= paths.public.to_a.first
- options.javascripts_dir ||= paths.public.javascripts.to_a.first
- options.stylesheets_dir ||= paths.public.stylesheets.to_a.first
- options.page_cache_directory ||= paths.public.to_a.first
+ options.assets_dir ||= paths["public"].first
+ options.javascripts_dir ||= paths["public/javascripts"].first
+ options.stylesheets_dir ||= paths["public/stylesheets"].first
+ options.page_cache_directory ||= paths["public"].first
# make sure readers methods get compiled
options.asset_path ||= nil
View
6 actionpack/lib/action_controller/railties/paths.rb
@@ -5,12 +5,14 @@ def self.with(app)
Module.new do
define_method(:inherited) do |klass|
super(klass)
+
if namespace = klass.parents.detect {|m| m.respond_to?(:_railtie) }
- klass.helpers_path = namespace._railtie.config.paths.app.helpers.to_a
+ paths = namespace._railtie.paths["app/helpers"].existent
else
- klass.helpers_path = app.config.helpers_paths
+ paths = app.config.helpers_paths
end
+ klass.helpers_path = paths
klass.helper :all if klass.superclass == ActionController::Base
end
end
View
6 activerecord/lib/active_record/railties/databases.rake
@@ -2,7 +2,7 @@ namespace :db do
task :load_config => :rails_env do
require 'active_record'
ActiveRecord::Base.configurations = Rails.application.config.database_configuration
- ActiveRecord::Migrator.migrations_path = Rails.application.config.paths.db.migrate.to_a.first
+ ActiveRecord::Migrator.migrations_path = Rails.application.paths["db/migrate"].first
end
task :copy_migrations => :load_config do
@@ -11,8 +11,8 @@ namespace :db do
Rails.application.railties.all do |railtie|
next unless to_load == :all || to_load.include?(railtie.railtie_name)
- if railtie.config.respond_to?(:paths) && railtie.config.paths.db
- railties[railtie.railtie_name] = railtie.config.paths.db.migrate.to_a.first
+ if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
+ railties[railtie.railtie_name] = path
end
end
View
2  railties/lib/rails.rb
@@ -92,7 +92,7 @@ def version
end
def public_path
- application && application.paths.public.to_a.first
+ application && application.paths["public"].first
end
end
end
View
2  railties/lib/rails/application.rb
@@ -72,7 +72,7 @@ def add_lib_to_load_path! #:nodoc:
end
def require_environment! #:nodoc:
- environment = paths.config.environment.to_a.first
+ environment = paths["config/environment"].existent.first
require environment if environment
end
View
2  railties/lib/rails/application/bootstrap.rb
@@ -23,7 +23,7 @@ module Bootstrap
# Initialize the logger early in the stack in case we need to log some deprecation.
initializer :initialize_logger do
Rails.logger ||= config.logger || begin
- path = config.paths.log.to_a.first
+ path = config.paths["log"].first
logger = ActiveSupport::BufferedLogger.new(path)
logger.level = ActiveSupport::BufferedLogger.const_get(config.log_level.to_s.upcase)
logger.auto_flushing = false if Rails.env.production?
View
18 railties/lib/rails/application/configuration.rb
@@ -9,7 +9,7 @@ class Configuration < ::Rails::Engine::Configuration
:filter_parameters, :log_level, :logger,
:preload_frameworks, :reload_plugins,
:secret_token, :serve_static_assets, :session_options,
- :time_zone, :whiny_nils
+ :time_zone, :whiny_nils, :helpers_paths
def initialize(*)
super
@@ -17,6 +17,7 @@ def initialize(*)
@allow_concurrency = false
@consider_all_requests_local = false
@filter_parameters = []
+ @helpers_paths = []
@dependency_loading = true
@serve_static_assets = true
@session_store = :cookie_store
@@ -60,13 +61,12 @@ def encoding=(value)
def paths
@paths ||= begin
paths = super
- paths.config.database "config/database.yml"
- paths.config.environment "config/environment.rb"
- paths.lib.templates "lib/templates"
- paths.log "log/#{Rails.env}.log"
- paths.tmp "tmp"
- paths.tmp.cache "tmp/cache"
-
+ paths.add "config/database", :with => "config/database.yml"
+ paths.add "config/environment", :with => "config/environment.rb"
+ paths.add "lib/templates"
+ paths.add "log", :with => "log/#{Rails.env}.log"
+ paths.add "tmp"
+ paths.add "tmp/cache"
paths
end
end
@@ -88,7 +88,7 @@ def threadsafe!
# YAML::load.
def database_configuration
require 'erb'
- YAML::load(ERB.new(IO.read(paths.config.database.to_a.first)).result)
+ YAML::load(ERB.new(IO.read(paths["config/database"].first)).result)
end
def cache_store
View
2  railties/lib/rails/application/finisher.rb
@@ -4,7 +4,7 @@ module Finisher
include Initializable
initializer :add_generator_templates do
- config.generators.templates.unshift(*paths.lib.templates.to_a)
+ config.generators.templates.unshift(*paths["lib/templates"].existent)
end
initializer :ensure_autoload_once_paths_as_subset do
View
59 railties/lib/rails/engine.rb
@@ -90,17 +90,17 @@ module Rails
# The available paths in an Engine are:
#
# class MyEngine < Rails::Engine
- # paths.app = "app"
- # paths.app.controllers = "app/controllers"
- # paths.app.helpers = "app/helpers"
- # paths.app.models = "app/models"
- # paths.app.views = "app/views"
- # paths.lib = "lib"
- # paths.lib.tasks = "lib/tasks"
- # paths.config = "config"
- # paths.config.initializers = "config/initializers"
- # paths.config.locales = "config/locales"
- # paths.config.routes = "config/routes.rb"
+ # paths["app"] #=> ["app"]
+ # paths["app/controllers"] #=> ["app/controllers"]
+ # paths["app/helpers"] #=> ["app/helpers"]
+ # paths["app/models"] #=> ["app/models"]
+ # paths["app/views"] #=> ["app/views"]
+ # paths["lib"] #=> ["lib"]
+ # paths["lib/tasks"] #=> ["lib/tasks"]
+ # paths["config"] #=> ["config"]
+ # paths["config/initializers"] #=> ["config/initializers"]
+ # paths["config/locales"] #=> ["config/locales"]
+ # paths["config/routes"] #=> ["config/routes.rb"]
# end
#
# Your Application class adds a couple more paths to this set. And as in your Application,
@@ -276,11 +276,11 @@ module Rails
# end
# end
#
- # There is also 'app' helper that gives you access to application's routes inside Engine:
+ # There is also 'main_app' helper that gives you access to application's routes inside Engine:
#
# module MyEngine
# class BarController
- # app.foo_path #=> /foo
+ # main_app.foo_path #=> /foo
# end
# end
#
@@ -381,7 +381,7 @@ def namespaced?
def load_tasks
super
- config.paths.lib.tasks.to_a.sort.each { |ext| load(ext) }
+ paths["lib/tasks"].existent.sort.each { |ext| load(ext) }
end
def eager_load!
@@ -441,7 +441,7 @@ def config
#
# Blog::Engine.load_seed
def load_seed
- seed_file = config.paths.db.seeds.to_a.first
+ seed_file = paths["db/seeds"].existent.first
load(seed_file) if File.exist?(seed_file)
end
@@ -469,20 +469,22 @@ def load_seed
end
initializer :add_routing_paths do |app|
- app.routes_reloader.blocks[routes] = routes_draw_block
- paths.config.routes.to_a.each do |route|
- app.routes_reloader.paths.unshift(route) if File.exists?(route)
+ paths = self.paths["config/routes"].existent
+
+ if routes? || paths.any?
+ app.routes_reloader.blocks[routes] = routes_draw_block
+ app.routes_reloader.paths.unshift(*paths)
end
end
# I18n load paths are a special case since the ones added
# later have higher priority.
initializer :add_locales do
- config.i18n.railties_load_path.concat(paths.config.locales.to_a)
+ config.i18n.railties_load_path.concat(paths["config/locales"].existent)
end
initializer :add_view_paths do
- views = paths.app.views.to_a
+ views = paths["app/views"].existent
unless views.empty?
ActiveSupport.on_load(:action_controller){ prepend_view_path(views) }
ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) }
@@ -490,28 +492,27 @@ def load_seed
end
initializer :load_environment_config, :before => :load_environment_hook do
- environment = config.paths.config.environments.to_a.first
+ environment = paths["config/environments"].existent.first
require environment if environment
end
initializer :append_asset_paths do
config.asset_path ||= "/#{engine_name}%s"
- public_path = config.paths.public.to_a.first
+ public_path = paths["public"].first
if config.compiled_asset_path && File.exist?(public_path)
config.static_asset_paths[config.compiled_asset_path] = public_path
end
end
- initializer :prepend_helpers_path do
- unless namespaced?
- config.helpers_paths = [] unless config.respond_to?(:helpers_paths)
- config.helpers_paths = config.paths.app.helpers.to_a + config.helpers_paths
+ initializer :prepend_helpers_path do |app|
+ if !namespaced? || (app == self)
+ app.config.helpers_paths.unshift(*paths["app/helpers"].existent)
end
end
initializer :load_config_initializers do
- paths.config.initializers.to_a.sort.each do |initializer|
+ config.paths["config/initializers"].existent.sort.each do |initializer|
load(initializer)
end
end
@@ -524,6 +525,10 @@ def load_seed
protected
attr_accessor :routes_draw_block
+ def routes?
+ defined?(@routes)
+ end
+
def find_root_with_flag(flag, default=nil)
root_path = self.class.called_from
View
43 railties/lib/rails/engine/configuration.rb
@@ -10,7 +10,6 @@ class Configuration < ::Rails::Railtie::Configuration
def initialize(root=nil)
super()
@root = root
- @helpers_paths = []
end
# Returns the middleware stack for the engine.
@@ -39,27 +38,27 @@ def generators #:nodoc
def paths
@paths ||= begin
paths = Rails::Paths::Root.new(@root)
- paths.app "app", :eager_load => true, :glob => "*"
- paths.app.controllers "app/controllers", :eager_load => true
- paths.app.helpers "app/helpers", :eager_load => true
- paths.app.models "app/models", :eager_load => true
- paths.app.mailers "app/mailers", :eager_load => true
- paths.app.views "app/views"
- paths.lib "lib", :load_path => true
- paths.lib.tasks "lib/tasks", :glob => "**/*.rake"
- paths.config "config"
- paths.config.initializers "config/initializers", :glob => "**/*.rb"
- paths.config.locales "config/locales", :glob => "*.{rb,yml}"
- paths.config.routes "config/routes.rb"
- paths.config.environments "config/environments", :glob => "#{Rails.env}.rb"
- paths.public "public"
- paths.public.javascripts "public/javascripts"
- paths.public.stylesheets "public/stylesheets"
- paths.vendor "vendor", :load_path => true
- paths.vendor.plugins "vendor/plugins"
- paths.db "db"
- paths.db.migrate "db/migrate"
- paths.db.seeds "db/seeds.rb"
+ paths.add "app", :eager_load => true, :glob => "*"
+ paths.add "app/controllers", :eager_load => true
+ paths.add "app/helpers", :eager_load => true
+ paths.add "app/models", :eager_load => true
+ paths.add "app/mailers", :eager_load => true
+ paths.add "app/views"
+ paths.add "lib", :load_path => true
+ paths.add "lib/tasks", :glob => "**/*.rake"
+ paths.add "config"
+ paths.add "config/environments", :glob => "#{Rails.env}.rb"
+ paths.add "config/initializers", :glob => "**/*.rb"
+ paths.add "config/locales", :glob => "*.{rb,yml}"
+ paths.add "config/routes", :with => "config/routes.rb"
+ paths.add "db"
+ paths.add "db/migrate"
+ paths.add "db/seeds", :with => "db/seeds.rb"
+ paths.add "public"
+ paths.add "public/javascripts"
+ paths.add "public/stylesheets"
+ paths.add "vendor", :load_path => true
+ paths.add "vendor/plugins"
paths
end
end
View
2  railties/lib/rails/engine/railties.rb
@@ -15,7 +15,7 @@ def all(&block)
def plugins
@plugins ||= begin
plugin_names = (@config.plugins || [:all]).map { |p| p.to_sym }
- Plugin.all(plugin_names, @config.paths.vendor.plugins)
+ Plugin.all(plugin_names, @config.paths["vendor/plugins"].existent)
end
end
end
View
152 railties/lib/rails/paths.rb
@@ -2,15 +2,17 @@
module Rails
module Paths
- module PathParent
- attr_reader :children
-
+ module PathParent #:nodoc:
def method_missing(id, *args)
- name = id.to_s
+ match = id.to_s.match(/^(.*)=$/)
+ full = [@current, $1 || id].compact.join("/")
+
+ ActiveSupport::Deprecation.warn 'config.paths.app.controller API is deprecated in ' <<
+ 'favor of config.paths["app/controller"] API.'
- if name =~ /^(.*)=$/ || args.any?
- @children[$1 || name] = Path.new(@root, *args)
- elsif path = @children[name]
+ if match || args.any?
+ @root[full] = Path.new(@root, full, *args)
+ elsif path = @root[full]
path
else
super
@@ -18,22 +20,72 @@ def method_missing(id, *args)
end
end
- class Root
+ # This object is an extended hash that behaves as root of the Rails::Paths system.
+ # It allows you to collect information about how you want to structure your application
+ # paths by a Hash like API. It requires you to give a physical path on initialization.
+ #
+ # root = Root.new
+ # root.add "app/controllers", :eager_load => true
+ #
+ # The command above creates a new root object and add "app/controllers" as a path.
+ # This means we can get a Path object back like below:
+ #
+ # path = root["app/controllers"]
+ # path.eager_load? #=> true
+ # path.is_a?(Rails::Paths::Path) #=> true
+ #
+ # The Path object is simply an array and allows you to easily add extra paths:
+ #
+ # path.is_a?(Array) #=> true
+ # path.inspect #=> ["app/controllers"]
+ #
+ # path << "lib/controllers"
+ # path.inspect #=> ["app/controllers", "lib/controllers"]
+ #
+ # Notice that when you add a path using #add, the path object created already
+ # contains te path with the same path value given to #add. In some situations,
+ # you may not want this behavior, so you can give :with as option.
+ #
+ # root.add "config/routes", :with => "config/routes.rb"
+ # root["config/routes"].inspect #=> ["config/routes.rb"]
+ #
+ # #add also accepts the following options as argument: eager_load, autoload,
+ # autoload_once and glob.
+ #
+ # Finally, the Path object also provides a few helpers:
+ #
+ # root = Root.new
+ # root.path = "/rails"
+ # root.add "app/controllers"
+ #
+ # root["app/controllers"].expanded #=> ["/rails/app/controllers"]
+ # root["app/controllers"].existent #=> ["/rails/app/controllers"]
+ #
+ # Check the Path documentation for more information.
+ class Root < ::Hash
include PathParent
-
attr_accessor :path
def initialize(path)
raise if path.is_a?(Array)
- @children = {}
+ @current = nil
@path = path
@root = self
- @all_paths = []
+ super()
+ end
+
+ def []=(path, value)
+ value = Path.new(self, path, value) unless value.is_a?(Path)
+ super(path, value)
+ end
+
+ def add(path, options={})
+ with = options[:with] || path
+ self[path] = Path.new(self, path, with, options)
end
def all_paths
- @all_paths.uniq!
- @all_paths
+ values.tap { |v| v.uniq! }
end
def autoload_once
@@ -52,68 +104,54 @@ def load_paths
filter_by(:load_path?)
end
- def push(*)
- raise "Application root can only have one physical path"
- end
-
- alias unshift push
- alias << push
- alias concat push
-
protected
def filter_by(constraint)
all = []
all_paths.each do |path|
if path.send(constraint)
- paths = path.paths
- paths -= path.children.values.map { |p| p.send(constraint) ? [] : p.paths }.flatten
+ paths = path.existent
+ paths -= path.children.map { |p| p.send(constraint) ? [] : p.existent }.flatten
all.concat(paths)
end
end
all.uniq!
- all.reject! { |p| !File.exists?(p) }
all
end
end
- class Path
- include PathParent, Enumerable
+ class Path < Array
+ include PathParent
attr_reader :path
attr_accessor :glob
- def initialize(root, *paths)
- options = paths.last.is_a?(::Hash) ? paths.pop : {}
- @children = {}
+ def initialize(root, current, *paths)
+ options = paths.last.is_a?(::Hash) ? paths.pop : {}
+ super(paths.flatten)
+
+ @current = current
@root = root
- @paths = paths.flatten
@glob = options[:glob]
autoload_once! if options[:autoload_once]
eager_load! if options[:eager_load]
autoload! if options[:autoload]
load_path! if options[:load_path]
-
- @root.all_paths << self
- end
-
- def each
- to_a.each { |p| yield p }
end
- def push(path)
- @paths.push path
+ def children
+ keys = @root.keys.select { |k| k.include?(@current) }
+ keys.delete(@current)
+ @root.values_at(*keys.sort)
end
- alias << push
-
- def unshift(path)
- @paths.unshift path
+ def first
+ expanded.first
end
- def concat(paths)
- @paths.concat paths
+ def last
+ expanded.last
end
%w(autoload_once eager_load autoload load_path).each do |m|
@@ -132,20 +170,36 @@ def #{m}?
RUBY
end
- def paths
+ # Expands all paths against the root and return all unique values.
+ def expanded
raise "You need to set a path root" unless @root.path
+ result = []
- result = @paths.map do |p|
+ each do |p|
path = File.expand_path(p, @root.path)
- @glob ? Dir[File.join(path, @glob)] : path
+
+ if @glob
+ result.concat Dir[File.join(path, @glob)]
+ else
+ result << path
+ end
end
- result.flatten!
result.uniq!
result
end
- alias to_a paths
+ # Returns all expanded paths but only if they exist in the filesystem.
+ def existent
+ expanded.select { |f| File.exists?(f) }
+ end
+
+ def paths
+ ActiveSupport::Deprecation.warn "paths is deprecated. Please call expand instead."
+ expanded
+ end
+
+ alias to_a expanded
end
end
end
View
4 railties/lib/rails/plugin.rb
@@ -62,13 +62,13 @@ def config
end
initializer :handle_lib_autoload, :before => :set_load_path do |app|
- paths = if app.config.reload_plugins
+ autoload = if app.config.reload_plugins
config.autoload_paths
else
config.autoload_once_paths
end
- paths.concat config.paths.lib.to_a
+ autoload.concat paths["lib"].existent
end
initializer :load_init_rb, :before => :load_config_initializers do |app|
View
2  railties/lib/rails/tasks/railties.rake
@@ -2,7 +2,7 @@ namespace :railties do
desc "Create symlinks to railties public directories in application's public directory."
task :create_symlinks => :environment do
paths = Rails.application.config.static_asset_paths.dup
- app_public_path = Rails.application.config.paths.public.to_a.first
+ app_public_path = Rails.application.paths["public"].first
paths.each do |mount_path, path|
symlink_path = File.join(app_public_path, mount_path)
View
4 railties/test/application/configuration_test.rb
@@ -42,7 +42,7 @@ def teardown
test "Rails::Application responds to paths" do
require "#{app_path}/config/environment"
assert_respond_to AppTemplate::Application, :paths
- assert_equal AppTemplate::Application.paths.app.views.to_a, ["#{app_path}/app/views"]
+ assert_equal AppTemplate::Application.paths["app/views"].expanded, ["#{app_path}/app/views"]
end
test "the application root is set correctly" do
@@ -180,7 +180,7 @@ def assert_utf8
test "config.paths.public sets Rails.public_path" do
add_to_config <<-RUBY
- config.paths.public = "somewhere"
+ config.paths["public"] = "somewhere"
RUBY
require "#{app_path}/config/application"
View
5 railties/test/application/console_test.rb
@@ -1,7 +1,7 @@
require 'isolation/abstract_unit'
class ConsoleTest < Test::Unit::TestCase
- include ActiveSupport::Testing::Isolation
+ include ActiveSupport::Testing::Isolation
def setup
build_app
@@ -14,16 +14,15 @@ def load_environment
end
def test_app_method_should_return_integration_session
+ TestHelpers::Rack.send :remove_method, :app
load_environment
console_session = app
- assert_not_nil console_session
assert_instance_of ActionDispatch::Integration::Session, console_session
end
def test_new_session_should_return_integration_session
load_environment
session = new_session
- assert_not_nil session
assert_instance_of ActionDispatch::Integration::Session, session
end
View
2  railties/test/application/generators_test.rb
@@ -69,7 +69,7 @@ def with_bare_config
assert_equal :rspec, Rails::Generators.options[:rails][:test_framework]
assert_equal "-w", Rails::Generators.aliases[:rails][:test_framework]
assert_equal Hash[:shoulda => :test_unit], Rails::Generators.fallbacks
- assert_equal ["#{app_path}/lib/templates", "some/where"], Rails::Generators.templates_path
+ assert_equal ["some/where"], Rails::Generators.templates_path
end
test "generators no color on initialization" do
View
28 railties/test/application/paths_test.rb
@@ -25,7 +25,7 @@ def root(*path)
end
def assert_path(paths, *dir)
- assert_equal [root(*dir)], paths.paths
+ assert_equal [root(*dir)], paths.expanded
end
def assert_in_load_path(*path)
@@ -37,20 +37,20 @@ def assert_not_in_load_path(*path)
end
test "booting up Rails yields a valid paths object" do
- assert_path @paths.app.models, "app", "models"
- assert_path @paths.app.helpers, "app", "helpers"
- assert_path @paths.app.views, "app", "views"
- assert_path @paths.lib, "lib"
- assert_path @paths.vendor, "vendor"
- assert_path @paths.vendor.plugins, "vendor", "plugins"
- assert_path @paths.tmp, "tmp"
- assert_path @paths.tmp.cache, "tmp", "cache"
- assert_path @paths.config, "config"
- assert_path @paths.config.locales, "config", "locales", "en.yml"
- assert_path @paths.config.environment, "config", "environment.rb"
- assert_path @paths.config.environments, "config", "environments", "development.rb"
+ assert_path @paths["app/models"], "app/models"
+ assert_path @paths["app/helpers"], "app/helpers"
+ assert_path @paths["app/views"], "app/views"
+ assert_path @paths["lib"], "lib"
+ assert_path @paths["vendor"], "vendor"
+ assert_path @paths["vendor/plugins"], "vendor/plugins"
+ assert_path @paths["tmp"], "tmp"
+ assert_path @paths["tmp/cache"], "tmp/cache"
+ assert_path @paths["config"], "config"
+ assert_path @paths["config/locales"], "config/locales/en.yml"
+ assert_path @paths["config/environment"], "config/environment.rb"
+ assert_path @paths["config/environments"], "config/environments/development.rb"
- assert_equal root("app", "controllers"), @paths.app.controllers.to_a.first
+ assert_equal root("app", "controllers"), @paths["app/controllers"].expanded.first
end
test "booting up Rails yields a list of paths that are eager" do
View
268 railties/test/paths_test.rb
@@ -20,234 +20,264 @@ def setup
test "a paths object initialized with nil can be updated" do
root = Rails::Paths::Root.new(nil)
- root.app = "app"
+ root.add "app"
root.path = "/root"
- assert_equal ["/root/app"], root.app.to_a
+ assert_equal ["app"], root["app"]
+ assert_equal ["/root/app"], root["app"].to_a
end
test "creating a root level path" do
- @root.app = "/foo/bar"
- assert_equal ["/foo/bar"], @root.app.to_a
+ @root.add "app"
+ assert_equal ["/foo/bar/app"], @root["app"].to_a
+ end
+
+ test "creating a root level path with options" do
+ @root.add "app", :with => "/foo/bar"
+ assert_equal ["/foo/bar"], @root["app"].to_a
end
test "raises exception if root path never set" do
root = Rails::Paths::Root.new(nil)
- root.app = "app"
+ root.add "app"
assert_raises RuntimeError do
- root.app.to_a
+ root["app"].to_a
end
end
- test "creating a root level path without assignment" do
- @root.app "/foo/bar"
- assert_equal ["/foo/bar"], @root.app.to_a
- end
-
- test "trying to access a path that does not exist raises NoMethodError" do
- assert_raises(NoMethodError) { @root.app }
- end
-
- test "relative paths are relative to the paths root" do
- @root.app = "app"
- assert_equal ["/foo/bar/app"], @root.app.to_a
- end
-
- test "relative paths are relative to the paths root without assignment" do
- @root.app "app"
- assert_equal ["/foo/bar/app"], @root.app.to_a
- end
-
test "creating a child level path" do
- @root.app = "/foo/bar"
- @root.app.models = "/foo/bar/baz"
- assert_equal ["/foo/bar/baz"], @root.app.models.to_a
+ @root.add "app"
+ @root.add "app/models"
+ assert_equal ["/foo/bar/app/models"], @root["app/models"].to_a
end
- test "creating a child level path without assignment" do
- @root.app = "/foo/bar"
- @root.app.models "/foo/bar/baz"
- assert_equal ["/foo/bar/baz"], @root.app.models.to_a
+ test "creating a child level path with option" do
+ @root.add "app"
+ @root.add "app/models", :with => "/foo/bar/baz"
+ assert_equal ["/foo/bar/baz"], @root["app/models"].to_a
end
test "child level paths are relative from the root" do
- @root.app = "/app"
- @root.app.models = "baz"
-
- assert_equal ["/foo/bar/baz"], @root.app.models.to_a
+ @root.add "app"
+ @root.add "app/models", :with => "baz"
+ assert_equal ["/foo/bar/baz"], @root["app/models"].to_a
end
test "adding multiple physical paths as an array" do
- @root.app = ["/app", "/app2"]
- assert_equal ["/app", "/app2"], @root.app.to_a
- end
-
- test "adding multiple physical paths as an array without assignment" do
- @root.app "/app", "/app2"
- assert_equal ["/app", "/app2"], @root.app.to_a
+ @root.add "app", :with => ["/app", "/app2"]
+ assert_equal ["/app", "/app2"], @root["app"].to_a
end
test "adding multiple physical paths using #push" do
- @root.app = "/app"
- @root.app.push "/app2"
- assert_equal ["/app", "/app2"], @root.app.to_a
+ @root.add "app"
+ @root["app"].push "app2"
+ assert_equal ["/foo/bar/app", "/foo/bar/app2"], @root["app"].to_a
end
test "adding multiple physical paths using <<" do
- @root.app = "/app"
- @root.app << "/app2"
- assert_equal ["/app", "/app2"], @root.app.to_a
+ @root.add "app"
+ @root["app"] << "app2"
+ assert_equal ["/foo/bar/app", "/foo/bar/app2"], @root["app"].to_a
end
test "adding multiple physical paths using concat" do
- @root.app = "/app"
- @root.app.concat ["/app2", "/app3"]
- assert_equal ["/app", "/app2", "/app3"], @root.app.to_a
+ @root.add "app"
+ @root["app"].concat ["app2", "/app3"]
+ assert_equal ["/foo/bar/app", "/foo/bar/app2", "/app3"], @root["app"].to_a
end
test "adding multiple physical paths using #unshift" do
- @root.app = "/app"
- @root.app.unshift "/app2"
- assert_equal ["/app2", "/app"], @root.app.to_a
+ @root.add "app"
+ @root["app"].unshift "app2"
+ assert_equal ["/foo/bar/app2", "/foo/bar/app"], @root["app"].to_a
end
test "the root can only have one physical path" do
assert_raise(RuntimeError) { Rails::Paths::Root.new(["/fiz", "/biz"]) }
- assert_raise(RuntimeError) { @root.push "/biz" }
- assert_raise(RuntimeError) { @root.unshift "/biz" }
- assert_raise(RuntimeError) { @root.concat ["/biz"]}
- assert_raise(RuntimeError) { @root << "/biz" }
end
test "it is possible to add a path that should be autoloaded only once" do
- @root.app = "/app"
- @root.app.autoload_once!
- assert @root.app.autoload_once?
- assert @root.autoload_once.include?(@root.app.paths.first)
+ @root.add "app", :with => "/app"
+ @root["app"].autoload_once!
+ assert @root["app"].autoload_once?
+ assert @root.autoload_once.include?(@root["app"].expanded.first)
end
test "it is possible to remove a path that should be autoloaded only once" do
- @root.app = "/app"
- @root.app.autoload_once!
- assert @root.app.autoload_once?
+ @root["app"] = "/app"
+ @root["app"].autoload_once!
+ assert @root["app"].autoload_once?
- @root.app.skip_autoload_once!
- assert !@root.app.autoload_once?
- assert !@root.autoload_once.include?(@root.app.paths.first)
+ @root["app"].skip_autoload_once!
+ assert !@root["app"].autoload_once?
+ assert !@root.autoload_once.include?(@root["app"].expanded.first)
end
test "it is possible to add a path without assignment and specify it should be loaded only once" do
- @root.app "/app", :autoload_once => true
- assert @root.app.autoload_once?
+ @root.add "app", :with => "/app", :autoload_once => true
+ assert @root["app"].autoload_once?
assert @root.autoload_once.include?("/app")
end
test "it is possible to add multiple paths without assignment and specify it should be loaded only once" do
- @root.app "/app", "/app2", :autoload_once => true
- assert @root.app.autoload_once?
+ @root.add "app", :with => ["/app", "/app2"], :autoload_once => true
+ assert @root["app"].autoload_once?
assert @root.autoload_once.include?("/app")
assert @root.autoload_once.include?("/app2")
end
test "making a path autoload_once more than once only includes it once in @root.load_once" do
- @root.app = "/app"
- @root.app.autoload_once!
- @root.app.autoload_once!
- assert_equal 1, @root.autoload_once.select {|p| p == @root.app.paths.first }.size
+ @root["app"] = "/app"
+ @root["app"].autoload_once!
+ @root["app"].autoload_once!
+ assert_equal 1, @root.autoload_once.select {|p| p == @root["app"].expanded.first }.size
end
test "paths added to a load_once path should be added to the autoload_once collection" do
- @root.app = "/app"
- @root.app.autoload_once!
- @root.app << "/app2"
+ @root["app"] = "/app"
+ @root["app"].autoload_once!
+ @root["app"] << "/app2"
assert_equal 2, @root.autoload_once.size
end
test "it is possible to mark a path as eager loaded" do
- @root.app = "/app"
- @root.app.eager_load!
- assert @root.app.eager_load?
- assert @root.eager_load.include?(@root.app.paths.first)
+ @root["app"] = "/app"
+ @root["app"].eager_load!
+ assert @root["app"].eager_load?
+ assert @root.eager_load.include?(@root["app"].to_a.first)
end
test "it is possible to skip a path from eager loading" do
- @root.app = "/app"
- @root.app.eager_load!
- assert @root.app.eager_load?
+ @root["app"] = "/app"
+ @root["app"].eager_load!
+ assert @root["app"].eager_load?
- @root.app.skip_eager_load!
- assert !@root.app.eager_load?
- assert !@root.eager_load.include?(@root.app.paths.first)
+ @root["app"].skip_eager_load!
+ assert !@root["app"].eager_load?
+ assert !@root.eager_load.include?(@root["app"].to_a.first)
end
test "it is possible to add a path without assignment and mark it as eager" do
- @root.app "/app", :eager_load => true
- assert @root.app.eager_load?
+ @root.add "app", :with => "/app", :eager_load => true
+ assert @root["app"].eager_load?
assert @root.eager_load.include?("/app")
end
test "it is possible to add multiple paths without assignment and mark them as eager" do
- @root.app "/app", "/app2", :eager_load => true
- assert @root.app.eager_load?
+ @root.add "app", :with => ["/app", "/app2"], :eager_load => true
+ assert @root["app"].eager_load?
assert @root.eager_load.include?("/app")
assert @root.eager_load.include?("/app2")
end
test "it is possible to create a path without assignment and mark it both as eager and load once" do
- @root.app "/app", :eager_load => true, :autoload_once => true
- assert @root.app.eager_load?
- assert @root.app.autoload_once?
+ @root.add "app", :with => "/app", :eager_load => true, :autoload_once => true
+ assert @root["app"].eager_load?
+ assert @root["app"].autoload_once?
assert @root.eager_load.include?("/app")
assert @root.autoload_once.include?("/app")
end
test "making a path eager more than once only includes it once in @root.eager_paths" do
- @root.app = "/app"
- @root.app.eager_load!
- @root.app.eager_load!
- assert_equal 1, @root.eager_load.select {|p| p == @root.app.paths.first }.size
+ @root["app"] = "/app"
+ @root["app"].eager_load!
+ @root["app"].eager_load!
+ assert_equal 1, @root.eager_load.select {|p| p == @root["app"].expanded.first }.size
end
test "paths added to a eager_load path should be added to the eager_load collection" do
- @root.app = "/app"
- @root.app.eager_load!
- @root.app << "/app2"
+ @root["app"] = "/app"
+ @root["app"].eager_load!
+ @root["app"] << "/app2"
assert_equal 2, @root.eager_load.size
end
test "it should be possible to add a path's default glob" do
- @root.app = "/app"
- @root.app.glob = "*.rb"
- assert_equal "*.rb", @root.app.glob
+ @root["app"] = "/app"
+ @root["app"].glob = "*.rb"
+ assert_equal "*.rb", @root["app"].glob
end
test "it should be possible to override a path's default glob without assignment" do
- @root.app "/app", :glob => "*.rb"
- assert_equal "*.rb", @root.app.glob
+ @root.add "app", :with => "/app", :glob => "*.rb"
+ assert_equal "*.rb", @root["app"].glob
end
test "a path can be added to the load path" do
- @root.app = "app"
- @root.app.load_path!
- @root.app.models = "app/models"
+ @root["app"] = "app"
+ @root["app"].load_path!
+ @root["app/models"] = "app/models"
assert_equal ["/foo/bar/app"], @root.load_paths
end
test "a path can be added to the load path on creation" do
- @root.app "/app", :load_path => true
- assert @root.app.load_path?
+ @root.add "app", :with => "/app", :load_path => true
+ assert @root["app"].load_path?
assert_equal ["/app"], @root.load_paths
end
test "a path can be marked as autoload path" do
- @root.app = "app"
- @root.app.autoload!
- @root.app.models = "app/models"
+ @root["app"] = "app"
+ @root["app"].autoload!
+ @root["app/models"] = "app/models"
assert_equal ["/foo/bar/app"], @root.autoload_paths
end
test "a path can be marked as autoload on creation" do
- @root.app "/app", :autoload => true
- assert @root.app.autoload?
+ @root.add "app", :with => "/app", :autoload => true
+ assert @root["app"].autoload?
assert_equal ["/app"], @root.autoload_paths
end
+
+ # Deprecated API tests
+
+ test "reading a root level path with assignment" do
+ @root.add "app"
+ assert_deprecated do
+ assert_equal ["/foo/bar/app"], @root.app.to_a
+ end
+ end
+
+ test "creating a root level path with assignment" do
+ assert_deprecated do
+ @root.app = "/foo/bar"
+ end
+ assert_equal ["/foo/bar"], @root["app"].to_a
+ end
+
+ test "creating a root level path without assignment" do
+ assert_deprecated do
+ @root.app "/foo/bar"
+ end
+ assert_equal ["/foo/bar"], @root["app"].to_a
+ end
+
+ test "reading a nested level path with assignment" do
+ @root.add "app"
+ @root.add "app/model"
+ assert_deprecated do
+ assert_equal ["/foo/bar/app/model"], @root.app.model.to_a
+ end
+ end
+
+ test "creating a nested level path with assignment" do
+ @root.add "app"
+ assert_deprecated do
+ @root.app.model = "/foo/bar"
+ end
+ assert_equal ["/foo/bar"], @root["app/model"].to_a
+ end
+
+ test "creating a nested level path without assignment" do
+ @root.add "app"
+ assert_deprecated do
+ @root.app.model "/foo/bar"
+ end
+ assert_equal ["/foo/bar"], @root["app/model"].to_a
+ end
+
+ test "trying to access a path that does not exist raises NoMethodError" do
+ assert_deprecated do
+ assert_raises(NoMethodError) { @root.app }
+ end
+ end
end
View
8 railties/test/railties/engine_test.rb
@@ -552,11 +552,11 @@ def new
end
RUBY
- @plugin.write "app/views/bukkits/posts/new.html.erb", <<-RUBY
+ @plugin.write "app/views/bukkits/posts/new.html.erb", <<-ERB
<%= form_for(Bukkits::Post.new) do |f| %>
<%= f.text_field :title %>
<% end %>
- RUBY
+ ERB
add_to_config("config.action_dispatch.show_exceptions = false")
@@ -593,7 +593,7 @@ class Engine < ::Rails::Engine
module Bukkits
class Engine < ::Rails::Engine
namespace(Bukkits)
- config.paths.public = "#{File.join(@plugin.path, "alternate_public")}"
+ paths["public"] = "#{File.join(@plugin.path, "alternate_public")}"
end
end
RUBY
@@ -611,7 +611,7 @@ class Engine < ::Rails::Engine
module Bukkits
class Engine < ::Rails::Engine
namespace(Bukkits)
- config.paths.public = "#{File.join(@plugin.path, "not_existing")}"
+ paths["public"] = "#{File.join(@plugin.path, "not_existing")}"
end
end
RUBY
Please sign in to comment.
Something went wrong with that request. Please try again.