Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 203 lines (167 sloc) 6.064 kb
c444b0f @josevalim ADdd missing requires (thanks to @arunagw)
josevalim authored
1 require 'active_support/core_ext/module/introspection'
f0dd77c Move railties/lib/* into railties/lib/*
Yehuda Katz + Carl Lerche authored
2 require 'rails/generators/base'
3 require 'rails/generators/generated_attribute'
301c48c @josevalim Added a generator file responsable for loading generator files.
josevalim authored
4
5 module Rails
6 module Generators
7 class NamedBase < Base
83f7fe2 @josevalim NamedBase.
josevalim authored
8 argument :name, :type => :string
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
9 class_option :skip_namespace, :type => :boolean, :default => false,
10 :desc => "Skip namespace (affects only isolated applications)"
83f7fe2 @josevalim NamedBase.
josevalim authored
11
22a3416 @sikachu Add --old-style-hash option to force creating old style hash on Ruby 1.9
sikachu authored
12 class_option :old_style_hash, :type => :boolean, :default => false,
13 :desc => "Force using old style hash (:foo => 'bar') on Ruby >= 1.9"
14
d627c93 @josevalim Pass config.generators options along when RAILS_GENERATORS is set and sh...
josevalim authored
15 def initialize(args, *options) #:nodoc:
9aa10a7 @amatsuda initialize @inside_template
amatsuda authored
16 @inside_template = nil
d627c93 @josevalim Pass config.generators options along when RAILS_GENERATORS is set and sh...
josevalim authored
17 # Unfreeze name in case it's given as a frozen string
18 args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
83f7fe2 @josevalim NamedBase.
josevalim authored
19 super
e084313 @josevalim Added HelperGenerator.
josevalim authored
20 assign_names!(self.name)
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
21 parse_attributes! if respond_to?(:attributes)
83f7fe2 @josevalim NamedBase.
josevalim authored
22 end
23
ced8ebc @drogus Change inside_namespace method to inside_template and use it in all temp...
drogus authored
24 no_tasks do
25 def template(source, *args, &block)
26 inside_template do
27 super
28 end
29 end
30 end
31
83f7fe2 @josevalim NamedBase.
josevalim authored
32 protected
0134c5c @drogus Refactor a few methods connected with namespacing in Rails::Generators::...
drogus authored
33 attr_reader :file_name
34 alias :singular_name :file_name
35
36 # Wrap block with namespace of current application
37 # if namespace exists and is not skipped
bac0826 @drogus Refactor namespaced generators to use module_namespacing block helper
drogus authored
38 def module_namespacing(&block)
ced8ebc @drogus Change inside_namespace method to inside_template and use it in all temp...
drogus authored
39 content = capture(&block)
40 content = wrap_with_namespace(content) if namespaced?
41 concat(content)
d0d30e7 @drogus Fix scaffold generator to be aware of namespace in isolated engine
drogus authored
42 end
43
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
44 def indent(content, multiplier = 2)
45 spaces = " " * multiplier
d0d30e7 @drogus Fix scaffold generator to be aware of namespace in isolated engine
drogus authored
46 content = content.each_line.map {|line| "#{spaces}#{line}" }.join
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
47 end
48
49 def wrap_with_namespace(content)
f0f978a @drogus Fix indentation and newlines in generated engine
drogus authored
50 content = indent(content).chomp
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
51 "module #{namespace.name}\n#{content}\nend\n"
52 end
53
ced8ebc @drogus Change inside_namespace method to inside_template and use it in all temp...
drogus authored
54 def inside_template
55 @inside_template = true
56 yield
0134c5c @drogus Refactor a few methods connected with namespacing in Rails::Generators::...
drogus authored
57 ensure
ced8ebc @drogus Change inside_namespace method to inside_template and use it in all temp...
drogus authored
58 @inside_template = false
59 end
60
61 def inside_template?
62 @inside_template
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
63 end
64
65 def namespace
f182831 @flippingbits Use namespace if it's a mountable engine
flippingbits authored
66 Rails::Generators.namespace
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
67 end
68
69 def namespaced?
4da6d95 @drogus Bye bye bang bang.
drogus authored
70 !options[:skip_namespace] && namespace
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
71 end
72
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
73 def file_path
74 @file_path ||= (class_path + [file_name]).join('/')
75 end
76
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
77 def class_path
ced8ebc @drogus Change inside_namespace method to inside_template and use it in all temp...
drogus authored
78 inside_template? || !namespaced? ? regular_class_path : namespaced_class_path
00aa13b @drogus Generators fix: properly check if module should be created when creating...
drogus authored
79 end
80
81 def regular_class_path
82 @class_path
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
83 end
84
b0f8355 @drogus Fix generators to help with ambiguous `ApplicationController` issue
drogus authored
85 def namespaced_file_path
86 @namespaced_file_path ||= namespaced_class_path.join("/")
87 end
88
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
89 def namespaced_class_path
90 @namespaced_class_path ||= begin
91 namespace_path = namespace.name.split("::").map {|m| m.underscore }
92 namespace_path + @class_path
93 end
94 end
95
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
96 def class_name
e836340 @drogus Generators are not aware of namespace of isolated engines and applicatio...
drogus authored
97 (class_path + [file_name]).map!{ |m| m.camelize }.join('::')
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
98 end
99
7a5da21 @josevalim Use human_name in scaffold.
josevalim authored
100 def human_name
101 @human_name ||= singular_name.humanize
102 end
103
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
104 def plural_name
105 @plural_name ||= singular_name.pluralize
106 end
107
108 def i18n_scope
109 @i18n_scope ||= file_path.gsub('/', '.')
110 end
111
112 def table_name
113 @table_name ||= begin
114 base = pluralize_table_names? ? plural_name : singular_name
115 (class_path + [base]).join('_')
83f7fe2 @josevalim NamedBase.
josevalim authored
116 end
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
117 end
83f7fe2 @josevalim NamedBase.
josevalim authored
118
ed3f042 @pixeltrix Make polymorphic_url and scaffolding work with uncountable resources [#3...
pixeltrix authored
119 def uncountable?
120 singular_name == plural_name
121 end
122
123 def index_helper
7008911 @siddick Patch for Namespace problem in Scaffold. [#4763 state:resolved]
siddick authored
124 uncountable? ? "#{plural_table_name}_index" : plural_table_name
125 end
126
127 def singular_table_name
c6fac7b @tukan Fixed issue #6363, avoid to pluralized already pluralized names and sing...
tukan authored
128 @singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name)
7008911 @siddick Patch for Namespace problem in Scaffold. [#4763 state:resolved]
siddick authored
129 end
130
131 def plural_table_name
c6fac7b @tukan Fixed issue #6363, avoid to pluralized already pluralized names and sing...
tukan authored
132 @plural_table_name ||= (pluralize_table_names? ? table_name : table_name.pluralize)
7008911 @siddick Patch for Namespace problem in Scaffold. [#4763 state:resolved]
siddick authored
133 end
134
135 def plural_file_name
136 @plural_file_name ||= file_name.pluralize
137 end
138
139 def route_url
140 @route_url ||= class_path.collect{|dname| "/" + dname }.join('') + "/" + plural_file_name
ed3f042 @pixeltrix Make polymorphic_url and scaffolding work with uncountable resources [#3...
pixeltrix authored
141 end
142
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
143 # Tries to retrieve the application name or simple return application.
144 def application_name
145 if defined?(Rails) && Rails.application
146 Rails.application.class.name.split('::').first.underscore
83f7fe2 @josevalim NamedBase.
josevalim authored
147 else
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
148 "application"
83f7fe2 @josevalim NamedBase.
josevalim authored
149 end
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
150 end
22b38c1 @hugopeixoto Fixed generating a namespaced model with table pluralization turned off....
hugopeixoto authored
151
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
152 def assign_names!(name) #:nodoc:
153 @class_path = name.include?('/') ? name.split('/') : name.split('::')
154 @class_path.map! { |m| m.underscore }
155 @file_name = @class_path.pop
83f7fe2 @josevalim NamedBase.
josevalim authored
156 end
157
9cb3ca1 Change mailer generator templates and refactor the whole naming schema.
José Valim and Mikel Lindsaar authored
158 # Convert attributes array into GeneratedAttribute objects.
2f3681d @josevalim Clean up class collisions check and a class method helper.
josevalim authored
159 def parse_attributes! #:nodoc:
7f5b516 @german added ability to specify from cli when generating a model/migration whet...
german authored
160 self.attributes = (attributes || []).map do |attr|
557014d @josevalim Tidy up migration types.
josevalim authored
161 Rails::Generators::GeneratedAttribute.parse(attr)
75fbd73 @josevalim Add GeneratedAttribute.
josevalim authored
162 end
163 end
164
7f7afdf @josevalim Rename ControllerNamedBase to ScaffoldBase and move ActionORM helpers.
josevalim authored
165 def pluralize_table_names?
166 !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
167 end
168
b77a7dc @josevalim Added controller named base.
josevalim authored
169 # Add a class collisions name to be checked on class initialization. You
170 # can supply a hash with a :prefix or :suffix to be tested.
171 #
172 # ==== Examples
173 #
174 # check_class_collision :suffix => "Observer"
2f3681d @josevalim Clean up class collisions check and a class method helper.
josevalim authored
175 #
b77a7dc @josevalim Added controller named base.
josevalim authored
176 # If the generator is invoked with class name Admin, it will check for
177 # the presence of "AdminObserver".
178 #
179 def self.check_class_collision(options={})
180 define_method :check_class_collision do
7f7afdf @josevalim Rename ControllerNamedBase to ScaffoldBase and move ActionORM helpers.
josevalim authored
181 name = if self.respond_to?(:controller_class_name) # for ScaffoldBase
b77a7dc @josevalim Added controller named base.
josevalim authored
182 controller_class_name
183 else
184 class_name
185 end
186
187 class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}"
188 end
2f3681d @josevalim Clean up class collisions check and a class method helper.
josevalim authored
189 end
63cd92f @sikachu Rails will now generate Ruby 1.9 style hash when running scaffold_contro...
sikachu authored
190
191 # Returns Ruby 1.9 style key-value pair if current code is running on
192 # Ruby 1.9.x. Returns the old-style (with hash rocket) otherwise.
193 def key_value(key, value)
22a3416 @sikachu Add --old-style-hash option to force creating old style hash on Ruby 1.9
sikachu authored
194 if options[:old_style_hash] || RUBY_VERSION < '1.9'
63cd92f @sikachu Rails will now generate Ruby 1.9 style hash when running scaffold_contro...
sikachu authored
195 ":#{key} => #{value}"
196 else
197 "#{key}: #{value}"
198 end
199 end
b77a7dc @josevalim Added controller named base.
josevalim authored
200 end
301c48c @josevalim Added a generator file responsable for loading generator files.
josevalim authored
201 end
202 end
Something went wrong with that request. Please try again.