From 0c181b4b70a703436e608c38fc02cf31db7c915c Mon Sep 17 00:00:00 2001 From: Ivan Vanyak Date: Mon, 17 Dec 2012 14:12:24 +1100 Subject: [PATCH 1/7] Add script_name option description and example for #url_for options --- actionpack/lib/action_dispatch/routing/url_for.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/actionpack/lib/action_dispatch/routing/url_for.rb b/actionpack/lib/action_dispatch/routing/url_for.rb index 76311c423a..8e19025722 100644 --- a/actionpack/lib/action_dispatch/routing/url_for.rb +++ b/actionpack/lib/action_dispatch/routing/url_for.rb @@ -130,6 +130,7 @@ module ActionDispatch # * :port - Optionally specify the port to connect to. # * :anchor - An anchor name to be appended to the path. # * :trailing_slash - If true, adds a trailing slash, as in "/archive/2009/" + # * :script_name - Specifies application path relative to domain root. If provided, prepends application path. # # Any other key (:controller, :action, etc.) given to # +url_for+ is forwarded to the Routes module. @@ -142,6 +143,10 @@ module ActionDispatch # # => 'http://somehost.org/tasks/testing/' # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', number: '33' # # => 'http://somehost.org/tasks/testing?number=33' + # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp" + # # => 'http://somehost.org/myapp/tasks/testing' + # url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true + # # => '/myapp/tasks/testing' def url_for(options = nil) case options when nil From 86c5cea9f414d34fd92adb064fde5ecc7b40c727 Mon Sep 17 00:00:00 2001 From: Damian Galarza Date: Sun, 16 Dec 2012 17:43:59 -0500 Subject: [PATCH 2/7] Explain controller specific assets more thoroughly The current section on controller specific assets does not really explain how the default application.css and application.js files generated by rails will work with controller specific assets and is a bit ambiguous. We should remind users that they will be included into their application by default but that they have the option to include them only where needed if they want and how this works with precompiling assets. [ci_skip] --- guides/source/asset_pipeline.md | 49 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md index 743a04ed42..0bb6d7ca71 100644 --- a/guides/source/asset_pipeline.md +++ b/guides/source/asset_pipeline.md @@ -96,29 +96,24 @@ Assets can still be placed in the `public` hierarchy. Any assets under `public` In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files in `app/assets` are never served directly in production. +### Controller Specific Assets + When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`) for that controller. -For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Note that you have to set `config.assets.precompile` in `config/environments/production.rb` if you want to precomepile them and use in production mode. You can append them one by one or do something like this: +For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. By default these files will be ready to use by your application immediately using the `require_tree` directive. See [Manifest Files and Directives](#manifest-files-and-directives) for more details on require_tree. - # config/environments/production.rb - config.assets.precompile << Proc.new { |path| - if path =~ /\.(css|js)\z/ - full_path = Rails.application.assets.resolve(path).to_path - app_assets_path = Rails.root.join('app', 'assets').to_path - if full_path.starts_with? app_assets_path - puts "including asset: " + full_path - true - else - puts "excluding asset: " + full_path - false - end - else - false - end - } +You can also opt to include controller specific stylesheets and javascripts only in the controllers they belong to using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once. + +WARNING: When using asset precompiliation (the production default) you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compiliation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works. NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes. +You can also disable the generation of asset files when generating a controller by adding the following to your `config/application.rb` configuration: + + config.generators do |g| + g.assets false + end + ### Asset Organization Pipeline assets can be placed inside an application in one of three locations: `app/assets`, `lib/assets` or `vendor/assets`. @@ -459,6 +454,26 @@ If you have other manifests or individual stylesheets and JavaScript files to in config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js'] ``` +Or you can opt to precompile all assets with something like this: + + # config/environments/production.rb + config.assets.precompile << Proc.new { |path| + if path =~ /\.(css|js)\z/ + full_path = Rails.application.assets.resolve(path).to_path + app_assets_path = Rails.root.join('app', 'assets').to_path + if full_path.starts_with? app_assets_path + puts "including asset: " + full_path + true + else + puts "excluding asset: " + full_path + false + end + else + false + end + } + + NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array. The rake task also generates a `manifest.yml` that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like: From 9f42f732a420ba77a5a2193cdc15ebb61ce2dda8 Mon Sep 17 00:00:00 2001 From: John Kelly Date: Thu, 20 Dec 2012 00:24:49 -0800 Subject: [PATCH 3/7] Remove Reference to Ruby 1.8.7 --- activesupport/lib/active_support/core_ext/array/wrap.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/activesupport/lib/active_support/core_ext/array/wrap.rb b/activesupport/lib/active_support/core_ext/array/wrap.rb index 05b09a4c7f..1245768870 100644 --- a/activesupport/lib/active_support/core_ext/array/wrap.rb +++ b/activesupport/lib/active_support/core_ext/array/wrap.rb @@ -29,8 +29,7 @@ class Array # # [*object] # - # which for +nil+ returns [nil] (Ruby 1.8.7) or [] (Ruby - # 1.9), and calls to Array(object) otherwise. + # which for +nil+ returns [], and calls to Array(object) otherwise. # # Thus, in this case the behavior may be different for +nil+, and the differences with # Kernel#Array explained above apply to the rest of objects. From 4c41e87e3ae548c44810b66437b2f0f6e73b2106 Mon Sep 17 00:00:00 2001 From: kei Date: Thu, 20 Dec 2012 18:10:21 +0900 Subject: [PATCH 4/7] Fix documentation style --- actionpack/lib/abstract_controller/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 36a0dcb2de..812a35735f 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -113,7 +113,7 @@ module AbstractController # helpers with the following behavior: # # String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper", - # and "foo_bar_helper.rb" is loaded using require_dependency. + # and "foo_bar_helper.rb" is loaded using require_dependency. # # Module:: No further processing # From e1f8ec59f2cc83f052b15233147aa2d6d8114a4d Mon Sep 17 00:00:00 2001 From: kei Date: Thu, 20 Dec 2012 18:24:31 +0900 Subject: [PATCH 5/7] Fix incorrect adjustment 4c41e87e3ae548c44810b66437b2f0f6e73b2106 --- actionpack/lib/abstract_controller/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 812a35735f..20f289239f 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -113,7 +113,7 @@ module AbstractController # helpers with the following behavior: # # String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper", - # and "foo_bar_helper.rb" is loaded using require_dependency. + # and "foo_bar_helper.rb" is loaded using require_dependency. # # Module:: No further processing # From 6fee8f3ce612b01d35b10da20f8bb8510003a5a2 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 21 Dec 2012 23:03:52 +0530 Subject: [PATCH 6/7] Revert "Fix incorrect adjustment 4c41e87e3ae548c44810b66437b2f0f6e73b2106" This reverts commit e1f8ec59f2cc83f052b15233147aa2d6d8114a4d. Reason: seems bad styling [ci skip] --- actionpack/lib/abstract_controller/helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/actionpack/lib/abstract_controller/helpers.rb b/actionpack/lib/abstract_controller/helpers.rb index 20f289239f..812a35735f 100644 --- a/actionpack/lib/abstract_controller/helpers.rb +++ b/actionpack/lib/abstract_controller/helpers.rb @@ -113,7 +113,7 @@ module AbstractController # helpers with the following behavior: # # String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper", - # and "foo_bar_helper.rb" is loaded using require_dependency. + # and "foo_bar_helper.rb" is loaded using require_dependency. # # Module:: No further processing # From 4a8bdabc8ee895eeb3c35f2c4dc4900c42b9cd86 Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Fri, 21 Dec 2012 23:08:56 +0530 Subject: [PATCH 7/7] copy edits in assets guide [ci skip] --- guides/source/asset_pipeline.md | 49 +++++++++++++++++---------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/guides/source/asset_pipeline.md b/guides/source/asset_pipeline.md index 0bb6d7ca71..b302ef76c6 100644 --- a/guides/source/asset_pipeline.md +++ b/guides/source/asset_pipeline.md @@ -102,17 +102,19 @@ When you generate a scaffold or a controller, Rails also generates a JavaScript For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. By default these files will be ready to use by your application immediately using the `require_tree` directive. See [Manifest Files and Directives](#manifest-files-and-directives) for more details on require_tree. -You can also opt to include controller specific stylesheets and javascripts only in the controllers they belong to using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once. +You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once. -WARNING: When using asset precompiliation (the production default) you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compiliation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works. +WARNING: When using asset precompilation (the production default), you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compilation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works. -NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes. +NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes. You can also disable the generation of asset files when generating a controller by adding the following to your `config/application.rb` configuration: - config.generators do |g| - g.assets false - end +```ruby +config.generators do |g| + g.assets false +end +``` ### Asset Organization @@ -456,23 +458,24 @@ config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js'] Or you can opt to precompile all assets with something like this: - # config/environments/production.rb - config.assets.precompile << Proc.new { |path| - if path =~ /\.(css|js)\z/ - full_path = Rails.application.assets.resolve(path).to_path - app_assets_path = Rails.root.join('app', 'assets').to_path - if full_path.starts_with? app_assets_path - puts "including asset: " + full_path - true - else - puts "excluding asset: " + full_path - false - end - else - false - end - } - +```ruby +# config/environments/production.rb +config.assets.precompile << Proc.new { |path| + if path =~ /\.(css|js)\z/ + full_path = Rails.application.assets.resolve(path).to_path + app_assets_path = Rails.root.join('app', 'assets').to_path + if full_path.starts_with? app_assets_path + puts "including asset: " + full_path + true + else + puts "excluding asset: " + full_path + false + end + else + false + end +} +``` NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array.