Skip to content

Commit

Permalink
Merge remote branch 'drogus/plugin_new'
Browse files Browse the repository at this point in the history
Conflicts:
	railties/test/generators/app_generator_test.rb
  • Loading branch information
josevalim committed Nov 11, 2010
2 parents de2933e + cc135e3 commit f912a35
Show file tree
Hide file tree
Showing 57 changed files with 1,307 additions and 320 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railtie.rb
Expand Up @@ -14,7 +14,7 @@ class Railtie < Rails::Railtie
config.active_record = ActiveSupport::OrderedOptions.new

config.app_generators.orm :active_record, :migration => true,
:timestamps => true
:timestamps => true

config.app_middleware.insert_after "::ActionDispatch::Callbacks",
"ActiveRecord::QueryCache"
Expand Down
1 change: 1 addition & 0 deletions activesupport/lib/active_support/core_ext/hash.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/hash/conversions'
require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/hash/deep_dup'
require 'active_support/core_ext/hash/diff'
require 'active_support/core_ext/hash/except'
require 'active_support/core_ext/hash/indifferent_access'
Expand Down
11 changes: 11 additions & 0 deletions activesupport/lib/active_support/core_ext/hash/deep_dup.rb
@@ -0,0 +1,11 @@
class Hash
# Returns a deep copy of hash.
def deep_dup
duplicate = self.dup
duplicate.each_pair do |k,v|
tv = duplicate[k]
duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
end
duplicate
end
end
15 changes: 15 additions & 0 deletions activesupport/test/core_ext/hash_ext_test.rb
Expand Up @@ -329,6 +329,21 @@ def test_deep_merge_on_indifferent_access
assert_equal expected, hash_1
end

def test_deep_dup
hash = { :a => { :b => 'b' } }
dup = hash.deep_dup
dup[:a][:c] = 'c'
assert_equal nil, hash[:a][:c]
assert_equal 'c', dup[:a][:c]
end

def test_deep_dup_initialize
zero_hash = Hash.new 0
hash = { :a => zero_hash }
dup = hash.deep_dup
assert_equal 0, dup[:a][44]
end

def test_store_on_indifferent_access
hash = HashWithIndifferentAccess.new
hash.store(:test1, 1)
Expand Down
7 changes: 6 additions & 1 deletion railties/lib/rails/cli.rb
Expand Up @@ -8,4 +8,9 @@
require 'rails/ruby_version_check'
Signal.trap("INT") { puts; exit }

require 'rails/commands/application'
if ARGV.first == 'plugin'
ARGV.shift
require 'rails/commands/plugin_new'
else
require 'rails/commands/application'
end
16 changes: 16 additions & 0 deletions railties/lib/rails/commands/plugin_new.rb
@@ -0,0 +1,16 @@
require 'rails/version'
if %w(--version -v).include? ARGV.first
puts "Rails #{Rails::VERSION::STRING}"
exit(0)
end

if ARGV.first != "new"
ARGV[0] = "--help"
else
ARGV.shift
end

require 'rails/generators'
require 'rails/generators/rails/plugin_new/plugin_new_generator'

Rails::Generators::PluginNewGenerator.start
8 changes: 8 additions & 0 deletions railties/lib/rails/configuration.rb
@@ -1,5 +1,6 @@
require 'active_support/deprecation'
require 'active_support/ordered_options'
require 'active_support/core_ext/hash/deep_dup'
require 'rails/paths'
require 'rails/rack'

Expand Down Expand Up @@ -51,6 +52,13 @@ def initialize
@colorize_logging = true
end

def initialize_copy(source)
@aliases = @aliases.deep_dup
@options = @options.deep_dup
@fallbacks = @fallbacks.deep_dup
@templates = @templates.dup
end

def method_missing(method, *args)
method = method.to_s.sub(/=$/, '').to_sym

Expand Down
1 change: 1 addition & 0 deletions railties/lib/rails/engine/configuration.rb
Expand Up @@ -10,6 +10,7 @@ class Configuration < ::Rails::Railtie::Configuration
def initialize(root=nil)
super()
@root = root
@generators = app_generators.dup
end

# Returns the middleware stack for the engine.
Expand Down
176 changes: 176 additions & 0 deletions railties/lib/rails/generators/app_base.rb
@@ -0,0 +1,176 @@
require 'digest/md5'
require 'active_support/secure_random'
require 'rails/version' unless defined?(Rails::VERSION)
require 'rbconfig'
require 'open-uri'
require 'uri'

module Rails
module Generators
class AppBase < Base
DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
JAVASCRIPTS = %w( prototype jquery )

attr_accessor :rails_template
add_shebang_option!

argument :app_path, :type => :string

def self.add_shared_options_for(name)
class_option :builder, :type => :string, :aliases => "-b",
:desc => "Path to a #{name} builder (can be a filesystem path or URL)"

class_option :template, :type => :string, :aliases => "-m",
:desc => "Path to an #{name} template (can be a filesystem path or URL)"

class_option :skip_gemfile, :type => :boolean, :default => false,
:desc => "Don't create a Gemfile"

class_option :skip_git, :type => :boolean, :aliases => "-G", :default => false,
:desc => "Skip Git ignores and keeps"

class_option :skip_active_record, :type => :boolean, :aliases => "-O", :default => false,
:desc => "Skip Active Record files"

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 :skip_javascript, :type => :boolean, :aliases => "-J", :default => false,
:desc => "Skip javascript files"

class_option :dev, :type => :boolean, :default => false,
:desc => "Setup the #{name} with Gemfile pointing to your Rails checkout"

class_option :edge, :type => :boolean, :default => false,
:desc => "Setup the #{name} with Gemfile pointing to Rails repository"

class_option :skip_test_unit, :type => :boolean, :aliases => "-T", :default => false,
:desc => "Skip Test::Unit files"

class_option :help, :type => :boolean, :aliases => "-h", :group => :rails,
:desc => "Show this help message and quit"
end

def initialize(*args)
@original_wd = Dir.pwd

super
end

protected

def builder
@builder ||= begin
if path = options[:builder]
if URI(path).is_a?(URI::HTTP)
contents = open(path, "Accept" => "application/x-thor-template") {|io| io.read }
else
contents = open(File.expand_path(path, @original_wd)) {|io| io.read }
end

prok = eval("proc { #{contents} }", TOPLEVEL_BINDING, path, 1)
instance_eval(&prok)
end

builder_class = get_builder_class
builder_class.send(:include, ActionMethods)
builder_class.new(self)
end
end

def build(meth, *args)
builder.send(meth, *args) if builder.respond_to?(meth)
end

def create_root
self.destination_root = File.expand_path(app_path, destination_root)
valid_const?

empty_directory '.'
set_default_accessors!
FileUtils.cd(destination_root) unless options[:pretend]
end

def apply_rails_template
apply rails_template if rails_template
rescue Thor::Error, LoadError, Errno::ENOENT => e
raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
end

def set_default_accessors!
self.rails_template = case options[:template]
when /^http:\/\//
options[:template]
when String
File.expand_path(options[:template], Dir.pwd)
else
options[:template]
end
end

def database_gemfile_entry
entry = ""
unless options[:skip_active_record]
entry = "gem '#{gem_for_database}'"
entry << ", :require => '#{require_for_database}'" if require_for_database
end
entry
end

def rails_gemfile_entry
if options.dev?
<<-GEMFILE
gem 'rails', :path => '#{Rails::Generators::RAILS_DEV_PATH}'
gem 'arel', :git => 'git://github.com/rails/arel.git'
gem "rack", :git => "git://github.com/rack/rack.git"
GEMFILE
elsif options.edge?
<<-GEMFILE
gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'arel', :git => 'git://github.com/rails/arel.git'
gem "rack", :git => "git://github.com/rack/rack.git"
GEMFILE
else
<<-GEMFILE
gem 'rails', '#{Rails::VERSION::STRING}'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
# gem 'arel', :git => 'git://github.com/rails/arel.git'
# gem "rack", :git => "git://github.com/rack/rack.git"
GEMFILE
end
end

def gem_for_database
# %w( mysql oracle postgresql sqlite3 frontbase ibm_db )
case options[:database]
when "oracle" then "ruby-oci8"
when "postgresql" then "pg"
when "sqlite3" then "sqlite3-ruby"
when "frontbase" then "ruby-frontbase"
when "mysql" then "mysql2"
else options[:database]
end
end

def require_for_database
case options[:database]
when "sqlite3" then "sqlite3"
end
end

def bundle_if_dev_or_edge
bundle_command = File.basename(Thor::Util.ruby_command).sub(/ruby/, 'bundle')
run "#{bundle_command} install" if dev_or_edge?
end

def dev_or_edge?
options.dev? || options.edge?
end
end
end
end
@@ -1,3 +1,4 @@
<% without_namespacing do -%>
<%%= form_for(@<%= singular_table_name %>) do |f| %>
<%% if @<%= singular_table_name %>.errors.any? %>
<div id="error_explanation">
Expand All @@ -21,3 +22,4 @@
<%%= f.submit %>
</div>
<%% end %>
<% end -%>
@@ -1,6 +1,8 @@
<% without_namespacing do -%>
<h1>Editing <%= singular_table_name %></h1>

<%%= render 'form' %>

<%%= link_to 'Show', @<%= singular_table_name %> %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
@@ -1,3 +1,4 @@
<% without_namespacing do -%>
<h1>Listing <%= plural_table_name %></h1>

<table>
Expand Down Expand Up @@ -25,3 +26,4 @@
<br />

<%%= link_to 'New <%= human_name %>', new_<%= singular_table_name %>_path %>
<% end -%>
@@ -1,5 +1,7 @@
<% without_namespacing do -%>
<h1>New <%= singular_table_name %></h1>

<%%= render 'form' %>

<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
@@ -1,3 +1,4 @@
<% without_namespacing do -%>
<p id="notice"><%%= notice %></p>

<% for attribute in attributes -%>
Expand All @@ -10,3 +11,4 @@

<%%= link_to 'Edit', edit_<%= singular_table_name %>_path(@<%= singular_table_name %>) %> |
<%%= link_to 'Back', <%= index_helper %>_path %>
<% end -%>
8 changes: 7 additions & 1 deletion railties/lib/rails/generators/named_base.rb
Expand Up @@ -30,9 +30,15 @@ def module_namespacing(&block)
end
end

def without_namespacing(&block)
inside_namespace do
concat(capture(&block))
end
end

def indent(content, multiplier = 2)
spaces = " " * multiplier
content.each_line.map {|line| "#{spaces}#{line}" }.join("\n")
content = content.each_line.map {|line| "#{spaces}#{line}" }.join
end

def wrap_with_namespace(content)
Expand Down

0 comments on commit f912a35

Please sign in to comment.