1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/generators/resource_helpers.rb

83 lines
2.6 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "rails/generators/active_model"
require "rails/generators/model_helpers"
module Rails
module Generators
# Deal with controller names on scaffold and add some helpers to deal with
# ActiveModel.
module ResourceHelpers # :nodoc:
def self.included(base) #:nodoc:
base.include(Rails::Generators::ModelHelpers)
base.class_option :model_name, type: :string, desc: "ModelName to be used"
end
# Set controller variables on initialization.
def initialize(*args) #:nodoc:
super
controller_name = name
if options[:model_name]
self.name = options[:model_name]
assign_names!(name)
end
assign_controller_names!(controller_name.pluralize)
end
private
2018-02-16 20:14:27 -05:00
attr_reader :controller_name, :controller_file_name
def controller_class_path
if options[:model_name]
@controller_class_path
else
class_path
end
end
def assign_controller_names!(name)
@controller_name = name
@controller_class_path = name.include?("/") ? name.split("/") : name.split("::")
@controller_class_path.map!(&:underscore)
@controller_file_name = @controller_class_path.pop
end
def controller_file_path
@controller_file_path ||= (controller_class_path + [controller_file_name]).join("/")
end
def controller_class_name
(controller_class_path + [controller_file_name]).map!(&:camelize).join("::")
end
def controller_i18n_scope
@controller_i18n_scope ||= controller_file_path.tr("/", ".")
end
2011-03-04 00:19:58 -05:00
# Loads the ORM::Generators::ActiveModel class. This class is responsible
2017-01-25 10:54:55 -05:00
# to tell scaffold entities how to generate a specific method for the
# ORM. Check Rails::Generators::ActiveModel for more information.
def orm_class
@orm_class ||= begin
# Raise an error if the class_option :orm was not defined.
unless self.class.class_options[:orm]
raise "You need to have :orm as class option to invoke orm_class and orm_instance"
end
begin
"#{options[:orm].to_s.camelize}::Generators::ActiveModel".constantize
rescue NameError
Rails::Generators::ActiveModel
end
end
end
# Initialize ORM::Generators::ActiveModel to access instance methods.
def orm_instance(name = singular_table_name)
@orm_instance ||= orm_class.new(name)
end
end
end
end