rails 0.9.5 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rails might be problematic. Click here for more details.
- data/CHANGELOG +46 -0
- data/README +36 -8
- data/Rakefile +18 -25
- data/bin/console +17 -30
- data/bin/console_sandbox.rb +6 -0
- data/bin/destroy +5 -0
- data/bin/generate +2 -70
- data/bin/rails +4 -28
- data/bin/server +4 -2
- data/bin/update +5 -0
- data/configs/apache.conf +7 -54
- data/configs/empty.log +0 -0
- data/configs/routes.rb +15 -0
- data/environments/shared.rb +14 -6
- data/environments/shared_for_gem.rb +12 -6
- data/fresh_rakefile +40 -8
- data/html/index.html +70 -1
- data/lib/breakpoint.rb +6 -1
- data/lib/breakpoint_client.rb +196 -193
- data/lib/dispatcher.rb +16 -38
- data/lib/rails_generator.rb +39 -198
- data/lib/rails_generator/base.rb +203 -0
- data/lib/rails_generator/commands.rb +409 -0
- data/lib/rails_generator/generators/applications/app/USAGE +16 -0
- data/lib/rails_generator/generators/applications/app/app_generator.rb +120 -0
- data/lib/rails_generator/generators/components/controller/USAGE +30 -0
- data/lib/rails_generator/generators/components/controller/controller_generator.rb +37 -0
- data/{generators → lib/rails_generator/generators/components}/controller/templates/controller.rb +1 -1
- data/lib/rails_generator/generators/components/controller/templates/functional_test.rb +18 -0
- data/{generators → lib/rails_generator/generators/components}/controller/templates/helper.rb +0 -0
- data/{generators → lib/rails_generator/generators/components}/controller/templates/view.rhtml +0 -0
- data/lib/rails_generator/generators/components/mailer/USAGE +19 -0
- data/lib/rails_generator/generators/components/mailer/mailer_generator.rb +32 -0
- data/{generators → lib/rails_generator/generators/components}/mailer/templates/fixture.rhtml +0 -0
- data/{generators → lib/rails_generator/generators/components}/mailer/templates/mailer.rb +0 -0
- data/{generators → lib/rails_generator/generators/components}/mailer/templates/unit_test.rb +7 -1
- data/{generators → lib/rails_generator/generators/components}/mailer/templates/view.rhtml +0 -0
- data/lib/rails_generator/generators/components/model/USAGE +17 -0
- data/lib/rails_generator/generators/components/model/model_generator.rb +18 -0
- data/{generators/scaffold → lib/rails_generator/generators/components/model}/templates/fixtures.yml +0 -2
- data/{generators → lib/rails_generator/generators/components}/model/templates/model.rb +0 -0
- data/{generators → lib/rails_generator/generators/components}/model/templates/unit_test.rb +5 -1
- data/lib/rails_generator/generators/components/scaffold/USAGE +32 -0
- data/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb +178 -0
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/controller.rb +1 -1
- data/lib/rails_generator/generators/components/scaffold/templates/form.rhtml +5 -0
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/functional_test.rb +7 -6
- data/lib/rails_generator/generators/components/scaffold/templates/helper.rb +2 -0
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/layout.rhtml +1 -1
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/style.css +17 -17
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/view_edit.rhtml +1 -1
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/view_list.rhtml +1 -1
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/view_new.rhtml +1 -1
- data/{generators → lib/rails_generator/generators/components}/scaffold/templates/view_show.rhtml +0 -0
- data/lib/rails_generator/lookup.rb +200 -0
- data/lib/rails_generator/manifest.rb +53 -0
- data/lib/rails_generator/options.rb +134 -0
- data/lib/rails_generator/scripts.rb +83 -0
- data/lib/rails_generator/scripts/destroy.rb +7 -0
- data/lib/rails_generator/scripts/generate.rb +7 -0
- data/lib/rails_generator/scripts/update.rb +12 -0
- data/lib/rails_generator/simple_logger.rb +46 -0
- data/lib/rails_generator/spec.rb +44 -0
- data/lib/webrick_server.rb +15 -65
- metadata +92 -48
- data/doc/apache_protection +0 -3
- data/doc/index.html +0 -70
- data/generators/controller/USAGE +0 -28
- data/generators/controller/controller_generator.rb +0 -26
- data/generators/controller/templates/functional_test.rb +0 -17
- data/generators/mailer/USAGE +0 -27
- data/generators/mailer/mailer_generator.rb +0 -22
- data/generators/model/USAGE +0 -17
- data/generators/model/model_generator.rb +0 -10
- data/generators/model/templates/fixtures.yml +0 -1
- data/generators/scaffold/USAGE +0 -27
- data/generators/scaffold/scaffold_generator.rb +0 -60
data/lib/dispatcher.rb
CHANGED
@@ -24,56 +24,34 @@
|
|
24
24
|
require 'breakpoint'
|
25
25
|
|
26
26
|
class Dispatcher
|
27
|
-
class <<self
|
27
|
+
class << self
|
28
28
|
def dispatch(cgi = CGI.new, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS)
|
29
29
|
begin
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
response = ActionController::CgiResponse.new(cgi)
|
34
|
-
|
35
|
-
controller_name, module_name = controller_name(request.parameters), module_name(request.parameters)
|
36
|
-
|
37
|
-
require_or_load("application")
|
38
|
-
require_or_load(controller_path(controller_name, module_name))
|
39
|
-
|
40
|
-
controller_class(controller_name).process(request, response).out
|
30
|
+
request, response = ActionController::CgiRequest.new(cgi, session_options), ActionController::CgiResponse.new(cgi)
|
31
|
+
prepare_application
|
32
|
+
ActionController::Routing::Routes.recognize!(request).process(request, response).out
|
41
33
|
rescue Object => exception
|
42
34
|
ActionController::Base.process_with_exception(request, response, exception).out
|
43
35
|
ensure
|
44
|
-
reset_application
|
45
|
-
Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
|
36
|
+
reset_application
|
46
37
|
end
|
47
38
|
end
|
48
39
|
|
49
40
|
private
|
50
|
-
def
|
51
|
-
Dependencies.
|
52
|
-
|
41
|
+
def prepare_application
|
42
|
+
ActionController::Routing::Routes.reload if Dependencies.load?
|
43
|
+
Breakpoint.activate_drb("druby://localhost:#{BREAKPOINT_SERVER_PORT}", nil, !defined?(FastCGI)) if defined?(BREAKPOINT_SERVER_PORT)
|
44
|
+
Controllers.const_load!(:ApplicationController, "application") unless Controllers.const_defined?(:ApplicationController)
|
53
45
|
end
|
54
46
|
|
55
|
-
def
|
56
|
-
if
|
57
|
-
|
58
|
-
|
59
|
-
|
47
|
+
def reset_application
|
48
|
+
if Dependencies.load?
|
49
|
+
Controllers.clear!
|
50
|
+
Dependencies.clear
|
51
|
+
Dependencies.remove_subclasses_for(ActiveRecord::Base, ActiveRecord::Observer, ActionController::Base)
|
60
52
|
end
|
61
|
-
end
|
62
|
-
|
63
|
-
def controller_class(controller_name)
|
64
|
-
Object.const_get(controller_class_name(controller_name))
|
65
|
-
end
|
66
|
-
|
67
|
-
def controller_class_name(controller_name)
|
68
|
-
"#{controller_name.camelize}Controller"
|
69
|
-
end
|
70
|
-
|
71
|
-
def controller_name(parameters)
|
72
|
-
parameters["controller"].downcase.gsub(/[^_a-zA-Z0-9]/, "").untaint
|
73
|
-
end
|
74
53
|
|
75
|
-
|
76
|
-
parameters["module"].downcase.gsub(/[^_a-zA-Z0-9]/, "").untaint if parameters["module"]
|
54
|
+
Breakpoint.deactivate_drb if defined?(BREAKPOINT_SERVER_PORT)
|
77
55
|
end
|
78
56
|
end
|
79
|
-
end
|
57
|
+
end
|
data/lib/rails_generator.rb
CHANGED
@@ -1,201 +1,42 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
# Require class file according to naming convention.
|
34
|
-
require "#{path}/#{name}_generator.rb"
|
35
|
-
|
36
|
-
# Find class according to naming convention. Allow Nesting::In::Modules.
|
37
|
-
class_name = Inflector.classify("#{name}_generator")
|
38
|
-
unless klass = find_generator_class(name)
|
39
|
-
raise GeneratorError, "no #{class_name} class defined in #{path}/#{name}_generator.rb"
|
40
|
-
end
|
41
|
-
|
42
|
-
# Instantiate and return generator.
|
43
|
-
klass.new(template_root, RAILS_ROOT, search_paths, args)
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
def builtin_generators
|
48
|
-
generators([BUILTIN_ROOT])
|
49
|
-
end
|
50
|
-
|
51
|
-
def contrib_generators
|
52
|
-
generators([CONTRIB_ROOT])
|
53
|
-
end
|
54
|
-
|
55
|
-
def generators(search_paths)
|
56
|
-
generator_paths(search_paths).keys.uniq.sort
|
57
|
-
end
|
58
|
-
|
59
|
-
# Find all generator paths.
|
60
|
-
def generator_paths(search_paths)
|
61
|
-
@paths ||= {}
|
62
|
-
unless @paths[search_paths]
|
63
|
-
paths = Hash.new { |h,k| h[k] = [] }
|
64
|
-
search_paths.each do |path|
|
65
|
-
Dir["#{path}/[a-z]*"].each do |dir|
|
66
|
-
paths[File.basename(dir)] << dir if File.directory?(dir)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
@paths[search_paths] = paths
|
70
|
-
end
|
71
|
-
@paths[search_paths]
|
72
|
-
end
|
73
|
-
|
74
|
-
def find_generator_path(name, search_paths)
|
75
|
-
generator_paths(search_paths)[name].first
|
76
|
-
end
|
77
|
-
|
78
|
-
# Find all generator classes.
|
79
|
-
def generator_classes
|
80
|
-
classes = Hash.new { |h,k| h[k] = [] }
|
81
|
-
class_re = /([^:]+)Generator$/
|
82
|
-
ObjectSpace.each_object(Class) do |object|
|
83
|
-
if md = class_re.match(object.name) and object < Rails::Generator::Base
|
84
|
-
classes[Inflector.underscore(md.captures.first)] << object
|
85
|
-
end
|
86
|
-
end
|
87
|
-
classes
|
88
|
-
end
|
89
|
-
|
90
|
-
def find_generator_class(name)
|
91
|
-
generator_classes[name].first
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
|
96
|
-
# Talk about generators.
|
97
|
-
class Base
|
98
|
-
attr_reader :template_root, :destination_root, :args, :options,
|
99
|
-
:class_name, :singular_name, :plural_name
|
100
|
-
|
101
|
-
alias_method :file_name, :singular_name
|
102
|
-
alias_method :table_name, :plural_name
|
103
|
-
|
104
|
-
def self.generator_name
|
105
|
-
Inflector.underscore(name.gsub('Generator', ''))
|
106
|
-
end
|
107
|
-
|
108
|
-
def initialize(template_root, destination_root, search_paths, args)
|
109
|
-
@template_root, @destination_root = template_root, destination_root
|
110
|
-
usage if args.empty?
|
111
|
-
@search_paths, @original_args = search_paths, args.dup
|
112
|
-
@class_name, @singular_name, @plural_name = inflect_names(args.shift)
|
113
|
-
@options = extract_options!(args)
|
114
|
-
@args = args
|
115
|
-
end
|
116
|
-
|
117
|
-
# Checks whether the class name that was assigned to this generator
|
118
|
-
# would cause a collision with a Class, Module or other constant
|
119
|
-
# that is already used up by Ruby or RubyOnRails.
|
120
|
-
def collision_with_builtin?
|
121
|
-
builtin = Object.const_get(full_class_name) rescue nil
|
122
|
-
type = case builtin
|
123
|
-
when Class: "Class"
|
124
|
-
when Module: "Module"
|
125
|
-
else "Constant"
|
126
|
-
end
|
127
|
-
|
128
|
-
if builtin then
|
129
|
-
"Sorry, you can't have a #{self.class.generator_name} named " +
|
130
|
-
"'#{full_class_name}' because Ruby or Rails already has a #{type} with that name.\n" +
|
131
|
-
"Please rerun the generator with a different name."
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
# Returns the complete name that the resulting Class would have.
|
136
|
-
# Used in collision_with_builtin(). The default guess is that it is
|
137
|
-
# the same as class_name. Override this in your generator in case
|
138
|
-
# it is wrong.
|
139
|
-
def full_class_name
|
140
|
-
class_name
|
141
|
-
end
|
142
|
-
|
143
|
-
protected
|
144
|
-
# Look up another generator with the same arguments.
|
145
|
-
def generator(name)
|
146
|
-
Rails::Generator.instance(name, @original_args, @search_paths)
|
147
|
-
end
|
148
|
-
|
149
|
-
# Generate a file for a Rails application using an ERuby template.
|
150
|
-
# Looks up and evalutes a template by name and writes the result
|
151
|
-
# to a file relative to +destination_root+. The template
|
152
|
-
# is evaluated in the context of the optional eval_binding argument.
|
153
|
-
#
|
154
|
-
# The ERB template uses explicit trim mode to best control the
|
155
|
-
# proliferation of whitespace in generated code. <%- trims leading
|
156
|
-
# whitespace; -%> trims trailing whitespace including one newline.
|
157
|
-
def template(template_name, destination_path, eval_binding = nil)
|
158
|
-
# Determine full paths for source and destination files.
|
159
|
-
template_path = find_template_path(template_name)
|
160
|
-
destination_path = File.join(destination_root, destination_path)
|
161
|
-
|
162
|
-
# Create destination directories.
|
163
|
-
FileUtils.mkdir_p(File.dirname(destination_path))
|
164
|
-
|
165
|
-
# Render template and write result.
|
166
|
-
eval_binding ||= binding
|
167
|
-
contents = ERB.new(File.read(template_path), nil, '-').result(eval_binding)
|
168
|
-
File.open(destination_path, 'w') { |file| file.write(contents) }
|
169
|
-
end
|
170
|
-
|
171
|
-
def usage
|
172
|
-
raise UsageError.new, File.read(usage_path)
|
173
|
-
end
|
174
|
-
|
175
|
-
private
|
176
|
-
def find_template_path(template_name)
|
177
|
-
name, path = template_name.split('/', 2)
|
178
|
-
if path.nil?
|
179
|
-
File.join(template_root, name)
|
180
|
-
elsif generator_path = Rails::Generator.find_generator_path(name, @search_paths)
|
181
|
-
File.join(generator_path, 'templates', path)
|
182
|
-
end
|
183
|
-
end
|
1
|
+
#--
|
2
|
+
# Copyright (c) 2004 Jeremy Kemper
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
5
|
+
# a copy of this software and associated documentation files (the
|
6
|
+
# "Software"), to deal in the Software without restriction, including
|
7
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
8
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
9
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
10
|
+
# the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be
|
13
|
+
# included in all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
17
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
19
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
20
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
21
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
#++
|
23
|
+
|
24
|
+
$:.unshift(File.dirname(__FILE__))
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'active_support'
|
28
|
+
rescue LoadError
|
29
|
+
require 'rubygems'
|
30
|
+
require_gem 'activesupport'
|
31
|
+
end
|
184
32
|
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
plural = Inflector.pluralize(under)
|
189
|
-
[camel, under, plural]
|
190
|
-
end
|
33
|
+
require 'rails_generator/base'
|
34
|
+
require 'rails_generator/lookup'
|
35
|
+
require 'rails_generator/commands'
|
191
36
|
|
192
|
-
|
193
|
-
|
194
|
-
end
|
37
|
+
Rails::Generator::Base.send(:include, Rails::Generator::Lookup)
|
38
|
+
Rails::Generator::Base.send(:include, Rails::Generator::Commands)
|
195
39
|
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
end
|
200
|
-
end
|
201
|
-
end
|
40
|
+
# Set up a default logger for convenience.
|
41
|
+
require 'rails_generator/simple_logger'
|
42
|
+
Rails::Generator::Base.logger = Rails::Generator::SimpleLogger.new(STDOUT)
|
@@ -0,0 +1,203 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/options'
|
2
|
+
require File.dirname(__FILE__) + '/manifest'
|
3
|
+
require File.dirname(__FILE__) + '/spec'
|
4
|
+
|
5
|
+
# Rails::Generator is a code generation platform tailored for the Rails
|
6
|
+
# web application framework. Generators are easily invoked within Rails
|
7
|
+
# applications to add and remove components such as models and controllers.
|
8
|
+
# New generators are easy to create and may be distributed as RubyGems or
|
9
|
+
# tarballs for inclusion system-wide, per-user, or per-application.
|
10
|
+
#
|
11
|
+
# Generators may subclass other generators to provide variations that
|
12
|
+
# require little or no new logic but replace the template files.
|
13
|
+
# The postback generator is an example: it subclasses the scaffold
|
14
|
+
# generator and just replaces the code templates with its own.
|
15
|
+
#
|
16
|
+
# Now go forth and multiply^Wgenerate.
|
17
|
+
module Rails
|
18
|
+
module Generator
|
19
|
+
class GeneratorError < StandardError; end
|
20
|
+
class UsageError < GeneratorError; end
|
21
|
+
|
22
|
+
|
23
|
+
# The base code generator is bare-bones. It sets up the source and
|
24
|
+
# destination paths and tells the logger whether to keep its trap shut.
|
25
|
+
# You're probably looking for NamedBase, a subclass meant for generating
|
26
|
+
# "named" components such as models, controllers, and mailers.
|
27
|
+
#
|
28
|
+
# Generators create a manifest of the actions they perform then hand
|
29
|
+
# the manifest to a command which replay the actions to do the heavy
|
30
|
+
# lifting. Create, destroy, and list commands are included. Since a
|
31
|
+
# single manifest may be used by any command, creating new generators is
|
32
|
+
# as simple as writing some code templates and declaring what you'd like
|
33
|
+
# to do with them.
|
34
|
+
#
|
35
|
+
# The manifest method must be implemented by subclasses, returning a
|
36
|
+
# Rails::Generator::Manifest. The record method is provided as a
|
37
|
+
# convenience for manifest creation. Example:
|
38
|
+
# class EliteGenerator < Rails::Generator::Base
|
39
|
+
# def manifest
|
40
|
+
# record do |m|
|
41
|
+
# m.do(some)
|
42
|
+
# m.things(in) { here }
|
43
|
+
# end
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
class Base
|
47
|
+
include Options
|
48
|
+
|
49
|
+
# Declare default options for the generator. These options
|
50
|
+
# are inherited to subclasses.
|
51
|
+
default_options :collision => :ask, :quiet => false
|
52
|
+
|
53
|
+
# A logger instance available everywhere in the generator.
|
54
|
+
cattr_accessor :logger
|
55
|
+
|
56
|
+
# Every generator that is dynamically looked up is tagged with a
|
57
|
+
# Spec describing where it was found.
|
58
|
+
class_inheritable_accessor :spec
|
59
|
+
|
60
|
+
attr_reader :source_root, :destination_root, :args
|
61
|
+
|
62
|
+
def initialize(runtime_args, runtime_options = {})
|
63
|
+
@args = runtime_args
|
64
|
+
parse!(@args, runtime_options)
|
65
|
+
|
66
|
+
# Derive source and destination paths.
|
67
|
+
@source_root = options[:source] || File.join(spec.path, 'templates')
|
68
|
+
if options[:destination]
|
69
|
+
@destination_root = options[:destination]
|
70
|
+
elsif defined? ::RAILS_ROOT
|
71
|
+
@destination_root = ::RAILS_ROOT
|
72
|
+
end
|
73
|
+
|
74
|
+
# Silence the logger if requested.
|
75
|
+
logger.quiet = options[:quiet]
|
76
|
+
|
77
|
+
# Raise usage error if help is requested.
|
78
|
+
usage if options[:help]
|
79
|
+
end
|
80
|
+
|
81
|
+
# Generators must provide a manifest. Use the record method to create
|
82
|
+
# a new manifest and record your generator's actions.
|
83
|
+
def manifest
|
84
|
+
raise NotImplementedError, "No manifest for '#{spec.name}' generator."
|
85
|
+
end
|
86
|
+
|
87
|
+
# Return the full path from the source root for the given path.
|
88
|
+
# Example for source_root = '/source':
|
89
|
+
# source_path('some/path.rb') == '/source/some/path.rb'
|
90
|
+
#
|
91
|
+
# The given path may include a colon ':' character to indicate that
|
92
|
+
# the file belongs to another generator. This notation allows any
|
93
|
+
# generator to borrow files from another. Example:
|
94
|
+
# source_path('model:fixture.yml') = '/model/source/path/fixture.yml'
|
95
|
+
def source_path(relative_source)
|
96
|
+
# Check whether we're referring to another generator's file.
|
97
|
+
name, path = relative_source.split(':', 2)
|
98
|
+
|
99
|
+
# If not, return the full path to our source file.
|
100
|
+
if path.nil?
|
101
|
+
File.join(source_root, name)
|
102
|
+
|
103
|
+
# Otherwise, ask our referral for the file.
|
104
|
+
else
|
105
|
+
# FIXME: this is broken, though almost always true. Others'
|
106
|
+
# source_root are not necessarily the templates dir.
|
107
|
+
File.join(self.class.lookup(name).path, 'templates', path)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# Return the full path from the destination root for the given path.
|
112
|
+
# Example for destination_root = '/dest':
|
113
|
+
# destination_path('some/path.rb') == '/dest/some/path.rb'
|
114
|
+
def destination_path(relative_destination)
|
115
|
+
File.join(destination_root, relative_destination)
|
116
|
+
end
|
117
|
+
|
118
|
+
protected
|
119
|
+
# Convenience method for generator subclasses to record a manifest.
|
120
|
+
def record
|
121
|
+
Rails::Generator::Manifest.new(self) { |m| yield m }
|
122
|
+
end
|
123
|
+
|
124
|
+
# Override with your own usage banner.
|
125
|
+
def banner
|
126
|
+
"Usage: #{$0} #{spec.name} [options]"
|
127
|
+
end
|
128
|
+
|
129
|
+
# Read USAGE from file in generator base path.
|
130
|
+
def usage_message
|
131
|
+
File.read(File.join(spec.path, 'USAGE')) rescue ''
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
# The base generator for named components: models, controllers, mailers,
|
137
|
+
# etc. The target name is taken as the first argument and inflected to
|
138
|
+
# singular, plural, class, file, and table forms for your convenience.
|
139
|
+
# The remaining arguments are aliased to actions for controller and
|
140
|
+
# mailer convenience.
|
141
|
+
#
|
142
|
+
# If no name is provided, the generator raises a usage error with content
|
143
|
+
# optionally read from the USAGE file in the generator's base path.
|
144
|
+
#
|
145
|
+
# See Rails::Generator::Base for a discussion of Manifests and Commands.
|
146
|
+
class NamedBase < Base
|
147
|
+
attr_reader :name, :class_name, :singular_name, :plural_name
|
148
|
+
attr_reader :class_path, :file_path, :class_nesting, :class_nesting_depth
|
149
|
+
alias_method :file_name, :singular_name
|
150
|
+
alias_method :table_name, :plural_name
|
151
|
+
alias_method :actions, :args
|
152
|
+
|
153
|
+
def initialize(runtime_args, runtime_options = {})
|
154
|
+
super
|
155
|
+
|
156
|
+
# Name argument is required.
|
157
|
+
usage if runtime_args.empty?
|
158
|
+
|
159
|
+
@args = runtime_args.dup
|
160
|
+
base_name = @args.shift
|
161
|
+
assign_names!(base_name)
|
162
|
+
end
|
163
|
+
|
164
|
+
protected
|
165
|
+
# Override with your own usage banner.
|
166
|
+
def banner
|
167
|
+
"Usage: #{$0} #{spec.name} #{spec.name.camelize}Name [options]"
|
168
|
+
end
|
169
|
+
|
170
|
+
private
|
171
|
+
def assign_names!(name)
|
172
|
+
@name = name
|
173
|
+
base_name, @class_path, @file_path, @class_nesting, @class_nesting_depth = extract_modules(@name)
|
174
|
+
@class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name)
|
175
|
+
if @class_nesting.empty?
|
176
|
+
@class_name = @class_name_without_nesting
|
177
|
+
else
|
178
|
+
@class_name = "#{@class_nesting}::#{@class_name_without_nesting}"
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
# Extract modules from filesystem-style or ruby-style path:
|
183
|
+
# good/fun/stuff
|
184
|
+
# Good::Fun::Stuff
|
185
|
+
# produce the same results.
|
186
|
+
def extract_modules(name)
|
187
|
+
modules = name.include?('/') ? name.split('/') : name.split('::')
|
188
|
+
name = modules.pop
|
189
|
+
path = modules.map { |m| m.underscore }
|
190
|
+
file_path = (path + [name.underscore]).join('/')
|
191
|
+
nesting = modules.map { |m| m.camelize }.join('::')
|
192
|
+
[name, path, file_path, nesting, modules.size]
|
193
|
+
end
|
194
|
+
|
195
|
+
def inflect_names(name)
|
196
|
+
camel = name.camelize
|
197
|
+
under = camel.underscore
|
198
|
+
plural = under.pluralize
|
199
|
+
[camel, under, plural]
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
end
|