diff options
Diffstat (limited to 'railties/lib/rails/generators/named_base.rb')
-rw-r--r-- | railties/lib/rails/generators/named_base.rb | 79 |
1 files changed, 34 insertions, 45 deletions
diff --git a/railties/lib/rails/generators/named_base.rb b/railties/lib/rails/generators/named_base.rb index 3e851bf888..12e918731e 100644 --- a/railties/lib/rails/generators/named_base.rb +++ b/railties/lib/rails/generators/named_base.rb @@ -6,17 +6,9 @@ module Rails class NamedBase < Base argument :name, :type => :string - no_tasks { - attr_reader :class_name, :singular_name, :plural_name, :table_name, - :class_path, :file_path, :class_nesting_depth - - alias :file_name :singular_name - } - def initialize(args, *options) #:nodoc: # Unfreeze name in case it's given as a frozen string args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen? - super assign_names!(self.name) parse_attributes! if respond_to?(:attributes) @@ -24,28 +16,48 @@ module Rails protected - def assign_names!(given_name) #:nodoc: - base_name, @class_path, @file_path, class_nesting, @class_nesting_depth = extract_modules(given_name) - class_name_without_nesting, @singular_name, @plural_name = inflect_names(base_name) + attr_reader :class_path, :file_name + alias :singular_name :file_name - @table_name = if pluralize_table_names? - plural_name - else - singular_name + def file_path + @file_path ||= (class_path + [file_name]).join('/') + end + + def class_name + @class_name ||= (class_path + [file_name]).map!{ |m| m.camelize }.join('::') + end + + def plural_name + @plural_name ||= singular_name.pluralize + end + + def i18n_scope + @i18n_scope ||= file_path.gsub('/', '.') + end + + def table_name + @table_name ||= begin + base = pluralize_table_names? ? plural_name : singular_name + (class_path + [base]).join('_') end + end - if class_nesting.empty? - @class_name = class_name_without_nesting + # Tries to retrieve the application name or simple return application. + def application_name + if defined?(Rails) && Rails.application + Rails.application.class.name.split('::').first.underscore else - @table_name = class_nesting.underscore << "_" << @table_name - @class_name = "#{class_nesting}::#{class_name_without_nesting}" + "application" end + end - @table_name.gsub!('/', '_') + def assign_names!(name) #:nodoc: + @class_path = name.include?('/') ? name.split('/') : name.split('::') + @class_path.map! { |m| m.underscore } + @file_name = @class_path.pop end - # Convert attributes hash into an array with GeneratedAttribute objects. - # + # Convert attributes array into GeneratedAttribute objects. def parse_attributes! #:nodoc: self.attributes = (attributes || []).map do |key_value| name, type = key_value.split(':') @@ -53,29 +65,6 @@ module Rails end end - # Extract modules from filesystem-style or ruby-style path. Both - # good/fun/stuff and Good::Fun::Stuff produce the same results. - # - def extract_modules(name) #:nodoc: - modules = name.include?('/') ? name.split('/') : name.split('::') - name = modules.pop - path = modules.map { |m| m.underscore } - - file_path = (path + [name.underscore]).join('/') - nesting = modules.map { |m| m.camelize }.join('::') - - [name, path, file_path, nesting, modules.size] - end - - # Receives name and return camelized, underscored and pluralized names. - # - def inflect_names(name) #:nodoc: - camel = name.camelize - under = camel.underscore - plural = under.pluralize - [camel, under, plural] - end - def pluralize_table_names? !defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names end |