2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-21 09:08:33 -04:00
|
|
|
require "rails/generators/active_model"
|
|
|
|
require "rails/generators/model_helpers"
|
2009-10-17 14:56:44 -04:00
|
|
|
|
2009-08-30 11:58:20 -04:00
|
|
|
module Rails
|
|
|
|
module Generators
|
|
|
|
# Deal with controller names on scaffold and add some helpers to deal with
|
|
|
|
# ActiveModel.
|
2012-10-07 16:36:39 -04:00
|
|
|
module ResourceHelpers # :nodoc:
|
2009-08-30 11:58:20 -04:00
|
|
|
def self.included(base) #:nodoc:
|
2015-01-31 23:12:37 -05:00
|
|
|
base.include(Rails::Generators::ModelHelpers)
|
2013-07-13 01:38:00 -04:00
|
|
|
base.class_option :model_name, type: :string, desc: "ModelName to be used"
|
2009-08-30 11:58:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set controller variables on initialization.
|
|
|
|
def initialize(*args) #:nodoc:
|
|
|
|
super
|
2014-01-20 04:07:53 -05:00
|
|
|
controller_name = name
|
2013-07-13 01:38:00 -04:00
|
|
|
if options[:model_name]
|
|
|
|
self.name = options[:model_name]
|
2016-08-07 19:05:28 -04:00
|
|
|
assign_names!(name)
|
2013-07-13 01:38:00 -04:00
|
|
|
end
|
2009-08-30 11:58:20 -04:00
|
|
|
|
2013-07-13 01:38:00 -04:00
|
|
|
assign_controller_names!(controller_name.pluralize)
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-08-30 11:58:20 -04:00
|
|
|
|
2016-12-23 05:20:01 -05:00
|
|
|
private
|
2018-02-16 20:14:27 -05:00
|
|
|
attr_reader :controller_name, :controller_file_name
|
2016-12-23 05:20:01 -05:00
|
|
|
|
2010-01-25 07:13:29 -05:00
|
|
|
def controller_class_path
|
2013-07-13 01:38:00 -04:00
|
|
|
if options[:model_name]
|
|
|
|
@controller_class_path
|
|
|
|
else
|
|
|
|
class_path
|
|
|
|
end
|
2009-08-30 11:58:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-13 01:38:00 -04:00
|
|
|
def assign_controller_names!(name)
|
|
|
|
@controller_name = name
|
2016-08-06 13:15:47 -04:00
|
|
|
@controller_class_path = name.include?("/") ? name.split("/") : name.split("::")
|
2014-10-27 12:28:53 -04:00
|
|
|
@controller_class_path.map!(&:underscore)
|
2013-07-13 01:38:00 -04:00
|
|
|
@controller_file_name = @controller_class_path.pop
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_file_path
|
2016-08-06 13:15:47 -04:00
|
|
|
@controller_file_path ||= (controller_class_path + [controller_file_name]).join("/")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_class_name
|
2016-08-06 13:15:47 -04:00
|
|
|
(controller_class_path + [controller_file_name]).map!(&:camelize).join("::")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_i18n_scope
|
2016-08-06 13:15:47 -04:00
|
|
|
@controller_i18n_scope ||= controller_file_path.tr("/", ".")
|
2010-01-25 07:13:29 -05:00
|
|
|
end
|
2009-08-30 11:58:20 -04:00
|
|
|
|
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
|
2009-08-30 11:58:20 -04:00
|
|
|
# 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
|
2011-11-26 13:57:51 -05:00
|
|
|
"#{options[:orm].to_s.camelize}::Generators::ActiveModel".constantize
|
2011-10-09 07:45:55 -04:00
|
|
|
rescue NameError
|
2009-10-17 14:56:44 -04:00
|
|
|
Rails::Generators::ActiveModel
|
2009-08-30 11:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize ORM::Generators::ActiveModel to access instance methods.
|
2016-10-28 23:05:58 -04:00
|
|
|
def orm_instance(name = singular_table_name)
|
2011-11-26 11:11:17 -05:00
|
|
|
@orm_instance ||= orm_class.new(name)
|
2009-08-30 11:58:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|