Skip to content

Commit

Permalink
-j parameter added to rails new command.
Browse files Browse the repository at this point in the history
 Now you can start your apps with jquery with rails new myapp -j jquery, the default is still prototype

[#5613 state:committed]

Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
  • Loading branch information
siong1987 authored and spastorino committed Oct 11, 2010
1 parent cecccf1 commit 708e094
Show file tree
Hide file tree
Showing 8 changed files with 6,309 additions and 57 deletions.
5 changes: 3 additions & 2 deletions railties/Rakefile
Expand Up @@ -45,8 +45,9 @@ task :generate_guides do
ruby "guides/rails_guides.rb"
end

task :update_prototype_ujs do
system "curl http://github.com/rails/prototype-ujs/raw/master/src/rails.js > lib/rails/generators/rails/app/templates/public/javascripts/rails.js"
task :update_ujs do
system "curl http://github.com/rails/prototype-ujs/raw/master/src/rails.js > lib/rails/generators/rails/app/templates/public/javascripts/prototype_ujs.js"
system "curl http://github.com/rails/jquery-ujs/raw/master/src/rails.js > lib/rails/generators/rails/app/templates/public/javascripts/jquery_ujs.js"
end

# Validate guides -------------------------------------------------------------------------
Expand Down
32 changes: 24 additions & 8 deletions railties/lib/rails/generators/rails/app/app_generator.rb
Expand Up @@ -108,12 +108,20 @@ def stylesheets
end

def javascripts
unless options[:skip_prototype]
directory "public/javascripts"
else
empty_directory_with_gitkeep "public/javascripts"
create_file "public/javascripts/application.js"
empty_directory "public/javascripts"

unless options[:skip_javascript]
copy_file "public/javascripts/#{@options[:javascript]}.js"
copy_file "public/javascripts/#{@options[:javascript]}_ujs.js", "public/javascripts/rails.js"

if options[:prototype]
copy_file "public/javascripts/controls.js"
copy_file "public/javascripts/dragdrop.js"
copy_file "public/javascripts/effects.js"
end
end

copy_file "public/javascripts/application.js"
end

def script
Expand Down Expand Up @@ -152,6 +160,7 @@ module Generators

class AppGenerator < Base
DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
JAVASCRIPTS = %w( prototype jquery )

attr_accessor :rails_template
add_shebang_option!
Expand All @@ -161,6 +170,9 @@ class AppGenerator < Base
class_option :database, :type => :string, :aliases => "-d", :default => "sqlite3",
:desc => "Preconfigure for selected database (options: #{DATABASES.join('/')})"

class_option :javascript, :type => :string, :aliases => "-j", :default => "prototype",
:desc => "Preconfigure for selected javascript library (options: #{JAVASCRIPTS.join('/')})"

class_option :builder, :type => :string, :aliases => "-b",
:desc => "Path to an application builder (can be a filesystem path or URL)"

Expand All @@ -182,8 +194,8 @@ class AppGenerator < Base
class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false,
:desc => "Skip Test::Unit files"

class_option :skip_prototype, :type => :boolean, :aliases => "-J", :default => false,
:desc => "Skip Prototype files"
class_option :skip_javascript, :type => :boolean, :aliases => "-J", :default => false,
:desc => "Skip javascript files"

class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
:desc => "Skip Git ignores and keeps"
Expand All @@ -205,6 +217,10 @@ def initialize(*args)
if !options[:skip_active_record] && !DATABASES.include?(options[:database])
raise Error, "Invalid value for --database option. Supported for preconfiguration are: #{DATABASES.join(", ")}."
end

if !options[:skip_javascript] && !JAVASCRIPTS.include?(options[:javascript])
raise Error, "Invalid value for --javascript option. Supported for preconfiguration are: #{JAVASCRIPTS.join(", ")}."
end
end

def create_root
Expand Down Expand Up @@ -269,7 +285,7 @@ def create_public_stylesheets_files
build(:stylesheets)
end

def create_prototype_files
def create_javascript_files
build(:javascripts)
end

Expand Down
Expand Up @@ -40,8 +40,10 @@ class Application < Rails::Application
# config.i18n.default_locale = :de

# JavaScript files you want as :defaults (application.js is always included).
<% if options[:skip_prototype] -%>
<% if options[:skip_javascript] -%>
config.action_view.javascript_expansions[:defaults] = %w()
<% elsif options[:javascript] == 'jquery' -%>
config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
<% else -%>
# config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
<% end -%>
Expand Down

16 comments on commit 708e094

@nicolasblanco
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhhh ya!

@ernie
Copy link
Contributor

@ernie ernie commented on 708e094 Oct 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome change.

@adamphillips
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweeeeeeeeeet!

@stjhimy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome stuff

@sikachu
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

w00t! Thank you for putting this up :)

@andreacampi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And there was much rejoycing!

@davout
Copy link

@davout davout commented on 708e094 Oct 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats sweet,
is there some sort of plugin hook in order to find out at runtime which javascript framework is currently used in a given Rails app ?
I can think of a couple of examples where that would be really awesome to be able to read some parameter of that sort when initializing.

@asanghi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

..and here's hoping that in future the default too shall reflect the will of the majority.

@davout
Copy link

@davout davout commented on 708e094 Oct 12, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asanghi
open source != democracy

@elisehuard
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is nice, especially since we use jquery :)
however, wouldn't it make more sense to insulate such things into separate gems with (if needed) a well-defined API, like for the ORMs ?

@sikachu
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elisehuard

I think there's a gem that will add a generator for you: http://github.com/indirect/jquery-rails

@siong1987
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@elisehuard the reason that I wrote this patch is more and more people want to use jQuery as the default Javascript library for Rails. And, there are actually many jquery gem out there.

But, wouldn't it be nice that we can get it just by using a simple approach like with the "-j" parameter?

@jeroenvandijk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I'm happy to see this commit, as a JQuery user you would still need to add the jQuery file yourself, while prototype works out of the box.

Shouldn't we also add the latest jQuery file to rails if we go this path?

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The jquery.js file was added. Look at the top of this commit page.

@jeroenvandijk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim yeah I saw that too late, sorry. I tried to delete my comment, but that didn't work apparently :)

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At this point it was already in everyone's e-mail. ;)

Please sign in to comment.