2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-21 09:08:33 -04:00
|
|
|
require "rails/generators/base"
|
|
|
|
require "rails/generators/generated_attribute"
|
2009-06-23 10:19:23 -04:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
class NamedBase < Base
|
2012-10-14 06:03:39 -04:00
|
|
|
argument :name, type: :string
|
2009-06-23 10:50:21 -04:00
|
|
|
|
2009-11-03 21:14:44 -05:00
|
|
|
def initialize(args, *options) #:nodoc:
|
2011-05-18 07:48:54 -04:00
|
|
|
@inside_template = nil
|
2009-11-03 21:14:44 -05:00
|
|
|
# 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?
|
2009-06-23 10:50:21 -04:00
|
|
|
super
|
2016-08-07 19:05:28 -04:00
|
|
|
assign_names!(name)
|
2009-06-23 12:12:37 -04:00
|
|
|
parse_attributes! if respond_to?(:attributes)
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
|
|
|
|
2015-06-15 00:16:43 -04:00
|
|
|
# Overrides <tt>Thor::Actions#template</tt> so it can tell if
|
|
|
|
# a template is currently being created.
|
2010-11-15 23:39:28 -05:00
|
|
|
no_tasks do
|
|
|
|
def template(source, *args, &block)
|
|
|
|
inside_template do
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
2016-05-08 10:22:43 -04:00
|
|
|
|
|
|
|
def js_template(source, destination)
|
2016-08-06 13:15:47 -04:00
|
|
|
template(source + ".js", destination + ".js")
|
2016-05-08 10:22:43 -04:00
|
|
|
end
|
2010-11-15 23:39:28 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
private
|
2018-02-16 20:14:27 -05:00
|
|
|
attr_reader :file_name
|
2016-12-23 05:20:01 -05:00
|
|
|
|
2014-05-24 12:34:16 -04:00
|
|
|
# FIXME: We are avoiding to use alias because a bug on thor that make
|
|
|
|
# this method public and add it to the task list.
|
2016-12-23 05:20:01 -05:00
|
|
|
def singular_name # :doc:
|
2013-01-25 23:27:33 -05:00
|
|
|
file_name
|
|
|
|
end
|
2010-09-25 07:41:42 -04:00
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def inside_template # :doc:
|
2010-11-15 23:39:28 -05:00
|
|
|
@inside_template = true
|
|
|
|
yield
|
2010-09-25 07:41:42 -04:00
|
|
|
ensure
|
2010-11-15 23:39:28 -05:00
|
|
|
@inside_template = false
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def inside_template? # :doc:
|
2010-11-15 23:39:28 -05:00
|
|
|
@inside_template
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def file_path # :doc:
|
2016-08-06 13:15:47 -04:00
|
|
|
@file_path ||= (class_path + [file_name]).join("/")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def class_path # :doc:
|
2010-11-15 23:39:28 -05:00
|
|
|
inside_template? || !namespaced? ? regular_class_path : namespaced_class_path
|
2010-09-24 15:54:16 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def regular_class_path # :doc:
|
2010-09-24 15:54:16 -04:00
|
|
|
@class_path
|
2010-09-23 13:13:07 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def namespaced_class_path # :doc:
|
2017-01-04 09:55:15 -05:00
|
|
|
@namespaced_class_path ||= namespace_dirs + @class_path
|
2012-06-06 05:02:11 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def class_name # :doc:
|
2016-08-06 13:15:47 -04:00
|
|
|
(class_path + [file_name]).map!(&:camelize).join("::")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def human_name # :doc:
|
2010-03-16 19:08:59 -04:00
|
|
|
@human_name ||= singular_name.humanize
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def plural_name # :doc:
|
2010-01-25 07:13:29 -05:00
|
|
|
@plural_name ||= singular_name.pluralize
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def i18n_scope # :doc:
|
2016-08-06 13:15:47 -04:00
|
|
|
@i18n_scope ||= file_path.tr("/", ".")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def table_name # :doc:
|
2010-01-25 07:13:29 -05:00
|
|
|
@table_name ||= begin
|
|
|
|
base = pluralize_table_names? ? plural_name : singular_name
|
2016-08-06 13:15:47 -04:00
|
|
|
(class_path + [base]).join("_")
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-06-23 10:50:21 -04:00
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def uncountable? # :doc:
|
2010-06-19 17:35:54 -04:00
|
|
|
singular_name == plural_name
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def index_helper # :doc:
|
2017-09-20 01:04:12 -04:00
|
|
|
uncountable? ? "#{plural_route_name}_index" : plural_route_name
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def show_helper # :doc:
|
2017-09-20 01:04:12 -04:00
|
|
|
"#{singular_route_name}_url(@#{singular_table_name})"
|
2015-10-26 20:41:27 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def edit_helper # :doc:
|
2015-10-26 20:41:27 -04:00
|
|
|
"edit_#{show_helper}"
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def new_helper # :doc:
|
2017-09-20 01:04:12 -04:00
|
|
|
"new_#{singular_route_name}_url"
|
2015-10-26 20:41:27 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def singular_table_name # :doc:
|
2011-02-07 20:26:42 -05:00
|
|
|
@singular_table_name ||= (pluralize_table_names? ? table_name.singularize : table_name)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def plural_table_name # :doc:
|
2011-02-07 20:26:42 -05:00
|
|
|
@plural_table_name ||= (pluralize_table_names? ? table_name : table_name.pluralize)
|
2010-06-23 03:03:34 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def plural_file_name # :doc:
|
2010-06-23 03:03:34 -04:00
|
|
|
@plural_file_name ||= file_name.pluralize
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def fixture_file_name # :doc:
|
2015-03-26 21:08:04 -04:00
|
|
|
@fixture_file_name ||= (pluralize_table_names? ? plural_file_name : file_name)
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def route_url # :doc:
|
2016-08-16 03:30:11 -04:00
|
|
|
@route_url ||= class_path.collect { |dname| "/" + dname }.join + "/" + plural_file_name
|
2010-06-19 17:35:54 -04:00
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def url_helper_prefix # :doc:
|
2016-08-06 13:15:47 -04:00
|
|
|
@url_helper_prefix ||= (class_path + [file_name]).join("_")
|
2016-01-06 01:01:14 -05:00
|
|
|
end
|
|
|
|
|
2014-12-30 12:00:04 -05:00
|
|
|
# Tries to retrieve the application name or simply return application.
|
2016-12-23 05:20:01 -05:00
|
|
|
def application_name # :doc:
|
2010-01-25 07:13:29 -05:00
|
|
|
if defined?(Rails) && Rails.application
|
2016-08-06 13:15:47 -04:00
|
|
|
Rails.application.class.name.split("::").first.underscore
|
2009-06-23 10:50:21 -04:00
|
|
|
else
|
2010-01-25 07:13:29 -05:00
|
|
|
"application"
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-08-09 06:24:40 -04:00
|
|
|
|
2017-09-20 01:04:12 -04:00
|
|
|
def redirect_resource_name # :doc:
|
|
|
|
model_resource_name(prefix: "@")
|
|
|
|
end
|
|
|
|
|
|
|
|
def model_resource_name(prefix: "") # :doc:
|
|
|
|
resource_name = "#{prefix}#{singular_table_name}"
|
2017-11-05 00:37:21 -04:00
|
|
|
if options[:model_name]
|
2017-09-20 01:04:12 -04:00
|
|
|
"[#{controller_class_path.map { |name| ":" + name }.join(", ")}, #{resource_name}]"
|
2017-11-05 00:37:21 -04:00
|
|
|
else
|
|
|
|
resource_name
|
2017-09-20 01:04:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def singular_route_name # :doc:
|
2017-11-05 00:37:21 -04:00
|
|
|
if options[:model_name]
|
2017-09-20 01:04:12 -04:00
|
|
|
"#{controller_class_path.join('_')}_#{singular_table_name}"
|
2017-11-05 00:37:21 -04:00
|
|
|
else
|
|
|
|
singular_table_name
|
2017-09-20 01:04:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_route_name # :doc:
|
2017-11-05 00:37:21 -04:00
|
|
|
if options[:model_name]
|
2017-09-20 01:04:12 -04:00
|
|
|
"#{controller_class_path.join('_')}_#{plural_table_name}"
|
2017-11-05 00:37:21 -04:00
|
|
|
else
|
|
|
|
plural_table_name
|
2017-09-20 01:04:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def assign_names!(name)
|
2016-08-06 13:15:47 -04:00
|
|
|
@class_path = name.include?("/") ? name.split("/") : name.split("::")
|
2014-10-27 12:28:53 -04:00
|
|
|
@class_path.map!(&:underscore)
|
2010-01-25 07:13:29 -05:00
|
|
|
@file_name = @class_path.pop
|
2009-06-23 10:50:21 -04:00
|
|
|
end
|
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
# Convert attributes array into GeneratedAttribute objects.
|
2016-12-23 05:20:01 -05:00
|
|
|
def parse_attributes!
|
2011-08-17 05:16:04 -04:00
|
|
|
self.attributes = (attributes || []).map do |attr|
|
2011-12-24 04:38:19 -05:00
|
|
|
Rails::Generators::GeneratedAttribute.parse(attr)
|
2009-06-23 12:12:37 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def attributes_names # :doc:
|
2012-12-09 13:53:23 -05:00
|
|
|
@attributes_names ||= attributes.each_with_object([]) do |a, names|
|
2012-12-09 15:01:03 -05:00
|
|
|
names << a.column_name
|
2016-08-06 13:15:47 -04:00
|
|
|
names << "password_confirmation" if a.password_digest?
|
2012-12-09 13:53:23 -05:00
|
|
|
names << "#{a.name}_type" if a.polymorphic?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def pluralize_table_names? # :doc:
|
2009-07-01 14:30:47 -04:00
|
|
|
!defined?(ActiveRecord::Base) || ActiveRecord::Base.pluralize_table_names
|
|
|
|
end
|
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
def mountable_engine? # :doc:
|
2015-06-27 23:42:02 -04:00
|
|
|
defined?(ENGINE_ROOT) && namespaced?
|
|
|
|
end
|
|
|
|
|
2009-06-30 07:25:27 -04:00
|
|
|
# Add a class collisions name to be checked on class initialization. You
|
|
|
|
# can supply a hash with a :prefix or :suffix to be tested.
|
|
|
|
#
|
|
|
|
# ==== Examples
|
|
|
|
#
|
2012-10-14 19:00:57 -04:00
|
|
|
# check_class_collision suffix: "Decorator"
|
2009-06-26 04:40:02 -04:00
|
|
|
#
|
2009-06-30 07:25:27 -04:00
|
|
|
# If the generator is invoked with class name Admin, it will check for
|
2012-10-14 19:00:57 -04:00
|
|
|
# the presence of "AdminDecorator".
|
2009-06-30 07:25:27 -04:00
|
|
|
#
|
2016-12-23 05:20:01 -05:00
|
|
|
def self.check_class_collision(options = {}) # :doc:
|
2009-06-30 07:25:27 -04:00
|
|
|
define_method :check_class_collision do
|
2017-11-07 00:23:16 -05:00
|
|
|
name = if respond_to?(:controller_class_name) # for ResourceHelpers
|
2009-06-30 07:25:27 -04:00
|
|
|
controller_class_name
|
|
|
|
else
|
|
|
|
class_name
|
|
|
|
end
|
|
|
|
|
|
|
|
class_collisions "#{options[:prefix]}#{name}#{options[:suffix]}"
|
|
|
|
end
|
2009-06-26 04:40:02 -04:00
|
|
|
end
|
2009-06-30 07:25:27 -04:00
|
|
|
end
|
2009-06-23 10:19:23 -04:00
|
|
|
end
|
|
|
|
end
|