Skip to content

Commit

Permalink
Create an :assets group in the Gemfile.
Browse files Browse the repository at this point in the history
This group is required by default only on development and test
(you can change it on config/application.rb).

`rake assets:precompile` will automatically add the assets group
to Rails.groups (and consequently Bundler.require) and should work
transparently.
  • Loading branch information
josevalim committed Jun 21, 2011
1 parent db909f8 commit c4794b0
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 23 deletions.
6 changes: 3 additions & 3 deletions railties/lib/rails/generators/app_base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -164,15 +164,15 @@ def gem_for_database
end end
end end


def gem_for_ruby_debugger def ruby_debugger_gemfile_entry
if RUBY_VERSION < "1.9" if RUBY_VERSION < "1.9"
"gem 'ruby-debug'" "gem 'ruby-debug'"
else else
"gem 'ruby-debug19', :require => 'ruby-debug'" "gem 'ruby-debug19', :require => 'ruby-debug'"
end end
end end


def gem_for_turn def turn_gemfile_entry
unless RUBY_VERSION < "1.9.2" || options[:skip_test_unit] unless RUBY_VERSION < "1.9.2" || options[:skip_test_unit]
<<-GEMFILE.strip_heredoc <<-GEMFILE.strip_heredoc
group :test do group :test do
Expand All @@ -183,7 +183,7 @@ def gem_for_turn
end end
end end


def gem_for_javascript def javascript_gemfile_entry
"gem '#{options[:javascript]}-rails'" unless options[:skip_javascript] "gem '#{options[:javascript]}-rails'" unless options[:skip_javascript]
end end


Expand Down
18 changes: 11 additions & 7 deletions railties/lib/rails/generators/rails/app/templates/Gemfile
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ source 'http://rubygems.org'
<%= database_gemfile_entry -%> <%= database_gemfile_entry -%>


<%= "gem 'jruby-openssl'\n" if defined?(JRUBY_VERSION) -%> <%= "gem 'jruby-openssl'\n" if defined?(JRUBY_VERSION) -%>
# Asset template engines
<%= "gem 'json'\n" if RUBY_VERSION < "1.9.2" -%> <%= "gem 'json'\n" if RUBY_VERSION < "1.9.2" -%>
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'


<%= gem_for_javascript %> # Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', "~> 3.1.0.rc"
gem 'coffee-script'
gem 'uglifier'
end

<%= javascript_gemfile_entry %>
# Use unicorn as the web server # Use unicorn as the web server
# gem 'unicorn' # gem 'unicorn'
Expand All @@ -20,6 +24,6 @@ gem 'uglifier'
# gem 'capistrano' # gem 'capistrano'
# To use debugger # To use debugger
# <%= gem_for_ruby_debugger %> # <%= ruby_debugger_gemfile_entry %>


<%= gem_for_turn -%> <%= turn_gemfile_entry -%>
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
<%= comment_if :skip_test_unit %> require "rails/test_unit/railtie" <%= comment_if :skip_test_unit %> require "rails/test_unit/railtie"
<% end -%> <% end -%>
# If you have a Gemfile, require the gems listed there, including any gems # If you have a Gemfile, require the default gems, the ones in the
# you've limited to :test, :development, or :production. # current environment and also include :assets gems if in development
Bundler.require(:default, Rails.env) if defined?(Bundler) # or test environments.
Bundler.require *Rails.groups(:assets => %w(development test)) if defined?(Bundler)


module <%= app_const_base %> module <%= app_const_base %>
class Application < Rails::Application class Application < Rails::Application
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ source "http://rubygems.org"
<% end -%> <% end -%>


<% if mountable? -%> <% if mountable? -%>
<%= gem_for_javascript -%> <%= javascript_gemfile_entry -%>
<% end -%> <% end -%>
if RUBY_VERSION < '1.9' # To use debugger
gem "ruby-debug", ">= 0.10.3" # <%= ruby_debugger_gemfile_entry %>
end
15 changes: 10 additions & 5 deletions railties/lib/rails/tasks/assets.rake
Original file line number Original file line Diff line number Diff line change
@@ -1,11 +1,16 @@
namespace :assets do namespace :assets do
desc "Compile all the assets named in config.assets.precompile" desc "Compile all the assets named in config.assets.precompile"
task :precompile => :environment do task :precompile do
# Give assets access to asset_path if ENV["RAILS_GROUPS"].to_s.empty?
Sprockets::Helpers::RailsHelper ENV["RAILS_GROUPS"] = "assets"
Kernel.exec $0, *ARGV
else
Rake::Task["environment"].invoke
Sprockets::Helpers::RailsHelper


assets = Rails.application.config.assets.precompile assets = Rails.application.config.assets.precompile
Rails.application.assets.precompile(*assets) Rails.application.assets.precompile(*assets)
end
end end


desc "Remove compiled assets" desc "Remove compiled assets"
Expand Down
14 changes: 13 additions & 1 deletion railties/test/application/assets_test.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,8 +1,9 @@
require 'isolation/abstract_unit' require 'isolation/abstract_unit'
require 'active_support/core_ext/kernel/reporting'
require 'rack/test' require 'rack/test'


module ApplicationTests module ApplicationTests
class RoutingTest < Test::Unit::TestCase class AssetsTest < Test::Unit::TestCase
include ActiveSupport::Testing::Isolation include ActiveSupport::Testing::Isolation
include Rack::Test::Methods include Rack::Test::Methods


Expand Down Expand Up @@ -34,6 +35,17 @@ def app
assert_match "alert()", last_response.body assert_match "alert()", last_response.body
end end


test "assets are compiled properly" do
app_file "app/assets/javascripts/application.js", "alert();"

capture(:stdout) do
Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
end

file = Dir["#{app_path}/public/assets/application-*.js"][0]
assert_equal "alert();\n", File.read(file)
end

test "does not stream session cookies back" do test "does not stream session cookies back" do
app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();" app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"


Expand Down
4 changes: 4 additions & 0 deletions railties/test/isolation/abstract_unit.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ def app_file(path, contents)
end end
end end


def remove_file(path)
FileUtils.rm_rf "#{app_path}/#{path}"
end

def controller(name, contents) def controller(name, contents)
app_file("app/controllers/#{name}_controller.rb", contents) app_file("app/controllers/#{name}_controller.rb", contents)
end end
Expand Down

0 comments on commit c4794b0

Please sign in to comment.