diff options
Diffstat (limited to 'railties/lib/rails/engine.rb')
-rw-r--r-- | railties/lib/rails/engine.rb | 95 |
1 files changed, 62 insertions, 33 deletions
diff --git a/railties/lib/rails/engine.rb b/railties/lib/rails/engine.rb index 89b151beb6..131d6e5711 100644 --- a/railties/lib/rails/engine.rb +++ b/railties/lib/rails/engine.rb @@ -228,7 +228,7 @@ module Rails # resources :articles # end # - # The routes above will automatically point to <tt>MyEngine::ApplicationContoller</tt>. Furthermore, you don't + # The routes above will automatically point to <tt>MyEngine::ArticlesController</tt>. Furthermore, you don't # need to use longer url helpers like <tt>my_engine_articles_path</tt>. Instead, you should simply use # <tt>articles_path</tt> as you would do with your application. # @@ -245,7 +245,7 @@ module Rails # # Additionally, an isolated engine will set its name according to namespace, so # MyEngine::Engine.engine_name will be "my_engine". It will also set MyEngine.table_name_prefix - # to "my_engine_", changing the MyEngine::Article model to use the my_engine_article table. + # to "my_engine_", changing the MyEngine::Article model to use the my_engine_articles table. # # == Using Engine's routes outside Engine # @@ -296,16 +296,16 @@ module Rails # If you want to share just a few specific helpers you can add them to application's # helpers in ApplicationController: # - # class ApplicationController < ActionController::Base - # helper MyEngine::SharedEngineHelper - # end + # class ApplicationController < ActionController::Base + # helper MyEngine::SharedEngineHelper + # end # - # If you want to include all of the engine's helpers, you can use #helpers method on an engine's + # If you want to include all of the engine's helpers, you can use #helper method on an engine's # instance: # - # class ApplicationController < ActionController::Base - # helper MyEngine::Engine.helpers - # end + # class ApplicationController < ActionController::Base + # helper MyEngine::Engine.helpers + # end # # It will include all of the helpers from engine's directory. Take into account that this does # not include helpers defined in controllers with helper_method or other similar solutions, @@ -326,10 +326,21 @@ module Rails # migration in the application and rerun copying migrations. # # If your engine has migrations, you may also want to prepare data for the database in - # the <tt>seeds.rb</tt> file. You can load that data using the <tt>load_seed</tt> method, e.g. + # the <tt>db/seeds.rb</tt> file. You can load that data using the <tt>load_seed</tt> method, e.g. # # MyEngine::Engine.load_seed # + # == Loading priority + # + # In order to change engine's priority you can use config.railties_order in main application. + # It will affect the priority of loading views, helpers, assets and all the other files + # related to engine or application. + # + # Example: + # + # # load Blog::Engine with highest priority, followed by application and other railties + # config.railties_order = [Blog::Engine, :main_app, :all] + # class Engine < Railtie autoload :Configuration, "rails/engine/configuration" autoload :Railties, "rails/engine/railties" @@ -371,20 +382,28 @@ module Rails self.routes.default_scope = { :module => ActiveSupport::Inflector.underscore(mod.name) } self.isolated = true - unless mod.respond_to?(:_railtie) - name = engine_name - _railtie = self + unless mod.respond_to?(:railtie_namespace) + name, railtie = engine_name, self + mod.singleton_class.instance_eval do - define_method(:_railtie) do - _railtie - end + define_method(:railtie_namespace) { railtie } unless mod.respond_to?(:table_name_prefix) - define_method(:table_name_prefix) do - "#{name}_" - end + define_method(:table_name_prefix) { "#{name}_" } + end + + unless mod.respond_to?(:use_relative_model_naming?) + class_eval "def use_relative_model_naming?; true; end", __FILE__, __LINE__ + end + + unless mod.respond_to?(:railtie_helpers_paths) + define_method(:railtie_helpers_paths) { railtie.helpers_paths } + end + + unless mod.respond_to?(:railtie_routes_url_helpers) + define_method(:railtie_routes_url_helpers) { railtie.routes_url_helpers } end - end + end end end @@ -429,13 +448,6 @@ module Rails def helpers @helpers ||= begin helpers = Module.new - - helpers_paths = if config.respond_to?(:helpers_paths) - config.helpers_paths - else - paths["app/helpers"].existent - end - all = ActionController::Base.all_helpers_from_path(helpers_paths) ActionController::Base.modules_for_helpers(all).each do |mod| helpers.send(:include, mod) @@ -444,6 +456,14 @@ module Rails end end + def helpers_paths + paths["app/helpers"].existent + end + + def routes_url_helpers + routes.url_helpers + end + def app @app ||= begin config.middleware = config.middleware.merge_into(default_middleware_stack) @@ -471,10 +491,19 @@ module Rails @routes end + def ordered_railties + railties.all + [self] + end + def initializers initializers = [] - railties.all { |r| initializers += r.initializers } - initializers += super + ordered_railties.each do |r| + if r == self + initializers += super + else + initializers += r.initializers + end + end initializers end @@ -488,7 +517,7 @@ module Rails # Blog::Engine.load_seed def load_seed seed_file = paths["db/seeds"].existent.first - load(seed_file) if File.exist?(seed_file) + load(seed_file) if seed_file end # Add configured load paths to ruby load paths and remove duplicates. @@ -532,17 +561,17 @@ module Rails initializer :add_view_paths do views = paths["app/views"].existent unless views.empty? - ActiveSupport.on_load(:action_controller){ prepend_view_path(views) } + ActiveSupport.on_load(:action_controller){ prepend_view_path(views) if respond_to?(:prepend_view_path) } ActiveSupport.on_load(:action_mailer){ prepend_view_path(views) } end end - initializer :load_environment_config, :before => :load_environment_hook do + initializer :load_environment_config, :before => :load_environment_hook, :group => :all do environment = paths["config/environments"].existent.first require environment if environment end - initializer :append_assets_path do |app| + initializer :append_assets_path, :group => :all do |app| app.config.assets.paths.unshift(*paths["vendor/assets"].existent_directories) app.config.assets.paths.unshift(*paths["lib/assets"].existent_directories) app.config.assets.paths.unshift(*paths["app/assets"].existent_directories) |