2017-12-21 17:36:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-11-15 12:56:05 +00:00
|
|
|
require 'rails/generators/base'
|
|
|
|
|
2010-06-13 11:04:24 +00:00
|
|
|
module Devise
|
|
|
|
module Generators
|
2011-06-27 14:31:29 +00:00
|
|
|
# Include this module in your generator to generate Devise views.
|
|
|
|
# `copy_views` is the main method and by default copies all views
|
|
|
|
# with forms.
|
|
|
|
module ViewPathTemplates #:nodoc:
|
2011-06-27 14:20:32 +00:00
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2014-02-25 16:42:55 +00:00
|
|
|
argument :scope, required: false, default: nil,
|
|
|
|
desc: "The scope to copy views to"
|
2011-06-27 14:31:29 +00:00
|
|
|
|
2012-06-18 13:18:05 +00:00
|
|
|
# Le sigh, ensure Thor won't handle opts as args
|
2012-06-19 09:27:30 +00:00
|
|
|
# It should be fixed in future Rails releases
|
2014-02-25 16:42:55 +00:00
|
|
|
class_option :form_builder, aliases: "-b"
|
2012-06-18 13:18:05 +00:00
|
|
|
class_option :markerb
|
2014-03-06 21:41:11 +00:00
|
|
|
class_option :views, aliases: "-v", type: :array, desc: "Select specific view directories to generate (confirmations, passwords, registrations, sessions, unlocks, mailer)"
|
2012-06-18 13:18:05 +00:00
|
|
|
|
2011-06-27 14:31:29 +00:00
|
|
|
public_task :copy_views
|
|
|
|
end
|
|
|
|
|
|
|
|
def copy_views
|
2014-03-06 21:41:11 +00:00
|
|
|
if options[:views]
|
|
|
|
options[:views].each do |directory|
|
2014-03-04 22:47:19 +00:00
|
|
|
view_directory directory.to_sym
|
|
|
|
end
|
|
|
|
else
|
|
|
|
view_directory :confirmations
|
|
|
|
view_directory :passwords
|
|
|
|
view_directory :registrations
|
|
|
|
view_directory :sessions
|
|
|
|
view_directory :unlocks
|
|
|
|
end
|
2011-06-27 14:20:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2011-09-13 07:19:48 +00:00
|
|
|
def view_directory(name, _target_path = nil)
|
2013-02-06 18:37:12 +00:00
|
|
|
directory name.to_s, _target_path || "#{target_path}/#{name}" do |content|
|
|
|
|
if scope
|
2015-10-21 18:53:03 +00:00
|
|
|
content.gsub "devise/shared/links", "#{plural_scope}/shared/links"
|
2013-02-06 18:37:12 +00:00
|
|
|
else
|
|
|
|
content
|
|
|
|
end
|
|
|
|
end
|
2011-06-27 14:20:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_path
|
2015-10-21 18:53:03 +00:00
|
|
|
@target_path ||= "app/views/#{plural_scope || :devise}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def plural_scope
|
|
|
|
@plural_scope ||= scope.presence && scope.underscore.pluralize
|
2011-06-27 14:20:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-03 13:40:59 +00:00
|
|
|
class SharedViewsGenerator < Rails::Generators::Base #:nodoc:
|
|
|
|
include ViewPathTemplates
|
|
|
|
source_root File.expand_path("../../../../app/views/devise", __FILE__)
|
|
|
|
desc "Copies shared Devise views to your application."
|
2012-03-03 14:32:58 +00:00
|
|
|
hide!
|
2012-03-03 13:40:59 +00:00
|
|
|
|
|
|
|
# Override copy_views to just copy mailer and shared.
|
|
|
|
def copy_views
|
|
|
|
view_directory :shared
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-27 14:31:29 +00:00
|
|
|
class FormForGenerator < Rails::Generators::Base #:nodoc:
|
2011-06-27 14:20:32 +00:00
|
|
|
include ViewPathTemplates
|
2011-06-27 14:50:28 +00:00
|
|
|
source_root File.expand_path("../../../../app/views/devise", __FILE__)
|
2011-06-27 14:20:32 +00:00
|
|
|
desc "Copies default Devise views to your application."
|
2012-02-03 08:04:05 +00:00
|
|
|
hide!
|
2011-06-27 14:31:29 +00:00
|
|
|
end
|
2010-06-13 11:04:24 +00:00
|
|
|
|
2011-06-27 14:31:29 +00:00
|
|
|
class SimpleFormForGenerator < Rails::Generators::Base #:nodoc:
|
|
|
|
include ViewPathTemplates
|
2011-06-30 18:09:54 +00:00
|
|
|
source_root File.expand_path("../../templates/simple_form_for", __FILE__)
|
2011-06-27 14:31:29 +00:00
|
|
|
desc "Copies simple form enabled views to your application."
|
2012-02-03 08:04:05 +00:00
|
|
|
hide!
|
2015-08-27 15:12:13 +00:00
|
|
|
|
|
|
|
def copy_views
|
|
|
|
if options[:views]
|
|
|
|
options[:views].delete('mailer')
|
|
|
|
end
|
|
|
|
super
|
|
|
|
end
|
2011-06-27 14:31:29 +00:00
|
|
|
end
|
|
|
|
|
2011-09-14 04:42:10 +00:00
|
|
|
class ErbGenerator < Rails::Generators::Base #:nodoc:
|
2011-09-13 07:19:48 +00:00
|
|
|
include ViewPathTemplates
|
2011-09-14 04:42:10 +00:00
|
|
|
source_root File.expand_path("../../../../app/views/devise", __FILE__)
|
|
|
|
desc "Copies Devise mail erb views to your application."
|
2012-02-03 08:04:05 +00:00
|
|
|
hide!
|
2011-09-14 04:42:10 +00:00
|
|
|
|
|
|
|
def copy_views
|
2014-03-06 21:41:11 +00:00
|
|
|
if !options[:views] || options[:views].include?('mailer')
|
2014-03-04 22:47:19 +00:00
|
|
|
view_directory :mailer
|
|
|
|
end
|
2011-09-14 04:42:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class MarkerbGenerator < Rails::Generators::Base #:nodoc:
|
|
|
|
include ViewPathTemplates
|
|
|
|
source_root File.expand_path("../../templates", __FILE__)
|
|
|
|
desc "Copies Devise mail markerb views to your application."
|
2012-02-03 08:04:05 +00:00
|
|
|
hide!
|
2011-09-13 07:19:48 +00:00
|
|
|
|
|
|
|
def copy_views
|
2014-03-06 21:41:11 +00:00
|
|
|
if !options[:views] || options[:views].include?('mailer')
|
2014-03-04 22:47:19 +00:00
|
|
|
view_directory :markerb, target_path
|
|
|
|
end
|
2011-09-13 07:19:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def target_path
|
2015-10-21 18:53:03 +00:00
|
|
|
"app/views/#{plural_scope || :devise}/mailer"
|
2011-09-13 07:19:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-27 14:31:29 +00:00
|
|
|
class ViewsGenerator < Rails::Generators::Base
|
|
|
|
desc "Copies Devise views to your application."
|
|
|
|
|
2014-02-25 16:42:55 +00:00
|
|
|
argument :scope, required: false, default: nil,
|
|
|
|
desc: "The scope to copy views to"
|
2011-06-27 14:31:29 +00:00
|
|
|
|
2012-03-03 13:40:59 +00:00
|
|
|
invoke SharedViewsGenerator
|
2012-06-18 11:44:43 +00:00
|
|
|
|
2014-02-25 16:42:55 +00:00
|
|
|
hook_for :form_builder, aliases: "-b",
|
|
|
|
desc: "Form builder to be used",
|
|
|
|
default: defined?(SimpleForm) ? "simple_form_for" : "form_for"
|
2011-09-14 04:42:10 +00:00
|
|
|
|
2014-02-25 16:42:55 +00:00
|
|
|
hook_for :markerb, desc: "Generate markerb instead of erb mail views",
|
2016-12-19 23:14:41 +00:00
|
|
|
default: defined?(Markerb),
|
|
|
|
type: :boolean
|
|
|
|
|
|
|
|
hook_for :erb, desc: "Generate erb mail views",
|
|
|
|
default: !defined?(Markerb),
|
2014-02-25 16:42:55 +00:00
|
|
|
type: :boolean
|
2010-06-13 11:04:24 +00:00
|
|
|
end
|
|
|
|
end
|
2011-01-11 10:56:43 +00:00
|
|
|
end
|